Android Toolbar Add SearchView Tutorial

Search feature is a useful tool for the user to search out what they want to see. SearchView is providing in app search for your Android app to improve user experience. Besides, it is widely available to the latest Android N back to Android 2.1. In my tutorial, I will give you SearchView tutorial on how to use it in android application. You just need to follow my steps, no stress no pressure.

Creating a New Project

1. Open Android Studio IDE in your computer.
2. Create a new project and Edit the Application name to “SearchExample”.
(Optional) You can edit the company domain or select the suitable location for current project tutorial. Then click next button to proceed.
3. Select Minimum SDK (API 15:Android 4.0.3 (IceCreamSandwich). I choose the API 15 because many android devices currently are support more than API 15. Click Next button.
4. Choose “Empty Activity” and Click Next button
5. Lastly, press finish button.

Add new dependencies

You need to add 3 dependency for appcompt, design and recyclerview. It will be use those library later.

Change your Theme

Go to styles.xml to change your application theme. The bottom are the sample source code, I change the parent to NoActionBar because I am using toolbar later.

Add a new menu

By right click your res folder and create a new menu folder. After that create a new menu xml name it as “menu_main.xml” to use for the main activity later. The following are the new menu xml source code.

Add a new layout for recyclerview item

Create a new xml layout in the layout folder and name it as “item_row.xml”. After that, edit the xml to the sample source code that i provide in the bottom.

Add a new layout for activity content

After that, add a new layout for your activity main content to display the item. Name the file to “content_main” and Edit the xml file.

Edit activity_main.xml layout

Modify your activity_main.xml layout to the following sample. This layout is display the toolbar and include the layout of content_main.xml.

Add a new Adapter

Right click your java package folder and create a new java class. After that, extends to RecyclerView.Adapter. You can follow sample in the bottom.

Edit MainActivity.java class

Modify your mainactivity.java class and implements the serchview.onquerytextlistener. I add some dummy item to test out the search feature.

Run your project

Lastly, you finish the search feature in this project, now you can test out the search feature in your real device.

(Android Toolbar Add SearchView Tutorial)

Source Code

(Visited 4,701 times, 3 visits today)
Advertisements

Kristofer Ng

A crazy android and web developer. Passion on developing android application and exploring new technology as a hobby.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

GET YOUR EBOOK FOR FREE

Unlock free ebook "Beginning Programming ALL-IN-ONE Desk Reference for DUMMIES" now!

We will send a copy to your email