Searching Data Using EditText and Radio Button in Recycle View.

Searching Data in Recycle View.

Prepare: 

In this tutorial, we have a tendency to ar progressing to learn the way to use associate EditText search function in our android RecycleView .
There ar scenario wherever a RecycleView is heavily populated with that in non alphabetical order. it'll an app user a substantial quantity of your time to seek out what he or she is looking for during a RecycleView . this can be not sensible for user expertise.
A way to alleviate this problem is to feature an EditText filter to the RecycleView . The EditText read object can wire addTextChangedListener event to know when one character has been entered within the EditText field.
If you wish to use a SearchView in your RecycleView , I wrote another post that may offer you a transparent insight on the way to use android SearchView in RecycleView with a custom Adapter
We ar progressing to produce our main layout for our application associated within the layout we are going to add an RecycleView read and a RecycleView . the information for the RecycleView can use a straightforward Array Adapter to bind the information to the RecycleView . The EditText read are connected to the RecycleView Adapter so it will simply filter the content of the RecycleView with the words entered within the EditText read.

Before we have a tendency to begin, the primary issue i will be able to do is to list the surroundings and tools I employed in this android code piece however be happy to use no matter surroundings or tools you're at home with.

Demo:

                                 

In our MainActivity.java file, we are going to do the following things listed below in our code.

1. We get the instances of our EditText View and RecycleView by using the findViewById of the Activity class.
2. We create an array of Model
3. We create an instance of an ArrayAdapter and pass our model array object as a parameter, the application context and the in-built RecycleView layout in android.
4. We set the ArrayAdapter to ouRecycleView 
5. We attached an event listener to our RecycleView for each item clicked
6. We add a text changed event listener to our EditText. It will notify the application whenever a user inputs a character in the EditText.
7. Inside the onTextChanged method of the TextWatcher class, will get the filter method of our ArrayAdapter and pass the user input character as a parameter in this method.
The code for our MainActivity.java file is shown below. You can copy and paste it on your own project.

main_activity.xml:

We will start by adding an EditText View and ListView in our activity_main.xml layout file. If you are using Eclipse or Android Studio, you can switch to the design view and drag and drop these Views inside your layout file.
You can also copy and paste the following code below into the file if you don’t want to do it yourself.


 <?xml version="1.0" encoding="utf-8"?>  
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
   android:layout_width="match_parent"  
   android:layout_height="match_parent"  
   android:orientation="vertical">  
   <RelativeLayout  
     android:layout_width="match_parent"  
     android:layout_height="match_parent"  
     >  
     <LinearLayout  
       android:id="@+id/linerSearch"  
       android:layout_width="match_parent"  
       android:layout_height="wrap_content"  
       android:background="@drawable/searching_background"  
       android:gravity="center_horizontal"  
       android:orientation="vertical"  
       android:padding="15dp">  
       <EditText  
         android:id="@+id/edtSearch"  
         android:layout_width="match_parent"  
         android:layout_height="40dp"  
         android:layout_marginLeft="25dp"  
         android:layout_marginRight="25dp"  
         android:background="@drawable/editext_rounder_courner"  
         android:hint="Find lost people using Name,Place"  
         android:inputType="text"  
         android:maxLength="20"  
         android:paddingLeft="15dp" />  
       <RadioGroup  
         android:id="@+id/radioSearch"  
         style="@style/RadioButton_Theme"  
         android:layout_width="match_parent"  
         android:layout_height="wrap_content"  
         android:layout_gravity="center"  
         android:layout_marginLeft="50dp"  
         android:layout_marginRight="50dp"  
         android:layout_marginTop="5dp"  
         android:gravity="center_horizontal"  
         android:orientation="horizontal"  
         android:weightSum="3"  
         >  
         <TextView  
           android:layout_width="wrap_content"  
           android:layout_height="wrap_content"  
           android:layout_weight="1"  
           android:text="Select Option :"  
           android:textColor="@color/colorWhite" />  
         <RadioButton  
           android:id="@+id/radioName"  
           style="@style/RadioButton_Theme"  
           android:layout_width="wrap_content"  
           android:layout_height="wrap_content"  
           android:layout_weight="1"  
           android:checked="true"  
           android:text="Name"  
           android:textColor="@color/colorWhite" />  
         <RadioButton  
           android:id="@+id/radioPlace"  
           style="@style/RadioButton_Theme"  
           android:layout_width="wrap_content"  
           android:layout_height="wrap_content"  
           android:layout_weight="1"  
           android:text="Place"  
           android:textColor="@color/colorWhite" />  
       </RadioGroup>  
     </LinearLayout>  
     <android.support.v7.widget.RecyclerView  
       android:id="@+id/reycleView"  
       android:layout_width="match_parent"  
       android:layout_height="wrap_content"  
       android:layout_below="@+id/linerSearch"></android.support.v7.widget.RecyclerView>  
     <android.support.design.widget.FloatingActionButton  
       android:id="@+id/btnAdd"  
       android:layout_width="wrap_content"  
       android:layout_height="wrap_content"  
       android:layout_alignParentBottom="true"  
       android:layout_alignParentRight="true"  
       android:layout_margin="50dp"  
       android:src="@drawable/ic_add_victim" />  
   </RelativeLayout>  
 </LinearLayout>  


