Android Toolbar with example
Now we will see the Tool bar of Android practically.
- First of all we will create a blank project which we will call 'AndroidToolbarExample', now we write a code inside it to create a Tool bar in Android.
- Now here first of all, we have our 'app' folder and than go to 'src' folder and design its layout, for that we will go inside the 'res' folder and design 'activity_main.xml'.
- First of all we will get the linear layout by default, after that we will use <android.support.v7.widget.Toolbar> to design the toolbar. So here we have selected the toolbar using the widget.
- Now we can specify all the properties that we want, just like we specify the property of different views, similarly we have specified the property of the toolbar here like width, height, background etc…
- Now we have to go inside our main file, for that we will first go to the 'main' folder, inside the 'java' folder and see the main activity we have, in which we have created a tool bar by creating an instance.
- Now here we have to create a method using which we can perform any action we want. So for that we will simply generate a Toast notification.
- Now we will create a method which we give the name as 'initToolbar ()'. Now inside this, first we will do the view identification of the tool bar, toolbar=(Toolbar)findViewById(R.id.toolbar);
- After that we have specified a title of the tool bar, toolbar.setTitle(R.string.toolbarTitle);
- After that we have to use a method, setSupportActionBar(toolbar);
- Now next we have to set its navigation icon,
public void onClick(View v)
Toast.makeText(AndroidToolbarExample.this, ""clicking the toolbar"", Toast.LENGTH_SHORT.show();
Now we will execute this tool bar to see how it runs, after running our tool bar it is working properly and the text that we wrote in the toast notification is also displayed correctly.