Thursday , September 16 2021

Using Toolbar in Your Application – Android Coding by DMTechnolab

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 toolbarandroid 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.xmlchanging 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.

toolbartoolbar

Setting Title for the Toolbar

  • To do this come inside MainActivity.java.
  • Now, execute your application and you will see the following output.

toolbartoolbar

  • 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 directorycreating 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 filemenu 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.

  • 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 toolbarmenu 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 exampleandroid 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.

  • 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. 

  • 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 exampleandroid 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 🙂

belal khan profilebelal khan profile

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.


Source link

About dmtechnolab

Check Also

Simplified Coding

Android ViewModel Unit Test Tutorial – Android Coding by DMTechnolab

Hi guys welcome Android ViewModel Unit Test Tutorial. This post is also part of our …

Leave a Reply

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