How to create recycler view UI layout in Android Studio.

In this blog, we are going to create a recycler view layout with the latest UI layout method which means we have a standard recycler view that includes one image, one title, and one description.

1st, we have to change the color system in the string.xml file.

<color name="purple_200">#024265</color>
    <color name="purple_500">#024265</color>
    <color name="purple_700">#024265</color>
    <color name="teal_200">#e6ebef</color>
    <color name="teal_700">#e6ebef</color>
    <color name="black">#FF000000</color>
    <color name="white">#FFFFFFFF</color>

2nd, we have to create a Shimmer Effect Dependency layout which is shown in recycler view items.

 // Shimmer Effect Dependency
    implementation("com.facebook.shimmer:shimmer:0.5.0")

3rd, we have to create a card.bg.xml layout which is shown in recycler view items.

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <stroke
        android:width="2dp"
        android:color="@color/purple_200" />

    <solid android:color="@color/teal_200" />
    <corners android:radius="2dp" />

</shape>

4th, we have to create an arrow_right.xml icon code for the description which is shown in recycler view items.

<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="24dp"
    android:height="24dp"
    android:autoMirrored="true"
    android:tint="@color/purple_200"
    android:viewportWidth="24"
    android:viewportHeight="24">
    <path
        android:fillColor="@android:color/white"
        android:pathData="M10,17l5,-5 -5,-5v10z" />
</vector>

5th, we have to create a model_layout.xml layout which is shown in recycler view items.

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:clickable="true"
    android:focusable="true"
    android:foreground="?attr/selectableItemBackground"
    app:cardBackgroundColor="@android:color/white"
    app:cardCornerRadius="2dp"
    app:cardElevation="4dp"
    app:cardUseCompatPadding="true">

    <com.facebook.shimmer.ShimmerFrameLayout
        android:id="@+id/shimmerLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/card_bg">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="2dp"
                android:orientation="horizontal"
                tools:ignore="UselessParent">

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="70dp"
                    android:background="@color/purple_200"
                    android:orientation="horizontal">

                    <RelativeLayout
                        android:layout_width="85dp"
                        android:layout_height="match_parent"
                        android:background="#ABF8EE"
                        android:layout_margin="2dp"
                        android:gravity="center"
                        android:padding="14dp">

                        <ImageView
                            android:id="@+id/modelImg"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:contentDescription="@string/app_name"
                            android:src="@drawable/icon" />

                    </RelativeLayout>

                    <View
                        android:layout_width="1dp"
                        android:layout_height="match_parent"
                        android:layout_marginStart="1.5dp"
                        android:background="@color/teal_200" />

                    <View
                        android:layout_width="1dp"
                        android:layout_height="match_parent"
                        android:layout_marginStart="1.5dp"
                        android:background="@color/teal_200" />

                    <View
                        android:layout_width="1dp"
                        android:layout_height="match_parent"
                        android:layout_marginStart="1.5dp"
                        android:background="@color/teal_200" />

                    <View
                        android:layout_width="1dp"
                        android:layout_height="match_parent"
                        android:layout_marginStart="1.5dp"
                        android:background="@color/teal_200" />

                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_marginStart="8dp"
                    android:gravity="center_vertical"
                    android:orientation="vertical">

                    <TextView
                        android:id="@+id/modelTxt"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_vertical"
                        android:fontFamily="@font/alata"
                        android:text="Bank Name Space"
                        android:textColor="@color/purple_200"
                        android:textSize="20sp" />

                    <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="-6dp"
                        android:orientation="horizontal">

                        <View
                            android:layout_width="4dp"
                            android:layout_height="10dp"
                            android:layout_gravity="center_vertical"
                            android:layout_marginStart="1dp"
                            android:layout_marginTop="1dp"
                            android:background="@color/purple_200" />

                        <View
                            android:layout_width="12dp"
                            android:layout_height="18dp"
                            android:layout_gravity="center_vertical"
                            android:layout_marginStart="-4.5dp"
                            android:layout_marginTop="1dp"
                            android:background="@drawable/arrow_right" />


                        <TextView
                            android:id="@+id/modelTxtDesc"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center_vertical"
                            android:layout_marginStart="-2dp"
                            android:fontFamily="@font/alatsi"
                            android:text="Description Details"
                            android:textColor="@color/purple_200"
                            android:textSize="12sp" />


                    </LinearLayout>


                </LinearLayout>

            </LinearLayout>

        </LinearLayout>

    </com.facebook.shimmer.ShimmerFrameLayout>

