The SearchView is part of the Android SDK beginning in Android 3.0 and allows a user to enter a search query and then submit that request to a search provider, it can also show suggestions and results if configured.
This widget can be added to the activity layout or can be integrated into the ActionBar. If placed on the ActionBar, you can define the SearchView to show at all times, only when there is room, or as a collapsible action, which displays the SearchView as an icon initially, then takes up the entire app bar as a search field when the user clicks the icon.
On this example, I will explain how to integrate the SearchView to the ActionBar and submit a query a within the same Activity.
Implementation
The first thing that you need is to create the configuration file for the ActionBar menu item, on the res/menu/ folder of your project.
On menu_main.xml, we define how to create the search item, such as the icon to use and the title of the item. See the file below:
After defining the search item, we now need to inflate the menu and setup the listener and define the logic we will run after the search is submitted.
This is done on the MainActivity, and specifically on the onCreateOptionsMenu() callback:
After inflating the menu and setting up the listener, we need to implement the onQueryTextSubmit() callback, which is the method that is called when the user clicks on the search button of the keyboard.
If you want to collapse the search bar back to the icon, you can clear the search bar and the focus as shown in code above.
The example is very basic, screenshots below:
You can find the full example on the following link:
You can also find more information about the SearchView on the link below:
This widget can be added to the activity layout or can be integrated into the ActionBar. If placed on the ActionBar, you can define the SearchView to show at all times, only when there is room, or as a collapsible action, which displays the SearchView as an icon initially, then takes up the entire app bar as a search field when the user clicks the icon.
On this example, I will explain how to integrate the SearchView to the ActionBar and submit a query a within the same Activity.
Implementation
The first thing that you need is to create the configuration file for the ActionBar menu item, on the res/menu/ folder of your project.
On menu_main.xml, we define how to create the search item, such as the icon to use and the title of the item. See the file below:
After defining the search item, we now need to inflate the menu and setup the listener and define the logic we will run after the search is submitted.
This is done on the MainActivity, and specifically on the onCreateOptionsMenu() callback:
After inflating the menu and setting up the listener, we need to implement the onQueryTextSubmit() callback, which is the method that is called when the user clicks on the search button of the keyboard.
If you want to collapse the search bar back to the icon, you can clear the search bar and the focus as shown in code above.
The example is very basic, screenshots below:
You can find the full example on the following link:
You can also find more information about the SearchView on the link below:




Como se usa en un Fragments?
ReplyDelete