recycleview_raw file:


 <?xml version="1.0" encoding="utf-8"?>  
 <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"  
   xmlns:app="http://schemas.android.com/apk/res-auto"  
   android:id="@+id/cardView"  
   android:layout_width="match_parent"  
   android:layout_height="wrap_content"  
   android:layout_margin="6dp"  
   android:background="@color/primary_light"  
   android:elevation="5dp"  
   app:cardCornerRadius="15dp">  
   <LinearLayout  
     android:layout_width="match_parent"  
     android:layout_height="wrap_content"  
     android:orientation="horizontal"  
     android:weightSum="2">  
     <RelativeLayout  
       android:layout_width="match_parent"  
       android:layout_height="match_parent"  
       android:layout_weight="1">  
       <ImageView  
         android:id="@+id/imgVictim"  
         android:layout_width="200dp"  
         android:layout_height="200dp"  
         android:scaleType="fitXY"  
         android:src="@drawable/ic_logo" />  
       <ProgressBar  
         android:id="@+id/progressBar"  
         android:layout_width="wrap_content"  
         android:layout_height="wrap_content"  
         android:layout_centerInParent="true"  
         android:layout_gravity="center_vertical" />  
     </RelativeLayout>  
     <LinearLayout  
       android:layout_width="match_parent"  
       android:layout_height="match_parent"  
       android:layout_marginTop="15dp"  
       android:layout_marginLeft="5dp"  
       android:layout_weight="1"  
       android:orientation="vertical"  
       android:weightSum="5">  
       <TextView  
         android:id="@+id/tvName"  
         android:layout_width="wrap_content"  
         android:layout_height="wrap_content"  
         android:layout_weight="1"  
         android:text="@string/home_Name" />  
       <TextView  
         android:id="@+id/tvAge"  
         android:layout_width="wrap_content"  
         android:layout_height="wrap_content"  
         android:layout_weight="1"  
         android:text="@string/home_Age" />  
       <TextView  
         android:id="@+id/tvFounded_place"  
         android:layout_width="wrap_content"  
         android:layout_height="wrap_content"  
         android:layout_weight="1"  
         android:text="@string/home_founded_place" />  
       <View  
         android:layout_width="match_parent"  
         android:layout_height="1dp"  
         android:layout_gravity="center_horizontal"  
         android:layout_marginRight="2dp"  
         android:background="@color/white_light"  
         android:padding="10dp" />  
       <LinearLayout  
         android:layout_width="match_parent"  
         android:layout_height="wrap_content"  
         android:layout_marginTop="5dp"  
         android:layout_weight="1"  
         android:orientation="horizontal"  
         android:weightSum="2">  
         <LinearLayout  
           android:id="@+id/linear_view"  
           android:layout_width="wrap_content"  
           android:layout_height="wrap_content"  
           android:layout_weight="1"  
           android:orientation="vertical"  
           android:weightSum="2">  
           <TextView  
             android:layout_width="wrap_content"  
             android:layout_height="wrap_content"  
             android:layout_weight="1"  
             android:text="VIEW"  
             android:textColor="@color/colorAccent" />  
           <ImageView  
             android:id="@+id/imgView"  
             android:layout_width="wrap_content"  
             android:layout_height="wrap_content"  
             android:layout_weight="1"  
             android:src="@android:drawable/ic_menu_view" />  
         </LinearLayout>  
         <LinearLayout  
           android:layout_width="wrap_content"  
           android:layout_height="wrap_content"  
           android:layout_weight="1"  
           android:id="@+id/linear_share"  
           android:orientation="vertical"  
           android:weightSum="2">  
           <TextView  
             android:layout_width="wrap_content"  
             android:layout_height="wrap_content"  
             android:layout_weight="1"  
             android:text="SHARE"  
             android:textColor="@color/colorAccent" />  
           <ImageView  
             android:layout_width="wrap_content"  
             android:layout_height="wrap_content"  
             android:layout_weight="1"  
             android:src="@android:drawable/ic_menu_share" />  
         </LinearLayout>  
       </LinearLayout>  
     </LinearLayout>  
   </LinearLayout>  
 </android.support.v7.widget.CardView>  