</androidx.cardview.widget.CardView>

6th, create a search.xml layout code that helps to find recycler view items.

7th, create a menu item which is given below.

first_action_bar menu system:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <item
        android:id="@+id/search"
        android:icon="@drawable/search"
        android:title="Search"
        app:actionViewClass="androidx.appcompat.widget.SearchView"
        app:showAsAction="ifRoom|collapseActionView" />

</menu>

second_action_bar menu system:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <item
        android:id="@+id/youtube"
        android:icon="@drawable/video_logo"
        android:title="YouTube"
        app:showAsAction="ifRoom" />

</menu>

8th, we have to create a custom toolbar layout that helps to show the app title name and search button which is the most important past of the recycler view layout.

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/purple_200">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/constraintLayout"
            android:layout_width="match_parent"
            android:layout_height="75dp"
            android:layout_margin="8dp"
            android:background="@color/teal_200"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0">

            <com.google.android.material.appbar.AppBarLayout
                android:id="@+id/appBarLayout"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_margin="2dp"
                android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent">

                <androidx.appcompat.widget.Toolbar
                    android:id="@+id/toolBar"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

                    <androidx.constraintlayout.widget.ConstraintLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content">

                        <TextView
                            android:id="@+id/textView"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:fontFamily="@font/alatsi"
                            android:shadowColor="@color/black"
                            android:shadowDx="1.5"
                            android:shadowDy="1.3"
                            android:shadowRadius="1.6"
                            android:text="@string/app_name"
                            android:textColor="@color/teal_200"
                            android:textSize="22sp"
                            android:textStyle="bold"
                            app:layout_constraintBottom_toBottomOf="parent"
                            app:layout_constraintEnd_toEndOf="parent"
                            app:layout_constraintHorizontal_bias="0"
                            app:layout_constraintStart_toStartOf="parent"
                            app:layout_constraintTop_toTopOf="parent" />

                    </androidx.constraintlayout.widget.ConstraintLayout>
                </androidx.appcompat.widget.Toolbar>
            </com.google.android.material.appbar.AppBarLayout>

        </androidx.constraintlayout.widget.ConstraintLayout>

        <View
            android:id="@+id/view"
            android:layout_width="match_parent"
            android:layout_height="8dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/constraintLayout" />

    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

9th, we have to create a recycler view layout with the under-constraint layout.

        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/constraintLayout2"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:background="@color/teal_200"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/view">

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/recyclerView"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:background="@color/teal_200"
                android:paddingLeft="12dp"
                android:paddingRight="12dp"
                app:layout_constraintBottom_toBottomOf="@+id/constraintLayout2"
                app:layout_constraintEnd_toEndOf="@+id/constraintLayout2"
                app:layout_constraintStart_toStartOf="@+id/constraintLayout2"
                app:layout_constraintTop_toTopOf="parent"
                tools:listitem="@layout/model_layout" />

        </androidx.constraintlayout.widget.ConstraintLayout>

10th, we have to create an ItemClickListener.java file which can help to show the recycler view click event system.

import android.view.View;

public interface ItemClickListener {
    void onItemClick(View view, int position);
}

11th, we have to create a Model.java file that can help to show the recycler view layout event system.

public class Model {
    private String title, disc;
    private int img;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getDisc() {
        return disc;
    }

    public void setDisc(String disc) {
        this.disc = disc;
    }

    public int getImg() {
        return img;
    }

    public void setImg(int img) {
        this.img = img;
    }
}

