Android Toolbar Example: Using Toolbar in Your Application

In this android toolbar example, I will be explaining to you how to use Toolbar in your application. So, if you are confused about using the toolbar in your android application, then this is the right …

android toolbar example

In this android toolbar example, I will be explaining to you how to use Toolbar in your application. So, if you are confused about using the toolbar in your android application, then this is the right place for you.

What is Toolbar in Android?

The toolbar was introduced from Android Lollipop when Material Design came into existence. Before Toolbar we were using ActionBar, the position of ActionBar was fix at the top of the screen. But in case of Toolbar, it is more flexible; you can place Toolbar anywhere in the Activity.  The toolbar is useful for displaying App Icon, Title, Navigation Menu, etc.

You can see an example of the toolbar in below image.

android toolbar
Android Toolbar Example

Android Toolbar Example

To learn to implement Toolbar, we need an Android Project. And for this example, let’s create a new one.

Assuming you also have a new Android Project, here I have a project created using Android Studio 2.3. And you don’t need to add additional libraries if you are using version ahead 2.3.

Changing Theme

When you create a new project in Android Studio, it has the old Actionbar. And to use Toolbar first, we need to remove the Actionbar, to do this we have to change our theme.

  • To change the theme go inside res->values->styles.xml. Here you will see the following.

changing theme in styles.xml

  • So we need to modify the above XML thing as below.

  • So we have changed the theme to NoActionBar. Now we can create a toolbar.

Creating Toolbar Layout

  • Now come inside activity_main.xml and create toolbar as shown below.

  • The above XML will generate a toolbar in your activity.

toolbar

Setting Title for the Toolbar

  • To do this come inside MainActivity.java.

  • Now, execute your application and you will see the following output.

toolbar

Setting up a Menu on the Toolbar

Creating a Menu

  • Many times you see a menu on the right of the toolbar. Now let’s see how you can create this.
  • First create a folder named menu inside res.

creating menu directory

  • Inside the menu directory, you need to create a new menu resource file. I have created a file named menu.xml.

menu resource file

  • Inside the menu.xml file write the following code.

  • Here, we have only three menu items but if you need you can add as many menu items as you want.

Adding Menu to Toolbar

  • We will do it inside MainActivity.java. Just add the following method inside MainActivity class.

  • Now you will see the menu in your application.

menu in toolbar

Handling Menu Clicks

  • Now we also need to handle the clicks on Menu Items. To do this we just need to override onOptionsItemSelected() method.

  • After adding the above code, you will see the toast on clicking Menu Item.
android toolbar example
Android Toolbar Example

Setting up a Search on the Toolbar

Many times you see a search icon that we can use for searching something in the app. The icon is nothing but the menu. So let’s look at how we can set up a Search option on the Toolbar.

Adding a Search Icon

  • First, you need to add a search icon to your drawable folder. You can download any magnifier icon from google images or any other website for this. After downloading the image rename it to icon_search or any name you want, but you can only use small letters with underscores.

Adding Search Menu

  • Now come inside the file menu.xml that we created above. Here you need to add one more item to the starting of your search.  So modify the code as below. Remember I have added one more namespace here.

  • Now the important things in the above code is  the actionViewClass=”android.support.v7.widget.SearchView”. This means we are using the SearchView. The next thing is showAsAction=”always” and it will make the search icon visible always. 

Modifying onCreateOptionsMenu()

  • Now come inside your MainActivity.java and modify the method onCreateOptionsMenu() as shown below.

  • After doing the above changes you can run your application and you will see a nice search option in your Toolbar.
android toolbar example
Android Toolbar Example

So that’s all for this Android Toolbar Example friends. We learned some fundamental things, but you can do whatever you want with the Toolbar.
If you have any confusions regarding this Android Toolbar Example, just leave your comments below. And if you found it helpful then, please SHARE. Thank You 🙂

Hi, my name is Belal Khan and I am a Google Developers Expert (GDE) for Android. The passion of teaching made me create this blog. If you are an Android Developer, or you are learning about Android Development, then I can help you a lot with Simplified Coding.

Expand Your Knowledge: Next Tutorial Picks

0 0 votes
Article Rating
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x