Course Content

Course Content

Android Toolbar Example

Android Toolbar with example

Now we will see the Tool bar of Android practically.

Stepwise :

  1. 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.
  2. 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'.
  3. 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.
  4. 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…
  5. 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.
  6. 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.
  7. 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);
  8. After that we have specified a title of the tool bar, toolbar.setTitle(R.string.toolbarTitle);
  9. After that we have to use a method, setSupportActionBar(toolbar);
  10. Now next we have to set its navigation icon,

toolbar.setNavigationIcon(R.drawable.ic_toolbar_arrow); toolbar.setNavigationOnClickListener(new_View.OnClickListener()

{

@Override

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.

FAQs

Recommended Courses

Share With Friend

Have a friend to whom you would want to share this course?

Download LearnVern App

App Preview Image
App QR Code Image
Code Scan or Download the app
Google Play Store
297K+ Downloads
App Download Section Circle 1
4.5 Rating
App Download Section Circle 2
10K+ Reviews
App Download Section Circle 3
  • Learn anywhere on the go
  • Get regular updates about your enrolled or new courses
  • Share content with your friends
  • Evaluate your progress through practice tests
  • No internet connection needed
  • Enroll for the webinar and join at the time of the webinar from anywhere