12th, we have to create a Holder.java file that can help to show the recycler view layout event system.


import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.facebook.shimmer.ShimmerFrameLayout;

public class Holder extends RecyclerView.ViewHolder implements View.OnClickListener {
    ImageView imageView;
    TextView textView, disc;
    private ItemClickListener itemClickListener;
    ShimmerFrameLayout shimmerFrameLayout;

    public Holder(@NonNull View itemView) {
        super(itemView);
        this.imageView = itemView.findViewById(R.id.modelImg);
        this.textView = itemView.findViewById(R.id.modelTxt);
        this.disc = itemView.findViewById(R.id.modelTxtDesc);
        this.shimmerFrameLayout = itemView.findViewById(R.id.shimmerLayout);
        itemView.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        this.itemClickListener.onItemClick(v, getLayoutPosition());

    }

    void setItemClickListener(ItemClickListener listener) {
        this.itemClickListener = listener;
    }
}

13th, we have to create an Adapter.java file that can help to show the recycler view item click event system.


import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Filter;
import android.widget.Filterable;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

public class Adapter extends RecyclerView.Adapter<Holder> implements Filterable {

    Context c;
    private final ArrayList<Model> models;
    private final ArrayList<Model> modelItemFull;
    boolean showShimmer = true;

    public Adapter(Context c, ArrayList<Model> models) {
        this.c = c;
        this.models = models;
        this.modelItemFull = new ArrayList<>(models);
    }

    @NonNull
    @Override
    public Holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.model_layout, null);
        return new Holder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull Holder holder, int position) {
        if (showShimmer) {
            holder.shimmerFrameLayout.startShimmer();
        } else {
            holder.shimmerFrameLayout.stopShimmer();
            holder.shimmerFrameLayout.setShimmer(null);
            holder.imageView.setImageResource(models.get(position).getImg());
            holder.textView.setText(models.get(position).getTitle());
            holder.disc.setText(models.get(position).getDisc());
            holder.setItemClickListener((view, position1) -> {
                if ("Indian & Allahabad Bank".equals(models.get(position1).getTitle())) {
                    //  Intent sampleApps = new Intent(c, IndianBank.class);
                    // c.startActivity(sampleApps);

                }
                if ("Andhra & Union Bank".equals(models.get(position1).getTitle())) {
                    //   Intent sampleApps = new Intent(c, AndhraBank.class);
                    //  c.startActivity(sampleApps);

                }

                if ("Axis Bank Ltd.".equals(models.get(position1).getTitle())) {
                    //   Intent sampleApps = new Intent(c, AxisBank.class);
                    //  c.startActivity(sampleApps);

                }

            });
        }

    }

    @Override
    public int getItemCount() {
        int SHIMMER_ITEM_NUMBER = 10;
        return showShimmer ? SHIMMER_ITEM_NUMBER : models.size();
    }

    @Override
    public Filter getFilter() {
        return itemFilter;
    }

    public Filter itemFilter = new Filter() {
        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            List<Model> filteredList = new ArrayList<>();
            if (constraint == null || constraint.length() == 0) {
                filteredList.addAll(modelItemFull);
            } else {
                String filterPattern = constraint.toString().toLowerCase().trim();
                for (Model item : modelItemFull) {
                    if (item.getTitle().toLowerCase().contains(filterPattern)) {
                        filteredList.add(item);
                    }
                }
            }
            FilterResults results = new FilterResults();
            results.values = filteredList;
            return results;
        }

        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            models.clear();
            models.addAll((List) results.values);
            notifyDataSetChanged();

        }
    };
}

14th, we have to create an icon file that can help to show the recycler view item image view layout system.

Rating: 1 out of 6.

15th, we have to create a MainActivity.java file that can help to show the recycler view layout system.

private Adapter adapter;
Toolbar toolbar = findViewById(R.id.toolBar);
        setSupportActionBar(toolbar);
