Widget Recent Post No.

header ads

RecyclerView(AndroidX 2020): | Search bar with RecyclerView | Android Studio


 Step 1 Main.xml


<?xml version="1.0" encoding="utf-8"?>

<androidx.constraintlayout.widget.ConstraintLayout 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"

    android:background="#FFFFFF"

    tools:context=".MainActivity">


    <androidx.recyclerview.widget.RecyclerView

        android:id="@+id/userRecycler"

        android:layout_width="0dp"

        android:layout_height="0dp"

        android:layout_marginTop="8dp"

        app:layout_constraintBottom_toBottomOf="parent"

        app:layout_constraintEnd_toEndOf="parent"

        app:layout_constraintStart_toStartOf="parent"

        app:layout_constraintTop_toBottomOf="@+id/search_bar" />





    <EditText

        android:id="@+id/search_bar"

        android:layout_width="0dp"

        android:layout_height="50dp"

        android:layout_marginStart="16dp"

        android:layout_marginTop="16dp"

        android:layout_marginEnd="16dp"

        android:background="@drawable/search"

        android:drawableStart="@drawable/ic_baseline_search_24"

        android:ems="10"

        android:hint="@string/search_your_product"

        android:inputType="textPersonName"

        android:padding="16dp"

        android:paddingStart="16dp"

        app:layout_constraintEnd_toEndOf="parent"

        app:layout_constraintStart_toStartOf="parent"

        app:layout_constraintTop_toTopOf="parent"

        android:importantForAutofill="no" />


</androidx.constraintlayout.widget.ConstraintLayout>



Step 2 recyclerview_row_item.xml

<?xml version="1.0" encoding="utf-8"?>

<androidx.constraintlayout.widget.ConstraintLayout

    xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="wrap_content"

    xmlns:app="http://schemas.android.com/apk/res-auto">


    <de.hdodenhof.circleimageview.CircleImageView

        android:id="@+id/userImage"

        android:layout_width="66dp"

        android:layout_height="64dp"

        android:layout_marginStart="16dp"

        android:layout_marginTop="8dp"

        android:layout_marginBottom="8dp"

        app:layout_constraintBottom_toBottomOf="parent"

        app:layout_constraintStart_toStartOf="parent"

        app:layout_constraintTop_toTopOf="parent"

        app:srcCompat="@drawable/ic_launcher_background"

        android:layout_marginLeft="16dp" />


    <TextView

        android:id="@+id/userName"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_marginStart="16dp"

        android:text="TextView"

        android:textColor="#000000"

        android:textStyle="bold"

        app:layout_constraintStart_toEndOf="@+id/userImage"

        app:layout_constraintTop_toTopOf="@+id/userImage"

        android:layout_marginLeft="16dp" />


    <TextView

        android:id="@+id/userDesc"

        android:layout_width="0dp"

        android:layout_height="wrap_content"

        android:layout_marginTop="8dp"

        android:layout_marginEnd="16dp"

        android:text="TextView"

        app:layout_constraintEnd_toEndOf="parent"

        app:layout_constraintStart_toStartOf="@+id/userName"

        app:layout_constraintTop_toBottomOf="@+id/userName" />


    <View

        android:layout_width="0dp"

        android:layout_height="1dp"

        android:layout_marginTop="16dp"

        android:layout_marginEnd="16dp"

        android:background="@android:color/darker_gray"

        app:layout_constraintEnd_toEndOf="parent"

        app:layout_constraintStart_toStartOf="@+id/userDesc"

        app:layout_constraintTop_toBottomOf="@+id/userDesc"

        android:layout_marginRight="16dp" />


</androidx.constraintlayout.widget.ConstraintLayout>


Step 3 Main.java

package in.learncodewithrk.androidxrecyclerview;

import androidx.appcompat.app.AppCompatActivity;

import androidx.recyclerview.widget.LinearLayoutManager;

import androidx.recyclerview.widget.RecyclerView;


import android.os.Bundle;

import android.text.Editable;

import android.text.TextWatcher;

import android.widget.EditText;




import java.util.ArrayList;

import java.util.List;


public class MainActivity extends AppCompatActivity {


    RecyclerView userRecycler;

    RecyclerviewAdapter recyclerviewAdapter;

    EditText searchView;

    CharSequence search="";


    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);


        searchView = findViewById(R.id.search_bar);


        List<UserData> userDataList = new ArrayList<>();

        userDataList.add(new UserData("jeans","Lorem Ipsum is simply dummy text of the printing and typesetting industry.", R.drawable.jeans));

        userDataList.add(new UserData("top","Lorem Ipsum is simply dummy text of the printing and typesetting industry.", R.drawable.top));

        userDataList.add(new UserData("shoes","Lorem Ipsum is simply dummy text of the printing and typesetting industry.", R.drawable.shoes));

        userDataList.add(new UserData("vegetable","Lorem Ipsum is simply dummy text of the printing and typesetting industry.", R.drawable.vegetable));

        userDataList.add(new UserData("fruit","Lorem Ipsum is simply dummy text of the printing and typesetting industry.", R.drawable.fruit));

        userDataList.add(new UserData("chicken","Lorem Ipsum is simply dummy text of the printing and typesetting industry.", R.drawable.chicken));

        userDataList.add(new UserData("fish","Lorem Ipsum is simply dummy text of the printing and typesetting industry.", R.drawable.fish));

        userDataList.add(new UserData("top","Lorem Ipsum is simply dummy text of the printing and typesetting industry.", R.drawable.top));

        userDataList.add(new UserData("shoes","Lorem Ipsum is simply dummy text of the printing and typesetting industry.", R.drawable.shoes));

        userDataList.add(new UserData("fruit","Lorem Ipsum is simply dummy text of the printing and typesetting industry.", R.drawable.fruit));

        userDataList.add(new UserData("chicken","Lorem Ipsum is simply dummy text of the printing and typesetting industry.", R.drawable.chicken));

        userDataList.add(new UserData("vegetable","Lorem Ipsum is simply dummy text of the printing and typesetting industry.", R.drawable.vegetable));

        userDataList.add(new UserData("fruit","Lorem Ipsum is simply dummy text of the printing and typesetting industry.", R.drawable.fruit));

        userDataList.add(new UserData("fish","Lorem Ipsum is simply dummy text of the printing and typesetting industry.", R.drawable.fish));

        userDataList.add(new UserData("top","Lorem Ipsum is simply dummy text of the printing and typesetting industry.", R.drawable.top));

        userDataList.add(new UserData("shoes","Lorem Ipsum is simply dummy text of the printing and typesetting industry.", R.drawable.shoes));




        setUserRecycler(userDataList);



        searchView.addTextChangedListener(new TextWatcher() {

            @Override

            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {


            }


            @Override

            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {


                recyclerviewAdapter.getFilter().filter(charSequence);

                search = charSequence;

            }


            @Override

            public void afterTextChanged(Editable editable) {


            }

        });


    }


    private  void  setUserRecycler(List<UserData> userDataList){

        userRecycler = findViewById(R.id.userRecycler);

        RecyclerView.LayoutManager layoutManager= new LinearLayoutManager(this, RecyclerView.VERTICAL, false);

        userRecycler.setLayoutManager(layoutManager);

        recyclerviewAdapter = new RecyclerviewAdapter(this, userDataList);

        userRecycler.setAdapter(recyclerviewAdapter);

    }


}

READ MORE >>

GITHUB : https://github.com/LearncodeWithRk/AndroidXRecyclerView

Post a Comment

0 Comments