In this blog, we are going to see how to use a custom toolbar in the android app. Before going to implement it first let’s discuss what Toolbar is.
public class Toolbar
A toolbar is used within the application. The toolbar is a generalization of Action Bar for use inside the application. An app may prefer to select a Toolbar as an Action bar for an activity using the setActionBar() method. The Toolbar supports more features than an Action bar. We can use texts and images on Toolbar
First, we are going to create a new android project, if you don’t know how to create a new project in the android studio then see the previous blog about how to create a new android project. Then open the style.xml file inside res > values > styles.xml. Change the App Theme to NoActionBar, (Fig 1) so that we are going to create a custom Toolbar as we want.
Then go our XML file where we are going to create our custom Toolbar. Change the parent layout to a Relative Layout. In the Android Tutorial section of my blog, we will discuss the various layout of android. After the parent layout, we type androidx.appcompat.widget.Toolbar and set layout width as match_parent and layout heigh as actionBarSize. As we already mentioned that Toolbar is a generalization of Action Bar, so we set the height of our Toolbar to the same height of ActionBar. And set the theme to android:theme=”@style/ThemeOverlay.AppCompat.DayNight.ActionBar”. We can also change the background of the Toolbar simply write android:background=” whatever you want to add”. And give an ID to the Toolbar (Fig 2).
preview of above code:
Click here for Add Image and Text in Custom Toolbar.