Customadapter.java

 package com.example.aaru.findpeople.adapter;  
 import android.content.Context;  
 import android.content.Intent;  
 import android.graphics.Bitmap;  
 import android.graphics.drawable.Drawable;  
 import android.net.Uri;  
 import android.os.Environment;  
 import android.support.v7.widget.RecyclerView;  
 import android.text.TextUtils;  
 import android.util.Log;  
 import android.view.LayoutInflater;  
 import android.view.View;  
 import android.view.ViewGroup;  
 import android.widget.Filter;  
 import android.widget.Filterable;  
 import android.widget.ImageView;  
 import android.widget.LinearLayout;  
 import android.widget.ProgressBar;  
 import android.widget.TextView;  
 import android.widget.Toast;  
 import com.bumptech.glide.Glide;  
 import com.bumptech.glide.load.engine.DiskCacheStrategy;  
 import com.bumptech.glide.load.resource.drawable.GlideDrawable;  
 import com.bumptech.glide.request.RequestListener;  
 import com.bumptech.glide.request.animation.GlideAnimation;  
 import com.bumptech.glide.request.target.BitmapImageViewTarget;  
 import com.bumptech.glide.request.target.Target;  
 import com.example.aaru.findpeople.R;  
 import com.example.aaru.findpeople.activity.VictimViewActivity;  
 import com.example.aaru.findpeople.model.ModelMissingPeople;  
 import java.io.File;  
 import java.io.FileNotFoundException;  
 import java.io.FileOutputStream;  
 import java.io.IOException;  
 import java.util.ArrayList;  
 /**  
  * Created by Aaru on 1/15/2018.  
  */  
 public class MissingPeopleAdapter extends RecyclerView.Adapter<MissingPeopleAdapter.ViewHolder> {  
   private ArrayList<ModelMissingPeople> aryMissingPeopleData;  
   private Context context;  
   public MissingPeopleAdapter(ArrayList<ModelMissingPeople> aryMissingPeopleData, Context context, String strSelected) {  
     this.aryMissingPeopleData = aryMissingPeopleData;  
     this.context = context;  
   }  
   @Override  
   public MissingPeopleAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {  
     View view = LayoutInflater.from(context).inflate(R.layout.missing_people_raw, parent, false);  
     return new ViewHolder(view);  
   }  
   @Override  
   public void onBindViewHolder(final MissingPeopleAdapter.ViewHolder holder, final int position) {  
     holder.tvName.setText("Name :" + aryMissingPeopleData.get(position).getName());  
     holder.tvAge.setText("Age :" + aryMissingPeopleData.get(position).getAge());  
     holder.tvFounded_place.setText("Place :" + aryMissingPeopleData.get(position).getPlace());  
     try {  
       if (TextUtils.isEmpty(aryMissingPeopleData.get(position).getImg())) {  
         holder.imgVictim.setImageResource(R.drawable.ic_logo);  
       } else {  
         Glide.with(context)  
             .load(aryMissingPeopleData.get(position).getImg())  
             .placeholder(R.mipmap.ic_launcher_app)  
             .listener(new RequestListener<String, GlideDrawable>() {  
               @Override  
               public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {  
                 holder.progressBar.setVisibility(View.GONE);  
                 return false;  
               }  
               @Override  
               public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {  
                 holder.progressBar.setVisibility(View.GONE);  
                 return false;  
               }  
             })  
             .into(holder.imgVictim);  
       }  
     } catch (NullPointerException e) {  
       Toast.makeText(context, "Image Null :" + e.toString(), Toast.LENGTH_SHORT).show();  
     }  
   }  @Override  
   public int getItemCount() {  
     return aryMissingPeopleData.size();  
   }  
   public class ViewHolder extends RecyclerView.ViewHolder {  
     private TextView tvName, tvAge, tvFounded_place;  
     private ImageView imgVictim;  
       private ProgressBar progressBar;  
     private ViewHolder(View itemView)  
     {  
       super(itemView);  
       try {  
         tvName = itemView.findViewById(R.id.tvName);  
         tvAge = itemView.findViewById(R.id.tvAge);  
         tvFounded_place = itemView.findViewById(R.id.tvFounded_place);  
         imgVictim = itemView.findViewById(R.id.imgVictim);  
         progressBarrogressBar = itemView.findViewById(R.id.progressBar);  
       } catch (Exception e) {  
         e.printStackTrace();  
       }  
     }  
   }  
 }  


