Today, I am going to show you how can you implement your searchView widget through recyclerView in android studio.
1) Create your seachView.xml
Here, add your searchView XML code where you want to add your searchView field.
<androidx.appcompat.widget.SearchView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/search_bar"
android:layout_margin="5dp"
android:background="@color/white"
app:queryHint="@string/hint_search"
app:iconifiedByDefault="false"/>
2) Coding Parts
Now, let’s move to the code section. Follow these steps to make your seachView work.
Steps to follow:
2.1) Make your adapter implements Filterable. See an example below:
public class CollectExpandableListAdapter extends BaseExpandableListAdapter implements Filterable {
//......Your all code goes here..
}
2.2) Add the below line
private List<Parent> originalList;
Now, your adapter.java looks like below:
public class CollectExpandableListAdapter extends BaseExpandableListAdapter implements Filterable {
private static final String TAG = "tag";
private final Context _context;
private List<Parent> _listDataHeader; // header title
private List<Parent> originalList;
private final HashMap<Parent, List<Child>> _listDataChild;
//...Rest code...
}
2.3) Add the below code to your Activity class.
SearchView searchView;
// initialize the variable
searchView = view.findViewById(R.id.search_bar);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
Log.d("newText1",query);
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
if ( TextUtils.isEmpty ( newText ) ) {
expListAdapter.getFilter().filter("");
} else {
expListAdapter.getFilter().filter(newText.toString());
}
return true;
}
});
2.4) Finally, write some code in your adapter class. Copy the below code:
public Filter getFilter() {
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
final FilterResults filterResults= new FilterResults();
final List<Parent> results = new ArrayList<Parent>();
if (originalList == null)
originalList = _listDataHeader;
if (constraint != null) {
assert originalList != null;
if (originalList.size() > 0) {
for (final Parent item : originalList) {
if (item.getPostcode().toLowerCase().contains(constraint.toString()) ||
(item.getAddress().toLowerCase().contains(constraint.toString())) ||
(item.getState().toLowerCase().contains(constraint.toString())))
{
results.add(item);
}
}
}
filterResults.values = results;
}
return filterResults;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
_listDataHeader = (ArrayList<Parent>) results.values;
notifyDataSetChanged();
}
};
That’s it. You have successfully created your searchView working. Now check your searchView and see the results.
You may be interested in the following topics:
- How to implement searchView widget in android Studio through Recycleview?
- How to create an expandable listview in Fragments in Android Studio?
- How to create an expandable list view in Android Studio?
- How do I check Intel Virtualization Technology is enabled or not in Windows 10?
- Plugin ‘Android WiFi ADB’ is compatible with IntelliJ IDEA only because it doesn’t define any explicit module dependencies
- Could not install Gradle Distribution from ‘https://services.gradle.org/distributions/gradle-6.5-all.zip’
- How to solve “INSTALL_PARSE_FAILED_NO_CERTIFICATES” error in Android Studio?
- Android Studio Run/Debug configuration error: Module not specified.
- How to search in multiple nodes in Firebase Database?
- How to get the sum of child nodes in Firebase database?
- How to display website in an android app using WebView component?
- Android Layout Using ViewPager and Fragments
- How do I install Android Studio in Windows 10?
- How to display ListView in Fragments using ViewPagers?
- How to create a custom AlertDialog in Android Studio?
- How do I change the name under apps that display in google play store?
- Where does my database store in Android Studio?
- How to add google places autocomplete in Android Edittext?
- How do I convert dp into px in the android studio?
- What are the android screen background sizes?
- What are the sizes of the Android icon located inside the mipmap?
- How do I remember my android app signing key?
- How do I create a Toolbar in Android Studio?
- How to get Android Spinner Dropdown?
- error: package R doesn’t exist in android studio project?
- Firebase Email and Password registration for Android App
- How do I change the company domain name in Android Studio after creating a project?
- How do I make an existing Android Studio Project copy?
- How do I migrate an Android Studio 3.2 existing project to Android X?
- Step by step to insert both banner and interstitial ads in android apps.
- Android TimePicker upon clicking on edittext in Android Studio?
- Migrating to AndroidX Errors:
- How to popup date picker when clicking on edittext in Android Studio?
- AndroidX: ExampleInstrumentedTest.java Class Source code
- How to add user registration data into the Firebase database?
- Cannot find Symbol: ApplicationTestCase
- How do I use Android vector images in Android apps?
- How to create a new Android Virtual Device (AVD) in Android Studio?
- On SDK version 23 and up, your app data will be automatically backed up and restored on app install.
- App is not indexable by Google Search; consider adding atleast one Activity with an Action-View intent-filter.
- How do I style my button in Android app?
- How do I create drawable folder in Android Studio?
- How do I create new classes in Android Studio?
- How to create new android project tutorial?
- How to upgrade Android Studio 2.2.3 to new versions or Android Studio 3.5.3?
- error: Package R does not exist after renamed package name?