Android Toolbar Example: How To Role Toolbar Every Mo Actionbar

Posted on 44 views

Android introduced Toolbar amongst the free of novel operating organization Android Lollipop (API 21). Toolbar has replaced the Android Action Bar in addition to nosotros tin role it anywhere inwards our application. Toolbar was introduced inwards android API 21 (Lollipop 5.0) even in addition to hence nosotros volition hold upwards able to role this inwards devices below API 21 also past times using appcompact back upwards library.



Android introduced Toolbar amongst the free of novel operating organization Android Lollipop  Android Toolbar Example: How to Use Toolbar every bit ActionBar

Related:
Android Action Bar Tutorial in addition to Example With Option Menu
Android Menus: Best Practice in addition to Awesome Android Menu Libraries


Why Toolbar instead of ActionBar?

Toolbar provides us lots of attributes in addition to nosotros tin customize it inwards our ain agency than ActionBar. Now nosotros tin sentiment multiple toolbars within the screen.


Welcome Toolbar, Goodbye Action Bar

Just follow the next steps to use Toolbar instead Action Bar.

Setup Appcompact Support Library to role Toolbar every bit ActionBar

 You convey to add together appcompact back upwards library every bit a dependency inwards your build.gradle file earlier jumping  to our coding part.  Just add together compile ‘com.android.support:appcompat-v7:22.2.0’ in your build.gradle file within dependencies.

dependencies {     compile fileTree(dir: 'libs', include: ['*.jar'])     compile 'com.android.support:appcompat-v7:22.2.0' }  


Change Your App Theme

To use Toolbar every bit ActionBar you convey to role Theme.AppCompact theme, simply use Theme.AppCompact.NoActionBar as bring upwards theme.

Your modified code of styles.xml file volition hold off similar this:

<resources>      <!-- Base application theme. -->     <style name="AppTheme" parent="Theme.AppCompat.NoActionBar">         <!-- Customize your topic here. -->     </style> </resources>  

  
Your AndroidManifest.xml file volition hold off similar this:

Adding Toolbar to your Activity Layout

Now you lot necessitate to add together Toolbar to your XML layout. Use the code given below to role Toolbar every bit Actionbar.


Finally, nosotros convey to add together ii lines of code inwards our coffee Activity.

Toolbar toolbar=(Toolbar)findViewById(R.id.toolbar);         setSupportActionBar(toolbar); 


Modified code of MainActivity.java file volition hold off similar this:

That’s all, straightaway piece of job your Toolbar Example application.

READ  Turn On Together With Off Wifi Connecter Programmatically Inwards Android