MainActivity.java

 package com.example.aaru.findpeople.dao;  
 import android.content.Intent;  
 import android.os.Bundle;  
 import android.support.annotation.Nullable;  
 import android.support.design.widget.FloatingActionButton;  
 import android.support.v4.app.Fragment;  
 import android.support.v7.widget.DefaultItemAnimator;  
 import android.support.v7.widget.LinearLayoutManager;  
 import android.support.v7.widget.RecyclerView;  
 import android.text.Editable;  
 import android.text.TextWatcher;  
 import android.util.Log;  
 import android.view.LayoutInflater;  
 import android.view.View;  
 import android.view.ViewGroup;  
 import android.widget.EditText;  
 import android.widget.RadioButton;  
 import android.widget.RadioGroup;  
 import com.example.aaru.findpeople.R;  
 import com.example.aaru.findpeople.activity.AddVictimActivity;  
 import com.example.aaru.findpeople.adapter.MissingPeopleAdapter;  
 import com.example.aaru.findpeople.model.ModelMissingPeople;  
 import com.example.aaru.findpeople.utills.EditableEditext;  
 import java.util.ArrayList;  
 /**  
  * Created by Aaru on 1/15/2018.  
  */  
 public class HomeFragment extends Fragment implements View.OnClickListener {  
   private RecyclerView recyclerView;  
   private MissingPeopleAdapter peopleAdapter;  
   private ArrayList<ModelMissingPeople> aryMissingPeopleSearch;  
   private ArrayList<ModelMissingPeople> aryMissingPeopleData = new ArrayList<>();  
   private FloatingActionButton btnAdd;  
   private RadioGroup radioSearch;  
   private RadioButton rdName, rdPlace;  
   private EditText edtSearch;  
   private String strSelected = "Name";  
 @Override  
 protected void onCreate(Bundle savedInstanceState) {  
   super.onCreate(savedInstanceState);  
   setContentView(R.layout.activity_main)  
     getID();  
     setListener()  
      missiongPeopleData();  
     radioData();  
     edtSearch.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) {  
         searchRadiobuttonVictim();  
       }  
       @Override  
       public void afterTextChanged(Editable editable)  
       {  
       }  
     });  
     return v;  
   }  
   private void searchRadiobuttonVictim() {  
     final String searchString = edtSearch.getText().toString();  
     final int serchLength = searchString.length();  
     if (searchString.trim().matches("")) {  
       aryMissingPeopleSearch.clear();  
       for (ModelMissingPeople m : aryMissingPeopleData) {  
         aryMissingPeopleSearch.add(m);  
         peopleAdapter.notifyDataSetChanged();  
       }  
     }  
     if (this.strSelected.matches("Place")) {  
       Log.e("Place :", this.strSelected);  
       aryMissingPeopleSearch.clear();  
       for (ModelMissingPeople m : aryMissingPeopleData) {  
         if (serchLength <= m.getPlace().length()) {  
           if (m.getPlace().toLowerCase().contains(searchString.toString().toLowerCase())) {  
             aryMissingPeopleSearch.add(m);  
           }  
         }  
       }  
       peopleAdapter.notifyDataSetChanged();  
     } else {  
       Log.e("Name :", this.strSelected);  
       aryMissingPeopleSearch.clear();  
       for (ModelMissingPeople m : aryMissingPeopleData) {  
         if (serchLength <= m.getName().length()) {  
           if (m.getName().toLowerCase().contains(searchString.toString().toLowerCase())) {  
             aryMissingPeopleSearch.add(m);  
           }  
         }  
         peopleAdapter.notifyDataSetChanged();  
       }  
     }  
   }  
   private void getID(View v) {  
     try {  
       recyclerView = findViewById(R.id.reycleView);  
       radioSearch = findViewById(R.id.radioSearch);  
       edtSearch = findViewById(R.id.edtSearch);  
     } catch (Exception e) {  
       e.printStackTrace();  
     }  
   }  
   private void setListener() {  
     edtSearch.setOnClickListener(this);  
   }  
   private void missiongPeopleData() {  
     aryMissingPeopleData.add(new ModelMissingPeople("Padvi Hansraj ", "http://db.and.nic.in/missing/readrealimage.aspx?id=433", "22", "Balck Skin ,Balck Hair", "Ahmedabad"));  
     aryMissingPeopleData.add(new ModelMissingPeople("Patel Moulik", "https://fi.ge.pgstatic.net/articles/processed/1451128824.80e7c89542c3415191dd90d70a4a38bc.jpg", "50", "Balck Skin ,Balck Hair", "Noroda"));  
     aryMissingPeopleData.add(new ModelMissingPeople("Patel Vivek", "https://s3.scoopwhoop.com/anj/1/706733341.jpg", "40", "Balck Skin ,Balck Hair", "Lal Darwaja"));  
     aryMissingPeopleData.add(new ModelMissingPeople("Patel Raj", "https://ichef.bbci.co.uk/news/660/cpsprodpb/F574/production/_92463826_mediaitem92461142.jpg", "30", "Balck Skin ,Balck Hair", "Ambawadi"));  
     aryMissingPeopleData.add(new ModelMissingPeople("Patel Krunal", "http://cgijeddah.mkcl.org/WebImages/missing1.png", "55", "Balck Skin ,Balck Hair", "Dharnidhar"));  
     aryMissingPeopleData.add(new ModelMissingPeople("Patel Ashvin", "https://imtcdl.files.wordpress.com/2012/09/ashwani1.jpg", "21", "Balck Skin ,Balck Hair", "Iskon Temple"));  
     aryMissingPeopleData.add(new ModelMissingPeople("Patel Mayank", "http://db.and.nic.in/missing/readrealimage.aspx?id=221", "23", "Balck Skin ,Balck Hair", "Income Text"));  
     aryMissingPeopleData.add(new ModelMissingPeople("Patel Akshay", "http://rpsdegreecollege.org/downloads/Results/Dec%202014/preety%20B.COM%203rd%20Sem.JPG", "29", "Balck Skin ,Balck Hair", "Commerce Six road"));  
     aryMissingPeopleData.add(new ModelMissingPeople("Padvi Hansraj ", "http://veronews.com/wp-content/uploads/2017/11/Missing-person-225x300.jpg", "22", "Balck Skin ,Balck Hair", "Ahmedabad"));  
     aryMissingPeopleData.add(new ModelMissingPeople("Patel Moulik", "https://s.ndtvimg.com/images/stories/milkhasingh300.jpg", "50", "White Skin ,Balck Hair", "Noroda"));  
     aryMissingPeopleData.add(new ModelMissingPeople("Patel Vivek", "http://rajbhavanjharkhand.nic.in/Officers/DSC_2767.jpg", "40", "Red Skin ,Balck Hair", "Lal Darwaja"));  
     aryMissingPeopleData.add(new ModelMissingPeople("Patel Raj", "https://0.academia-photos.com/6213762/2568969/2983270/s200_sreeraj.cr.jpg", "30", "Balck Skin ,Balck Hair", "Ambawadi"));  
     aryMissingPeopleData.add(new ModelMissingPeople("Patel Krunal", "http://db.and.nic.in/missing/readrealimage.aspx?id=433", "55", "Brown Skin ,Balck Hair", "Dharnidhar"));  
     aryMissingPeopleData.add(new ModelMissingPeople("Patel Ashvin", "http://cgijeddah.mkcl.org/WebImages/missing1.png", "21", "Brown Skin ,Balck Hair", "Iskon Temple"));  
     aryMissingPeopleData.add(new ModelMissingPeople("Patel Mayank", "http://db.and.nic.in/missing/readrealimage.aspx?id=221", "23", "Balck Skin ,Balck Hair", "Income Text"));  
     aryMissingPeopleData.add(new ModelMissingPeople("Patel Akshay", "http://rajbhavanjharkhand.nic.in/Officers/DSC_2767.jpg", "29", "Balck Skin ,Balck Hair", "Commerce Six road"));  
     aryMissingPeopleSearch = new ArrayList<>(aryMissingPeopleData);  
     peopleAdapter = new MissingPeopleAdapter(aryMissingPeopleSearch, getActivity(), strSelected);  
     recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));  
     recyclerView.setItemAnimator(new DefaultItemAnimator());  
     recyclerView.setHasFixedSize(true);  
     recyclerView.setAdapter(peopleAdapter);  
   }  
   private void radioData() {  
     radioSearch.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {  
       @Override  
       public void onCheckedChanged(RadioGroup radioGroup, int i) {  
         rdName = radioGroup.findViewById(R.id.radioName);  
         rdPlace = radioGroup.findViewById(R.id.radioPlace);  
         switch (i) {  
           case R.id.radioName:  
             strSelected = "Name";  
 //            searchVictim();  
             searchRadiobuttonVictim();  
             Log.e("Radio Name :", strSelected);  
             break;  
           case R.id.radioPlace:  
             strSelected = "Place";  
 //            searchVictim();  
             searchRadiobuttonVictim();  
             Log.e("Radio place :", strSelected);  
             break;  
         }  
       }  
     });  
   }  
   @Override  
   public void onClick(View view) {  
     switch (view.getId()) {  
       case R.id.edtSearch:  
                break;  
     }  
   }  
 }  



Comments