How to work with searchview on Android systems and perform search operations
5 minute(s) read | Published on: May 03, 2021 Updated on: Dec 14, 2021 |
It should be noted that the search process can be different in different sections. In this case, the search results can be displayed to the user from the server, application, system, device, etc. Therefore, the user can easily perform the search operation according to his needs and receive the related results. In this part of the tutorial, we want to talk about search view android and search operations.
What he learns in this section is as follows:
- What is searchview android ?
- How to build and run a searchview android project in Android Studio - How to add a searchview to the toolbar
- searchview in action bar android example
What is Searchview?
searchview android is another widget that exists in Android, and the user can use it to search for one or more phrases and view the results there. searchview android is a bar used to perform the search process, in which the user searches for the words or phrases he wants there and sees the results.
1- I create a project in Android Studio and name it SearchView as desired.
2- The type of activity used in this training is Empty Activity.
3- It should be noted that the language required for this project is Java.
4- In the activity layout, we add a ListView and SearchView.
5- The codes that should be in the activity_main.xml section are as follows:
In the code above, the query hint feature is used to add help, and the iconifiedByDefault feature displays search options. A false value is set for the second attribute, which ultimately causes the widget not to be an icon but also to have a search box. Note that if we set the value of the second attribute to true, it will cause the search box not to be displayed and only a magnifying glass icon to be displayed.
6- Define two widgets inactivity and create a list that includes the names of several cities.
7- The codes that should be in the MainActivity.java section are as follows:
By inserting the above code, we have created a list containing the city names' values, which is set on ListView using setAdapter ().
In the next steps, we need to create a Listener for SearchView. Creating a listener enables the user to check the input and provide the appropriate and related result.
8- To do this, use the setOnQueryTextListener method.
9- I define the method inside the onCreate () activity. Like the following:
It should be noted that this method itself has two methods, onQueryTextSubmit () and onQueryTextChange (), which Android Studio automatically adds.
Both methods are boolean. In other words, the values that can be placed in these methods are false or true.
10- We complete the methods as follows:
By entering the above code, the characters entered by the user are stored in a String named s.
In the code above, we have defined a condition for onQueryTextSubmit to check whether the value entered by the user stored in s is in the list.
In other words, the strings stored in s are set by the adapter in listView.
Get Filter () also filters the options in the list and removes the rest of the items in the list. However, if the characters entered by the user do not match the items in the list, a toast will be displayed indicating that the item was not found.
But it should be noted that in onQueryTextChange, we have defined a condition that checks the user input characters and adds them to the list if it is not in the list. This means that if the item that the user is looking for is not in the list, it should be added to the list:
11- The complete code that should be included in MainActivity.java is as follows:
12- We will implement the project.
13- Enter the letter "I" to test. We see that it displays the corresponding value.
Add SearchView to the toolbar.
In this part of the tutorial, we want to enter SearchView into the toolbar and display it there.
1- To do this, we must first create a menu directory.
2- Then add the XML file to it. It should be noted that the name of this file has been chosen arbitrarily in this tutorial, toolbar_searchview.
3- In the menu, we define an item like the following.
4- The code that should be in the toolbar_searchview.xml file is as follows:
In the codes listed above, we add a magnifying glass icon to the drawable folder of the project by default from the icons of Android Studio
Assets Studio.
5- In the next step, after the onCreate () method and in the Activity class, I add the onCreateOptionsMenu () method. Like the following:
6- Then, we complete the method as follows:
After inflating the menu, we created a SearchView with the desired toolbar_searchview name linked to the menu item.
Then we use the setQueryHint and setIconifiedByDefault methods. The first method adds a Hint, and the second method is true, only the magnifying glass icon is displayed, and the search box is not displayed.
7- Then, we run the project.
8- We will see that an icon has been added to the toolbar, which by clicking on it, the search box will be added, and you can easily use it to perform search operations.
9- Note that if we set setIconifiedByDefault to false, the search box will be displayed by default.
10- In this part of the simple mode tutorial, we reviewed and created a searchview android example.
11- The complete and final code that should be in the MainActivity.java section are as follows:
In this tutorial, you learned how to check and create a searchview android
example, and you were able to make settings related to it.
About our Android and iOS app development services