RecyclerView firstRecyclerView = findViewById(R.id.recyclerView);
        firstRecyclerView.setLayoutManager(new GridLayoutManager(this, 1));
        firstRecyclerView.setItemAnimator(new DefaultItemAnimator());
        adapter = new Adapter(this, getModels());
        firstRecyclerView.setAdapter(adapter);
  new Handler().postDelayed(() -> {
            getModels();
            adapter.showShimmer = false;
            adapter.notifyDataSetChanged();
        }, 3000);
    private ArrayList<Model> getModels() {
        ArrayList<Model> models = new ArrayList<>();
        Model click2code;

        click2code = new Model();
        click2code.setTitle("Indian & Allahabad Bank");
        click2code.setDisc("Private Sector Bank");
        click2code.setImg(R.drawable.indian);
        models.add(click2code);

        click2code = new Model();
        click2code.setTitle("Andhra & Union Bank");
        click2code.setDisc("Private Sector Bank");
        click2code.setImg(R.drawable.andhra);
        models.add(click2code);

        click2code = new Model();
        click2code.setTitle("Axis Bank Ltd.");
        click2code.setDisc("Private Sector Bank");
        click2code.setImg(R.drawable.axis);
        models.add(click2code);

        click2code = new Model();
        click2code.setTitle("Bandhan Bank Ltd.");
        click2code.setDisc("Private Sector Bank");
        click2code.setImg(R.drawable.bandhan);
        models.add(click2code);

        click2code = new Model();
        click2code.setTitle("Bank of Baroda");
        click2code.setDisc("Public Sector Bank");
        click2code.setImg(R.drawable.baroda);
        models.add(click2code);

        click2code = new Model();
        click2code.setTitle("Bank of India");
        click2code.setDisc("Public Sector Bank");
        click2code.setImg(R.drawable.boi);
        models.add(click2code);

        click2code = new Model();
        click2code.setTitle("Bank of Maharashtra");
        click2code.setDisc("Public Sector Bank");
        click2code.setImg(R.drawable.maharashtra);
        models.add(click2code);

        click2code = new Model();
        click2code.setTitle("Canara Bank");
        click2code.setDisc("Public Sector Bank");
        click2code.setImg(R.drawable.canara);
        models.add(click2code);

        click2code = new Model();
        click2code.setTitle("Catholic Syrian Bank");
        click2code.setDisc("Private Sector Bank");
        click2code.setImg(R.drawable.catholic);
        models.add(click2code);

        click2code = new Model();
        click2code.setTitle("Central Bank of India");
        click2code.setDisc("Public Sector Bank");
        click2code.setImg(R.drawable.cbi);
        models.add(click2code);

        click2code = new Model();
        click2code.setTitle("City Union Bank Ltd.");
        click2code.setDisc("Private Sector Bank");
        click2code.setImg(R.drawable.cityunion);
        models.add(click2code);

        click2code = new Model();
        click2code.setTitle("DCB Bank Ltd.");
        click2code.setDisc("Private Sector Bank");
        click2code.setImg(R.drawable.dcb);
        models.add(click2code);

        click2code = new Model();
        click2code.setTitle("Dhanlaxmi Bank Ltd.");
        click2code.setDisc("Private Sector Bank");
        click2code.setImg(R.drawable.dhanlaxmi);
        models.add(click2code);

        click2code = new Model();
        click2code.setTitle("Federal Bank Ltd.");
        click2code.setDisc("Private Sector Bank");
        click2code.setImg(R.drawable.federal);
        models.add(click2code);

        click2code = new Model();
        click2code.setTitle("HDFC Bank Ltd");
        click2code.setDisc("Private Sector Bank");
        click2code.setImg(R.drawable.hdfc);
        models.add(click2code);

        click2code = new Model();
        click2code.setTitle("ICICI Bank Ltd.");
        click2code.setDisc("Private Sector Bank");
        click2code.setImg(R.drawable.icici);
        models.add(click2code);

        click2code = new Model();
        click2code.setTitle("IDBI Bank Ltd.");
        click2code.setDisc("Private Sector Bank");
        click2code.setImg(R.drawable.idbi);
        models.add(click2code);

        click2code = new Model();
        click2code.setTitle("IDFC First Bank Ltd.");
        click2code.setDisc("Private Sector Bank");
        click2code.setImg(R.drawable.idfc);
        models.add(click2code);

        click2code = new Model();
        click2code.setTitle("India Post Payments Bank");
        click2code.setDisc("Payment Bank");
        click2code.setImg(R.drawable.india_post);
        models.add(click2code);

        click2code = new Model();
        click2code.setTitle("Indian Overseas Bank");
        click2code.setDisc("Public Sector Bank");
        click2code.setImg(R.drawable.ios);
        models.add(click2code);

        click2code = new Model();
        click2code.setTitle("Induslnd Bank Ltd");
        click2code.setDisc("Private Sector Bank");
        click2code.setImg(R.drawable.indusind);
        models.add(click2code);

        click2code = new Model();
        click2code.setTitle("Jammu & Kashmir Bank Ltd.");
        click2code.setDisc("Private Sector Bank");
        click2code.setImg(R.drawable.jk);
        models.add(click2code);

        click2code = new Model();
        click2code.setTitle("Jharkhand Rajya Gramin Bank");
        click2code.setDisc("Regional Rural Bank");
        click2code.setImg(R.drawable.jrgb_bank);
        models.add(click2code);

        click2code = new Model();
        click2code.setTitle("Karnataka Bank Ltd.");
        click2code.setDisc("Private Sector Bank");
        click2code.setImg(R.drawable.karnataka);
        models.add(click2code);

        click2code = new Model();
        click2code.setTitle("Karur Vysya Bank Ltd.");
        click2code.setDisc("Private Sector Bank");
        click2code.setImg(R.drawable.karur);
        models.add(click2code);

        click2code = new Model();
        click2code.setTitle("Kotak Mahindra Bank Ltd");
        click2code.setDisc("Private Sector Bank");
        click2code.setImg(R.drawable.kotak);
        models.add(click2code);

        click2code = new Model();
        click2code.setTitle("Nainital Bank Ltd.");
        click2code.setDisc("Private Sector Bank");
        click2code.setImg(R.drawable.nainital);
        models.add(click2code);

        click2code = new Model();
        click2code.setTitle("Punjab National Bank");
        click2code.setDisc("Public Sector Bank");
        click2code.setImg(R.drawable.pnb);
        models.add(click2code);

        click2code = new Model();
        click2code.setTitle("Punjab & Sind Bank");
        click2code.setDisc("Public Sector Bank");
        click2code.setImg(R.drawable.psb);
        models.add(click2code);

        click2code = new Model();
        click2code.setTitle("RBL Bank Ltd.");
        click2code.setDisc("Private Sector Bank");
        click2code.setImg(R.drawable.rbl);
        models.add(click2code);

        click2code = new Model();
        click2code.setTitle("State Bank of India");
        click2code.setDisc("Public Sector Bank");
        click2code.setImg(R.drawable.sbi);
        models.add(click2code);

        click2code = new Model();
        click2code.setTitle("South Indian Bank Ltd.");
        click2code.setDisc("Private Sector Bank");
        click2code.setImg(R.drawable.southindian);
        models.add(click2code);

        click2code = new Model();
        click2code.setTitle("UCO Bank");
        click2code.setDisc("Public Sector Bank");
        click2code.setImg(R.drawable.uco);
        models.add(click2code);

        return models;
    }
@Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        return super.onOptionsItemSelected(item);
    }
  @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.first_action_bar, menu);
        MenuItem item = menu.findItem(R.id.search);
        androidx.appcompat.widget.SearchView searchView = (androidx.appcompat.widget.SearchView) MenuItemCompat.getActionView(item);
        searchView.setQueryHint("Type Any Bank Name...");

        searchView.setOnQueryTextListener(new androidx.appcompat.widget.SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                adapter.getFilter().filter(newText);
                return false;
            }
        });

        return true;
    }

::: Thank You :::

Post Related Videos

Related Post

× Chat Now