RecyclerView with SearchView in Android


RecyclerView with SearchView in Android


Output Screen:


       

implementation 'com.google.android.material:material:1.1.0'

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.helloworld">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

Student.java
package com.example.helloworld;

public class Student {
private String name;
private float marks;
private int roll;
private String email;

public Student(String name, float marks, int roll, String email) {
this.name = name;
this.marks = marks;
this.roll = roll;
this.email = email;
}

public Student() {
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public float getMarks() {
return marks;
}

public void setMarks(float marks) {
this.marks = marks;
}

public int getRoll() {
return roll;
}

public void setRoll(int roll) {
this.roll = roll;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}
}

MainActivity.java
package com.example.helloworld;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.text.TextUtils;
import android.widget.SearchView;

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

public class MainActivity extends AppCompatActivity {

private RecyclerView recyclerView;
private SearchView searchView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

initViews();

setDummyData();

recyclerView.setLayoutManager(new LinearLayoutManager(this));

final CustomStudentAdapter customStudentAdapter = new CustomStudentAdapter(studentList);

recyclerView.setAdapter(customStudentAdapter);

searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {

return false;
}

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

private void initViews() {
searchView = findViewById(R.id.searchitem);
recyclerView = findViewById(R.id.myrec);
}

private List<Student> studentList = new ArrayList<>();

private void setDummyData() {
Student s1 = new Student("Arpit", 67.5f, 1, "arpit@gmail.com");
Student s2 = new Student("Vijay", 57.5f, 2, "vijay@gmail.com");
Student s3 = new Student("Sonu", 76.5f, 3, "sonu@gmail.com");
Student s4 = new Student("Rahul", 59.5f, 4, "rahul@gmail.com");
Student s5 = new Student("Deep", 66.5f, 5, "deep@gmail.com");
Student s6 = new Student("Suresh", 77.5f, 6, "suresh@gmail.com");
Student s7 = new Student("Ashok", 49.5f, 7, "ashok@gmail.com");
Student s8 = new Student("Seema", 88.5f, 8, "seema@gmail.com");
Student s9 = new Student("Anamika", 55.5f, 9, "anamika@gmail.com");
Student s10 = new Student("Chandan", 69.5f, 10, "chandan@gmail.com");
Student s11 = new Student("Syed", 70.5f, 11, "syed@gmail.com");
Student s12 = new Student("Salim", 71.5f, 12, "salim@gmail.com");

studentList.add(s1);
studentList.add(s2);
studentList.add(s3);
studentList.add(s4);
studentList.add(s5);
studentList.add(s6);
studentList.add(s7);
studentList.add(s8);
studentList.add(s9);
studentList.add(s10);
studentList.add(s11);
studentList.add(s12);

}

}

CustomStudentAdapter.java
package com.example.helloworld;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.TextView;

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

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

public class CustomStudentAdapter extends RecyclerView.Adapter<CustomStudentAdapter.MyView>
implements Filterable {

private List<Student> studentList;
private List<Student> orignalstudentList;

public CustomStudentAdapter(List<Student> studentList) {
this.studentList = studentList;
orignalstudentList=studentList;
}

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

@Override
public void onBindViewHolder(@NonNull MyView holder, int position) {
Student student = orignalstudentList.get(position);
holder.troll.setText(String.valueOf(student.getRoll()));
holder.tname.setText(student.getName());
holder.tmarks.setText(String.valueOf(student.getMarks()));
holder.temail.setText(student.getEmail());
}

@Override
public int getItemCount() {
return orignalstudentList.size();
}

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

private Filter myFilter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {

if (constraint == null || constraint.length() == 0) {
orignalstudentList=studentList;
} else {
List<Student> filteredList = new ArrayList<>();
String filterPattern = constraint.toString().toLowerCase().trim();
for (Student student : studentList) {
if (student.getName().toLowerCase().startsWith(filterPattern)) {
filteredList.add(student);
}
}
orignalstudentList=filteredList;
}
FilterResults results = new FilterResults();
results.values = orignalstudentList;
return results;
}

@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
orignalstudentList=(List) results.values;
notifyDataSetChanged();
}
};

class MyView extends RecyclerView.ViewHolder {
private TextView troll, tname, tmarks, temail;

public MyView(@NonNull View itemView) {
super(itemView);
troll = itemView.findViewById(R.id.roll);
tname = itemView.findViewById(R.id.name);
tmarks = itemView.findViewById(R.id.marks);
temail = itemView.findViewById(R.id.email);
}
}
}

res/drawable/edit_back.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke
android:width="1dp"
android:color="@color/black" />
</shape>

res/layout/activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<SearchView
android:id="@+id/searchitem"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:layout_marginRight="20dp"
android:background="@drawable/edit_back"
android:iconifiedByDefault="false"
android:queryHint="Search Here" />

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/myrec"
android:layout_marginRight="20dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="20dp"
android:layout_width="match_parent"
android:layout_height="match_parent" />


</LinearLayout>

res/layout/item.xml
<?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"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardMaxElevation="10dp"
app:cardUseCompatPadding="true">


<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stretchColumns="*">


<TableRow>

<TextView
android:text="Roll : "
android:textStyle="bold" />

<TextView android:id="@+id/roll" />
</TableRow>

<TableRow>

<TextView
android:text="Name : "
android:textStyle="bold" />

<TextView android:id="@+id/name" />
</TableRow>

<TableRow>

<TextView
android:text="Marks : "
android:textStyle="bold" />

<TextView android:id="@+id/marks" />
</TableRow>

<TableRow>

<TextView
android:text="Email : "
android:textStyle="bold" />

<TextView android:id="@+id/email" />
</TableRow>
</TableLayout>


</androidx.cardview.widget.CardView>

res/values/colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#6200EE</color>
<color name="colorPrimaryDark">#3700B3</color>
<color name="colorAccent">#03DAC5</color>

<color name="darkyellow">#D19F09</color>
<color name="white">#fff</color>
<color name="black">#000</color>
</resources>

res/values/strings.xml
<resources>
<string name="app_name">RecyclerView Tutorial</string>
</resources>

res/values/styles.xml
<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
</resources>


No comments: