How to use the Filtering on RecycleView in Android Studio.
Creating New Project.
Open android studio and create a new project.
File => New => New Project => Configure your new project => Select the form factor yours app will run on => Add an Activity to Mobile => Customize the Activity => Finish.
First we need to add Library to our project.
compile 'com.android.support:recyclerview-v7:25.2.0'
compile 'com.android.support:design:25.2.0'
Create Xml file in project.
Open => app => res => layout - activity_main.xml.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="com.gswebtechnologies.recycleviewfiltering.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<SearchView
android:id="@+id/searchView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<android.support.v7.widget.RecyclerView
android:id="@+id/recycleView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="12dp" />
</LinearLayout>
</RelativeLayout>
Create the Java file in project.
Open app => main => src = MainActivity.java
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.SearchView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity implements SearchView.OnQueryTextListener {
private static final String TAG = "MainActivity";
RecyclerView recyclerView;
SearchView searchView;
RecycleViewAdapter recycleViewAdapter;
private ArrayList<ListItem> listItems;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//getting the widget id
init();
//using the click listner in android
listener();
//set the data in listView
setList();
//SearchView
setSearchView();
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recycleViewAdapter = new RecycleViewAdapter(this, listItems);
recyclerView.setAdapter(recycleViewAdapter);
}
private void setSearchView() {
searchView.setIconifiedByDefault(false);
searchView.setOnQueryTextListener(this);
searchView.setSubmitButtonEnabled(true);
searchView.setQueryHint("Search Here");
}
private void init() {
recyclerView = (RecyclerView) findViewById(R.id.recycleView);
//SearchView
searchView = (SearchView) findViewById(R.id.searchView);
}
private void listener() {
}
public void setList() {
listItems = new ArrayList<ListItem>();
ListItem item = new ListItem();
item.setData("Google");
listItems.add(item);
item = new ListItem();
item.setData("Arun");
listItems.add(item);
item = new ListItem();
item.setData("Kamal");
listItems.add(item);
item = new ListItem();
item.setData("Balvinder");
listItems.add(item);
item = new ListItem();
item.setData("Manjeet");
listItems.add(item);
}
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
recycleViewAdapter.filter(newText);
return true;
}
}
Create the Java file in project.
Open app => main => src = RecycleViewAdapter.java
import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.ArrayList;
import java.util.List;
public class RecycleViewAdapter extends RecyclerView.Adapter<MyCustomViewHolder>{
private List<ListItem> listItems, filterList;
private Context mContext;
public RecycleViewAdapter(Context context, List<ListItem> listItems) {
this.listItems = listItems;
this.mContext = context;
this.filterList = new ArrayList<ListItem>();
// we copy the original list to the filter list and use it for setting row values
this.filterList.addAll(this.listItems);
}
@Override
public MyCustomViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.row_item, null);
MyCustomViewHolder viewHolder = new MyCustomViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(MyCustomViewHolder customViewHolder, int position) {
ListItem listItem = filterList.get(position);
customViewHolder.tvName.setText(listItem.name);
}
@Override
public int getItemCount() {
return (null != filterList ? filterList.size() : 0);
}
// Do Search...
public void filter(final String text) {
// Searching could be complex..so we will dispatch it to a different thread...
new Thread(new Runnable() {
@Override
public void run() {
// Clear the filter list
filterList.clear();
// If there is no search value, then add all original list items to filter list
if (TextUtils.isEmpty(text)) {
filterList.addAll(listItems);
} else {
// Iterate in the original List and add it to filter list...
for (ListItem item : listItems) {
if (item.name.toLowerCase().contains(text.toLowerCase()) ||
item.name.toLowerCase().contains(text.toLowerCase())) {
// Adding Matched items
filterList.add(item);
}
}
}
// Set on UI Thread
((Activity) mContext).runOnUiThread(new Runnable() {
@Override
public void run() {
// Notify the List that the DataSet has changed...
notifyDataSetChanged();
}
});
}
}).start();
}
}
Create the Java file in project.
Open app => main => src = MyCustomViewHolder.java
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
public class MyCustomViewHolder extends RecyclerView.ViewHolder {
protected ImageView imageView;
protected TextView tvName, tvPlace;
protected ImageView imgThumb;
public MyCustomViewHolder(View view) {
super(view);
this.tvName = (TextView) view.findViewById(R.id.txtName);
}
}
Create the Java file in project.
Open app => main => src = ListItem.java
public class ListItem {
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
String name;
public void setData(String name){
this.name=name;
}
}
Creating New Project.
Open android studio and create a new project.
File => New => New Project => Configure your new project => Select the form factor yours app will run on => Add an Activity to Mobile => Customize the Activity => Finish.
First we need to add Library to our project.
compile 'com.android.support:recyclerview-v7:25.2.0'
compile 'com.android.support:design:25.2.0'
Create Xml file in project.
Open => app => res => layout - activity_main.xml.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="com.gswebtechnologies.recycleviewfiltering.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<SearchView
android:id="@+id/searchView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<android.support.v7.widget.RecyclerView
android:id="@+id/recycleView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="12dp" />
</LinearLayout>
</RelativeLayout>
Create the Java file in project.
Open app => main => src = MainActivity.java
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.SearchView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity implements SearchView.OnQueryTextListener {
private static final String TAG = "MainActivity";
RecyclerView recyclerView;
SearchView searchView;
RecycleViewAdapter recycleViewAdapter;
private ArrayList<ListItem> listItems;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//getting the widget id
init();
//using the click listner in android
listener();
//set the data in listView
setList();
//SearchView
setSearchView();
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recycleViewAdapter = new RecycleViewAdapter(this, listItems);
recyclerView.setAdapter(recycleViewAdapter);
}
private void setSearchView() {
searchView.setIconifiedByDefault(false);
searchView.setOnQueryTextListener(this);
searchView.setSubmitButtonEnabled(true);
searchView.setQueryHint("Search Here");
}
private void init() {
recyclerView = (RecyclerView) findViewById(R.id.recycleView);
//SearchView
searchView = (SearchView) findViewById(R.id.searchView);
}
private void listener() {
}
public void setList() {
listItems = new ArrayList<ListItem>();
ListItem item = new ListItem();
item.setData("Google");
listItems.add(item);
item = new ListItem();
item.setData("Arun");
listItems.add(item);
item = new ListItem();
item.setData("Kamal");
listItems.add(item);
item = new ListItem();
item.setData("Balvinder");
listItems.add(item);
item = new ListItem();
item.setData("Manjeet");
listItems.add(item);
}
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
recycleViewAdapter.filter(newText);
return true;
}
}
Create the Java file in project.
Open app => main => src = RecycleViewAdapter.java
import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.ArrayList;
import java.util.List;
public class RecycleViewAdapter extends RecyclerView.Adapter<MyCustomViewHolder>{
private List<ListItem> listItems, filterList;
private Context mContext;
public RecycleViewAdapter(Context context, List<ListItem> listItems) {
this.listItems = listItems;
this.mContext = context;
this.filterList = new ArrayList<ListItem>();
// we copy the original list to the filter list and use it for setting row values
this.filterList.addAll(this.listItems);
}
@Override
public MyCustomViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.row_item, null);
MyCustomViewHolder viewHolder = new MyCustomViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(MyCustomViewHolder customViewHolder, int position) {
ListItem listItem = filterList.get(position);
customViewHolder.tvName.setText(listItem.name);
}
@Override
public int getItemCount() {
return (null != filterList ? filterList.size() : 0);
}
// Do Search...
public void filter(final String text) {
// Searching could be complex..so we will dispatch it to a different thread...
new Thread(new Runnable() {
@Override
public void run() {
// Clear the filter list
filterList.clear();
// If there is no search value, then add all original list items to filter list
if (TextUtils.isEmpty(text)) {
filterList.addAll(listItems);
} else {
// Iterate in the original List and add it to filter list...
for (ListItem item : listItems) {
if (item.name.toLowerCase().contains(text.toLowerCase()) ||
item.name.toLowerCase().contains(text.toLowerCase())) {
// Adding Matched items
filterList.add(item);
}
}
}
// Set on UI Thread
((Activity) mContext).runOnUiThread(new Runnable() {
@Override
public void run() {
// Notify the List that the DataSet has changed...
notifyDataSetChanged();
}
});
}
}).start();
}
}
Create the Java file in project.
Open app => main => src = MyCustomViewHolder.java
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
public class MyCustomViewHolder extends RecyclerView.ViewHolder {
protected ImageView imageView;
protected TextView tvName, tvPlace;
protected ImageView imgThumb;
public MyCustomViewHolder(View view) {
super(view);
this.tvName = (TextView) view.findViewById(R.id.txtName);
}
}
Create the Java file in project.
Open app => main => src = ListItem.java
public class ListItem {
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
String name;
public void setData(String name){
this.name=name;
}
}
May this code help you. Thanks!!!!!!