Android toolbar example

In this Android tip, I am going to give you an example of using Toolbar in an application. In Android Lollipop, Toolbar is used to replace the ActionBar. However, in older Android versions, it is still possible to get the Toolbar to work using the support library (AppCompat_v7). Unlike ActionBar, Toolbar is part of your layout so that it is easy to customize it. You can change its width, height, background color, etc. The Toolbar can occupy the area of the ActionBar and be moved around on your user interface.
To get the Toolbar to work on your application, the first thing you have to do is adding AppCompat_v7 dependency library to the build.gradel file. If you are using Android Studio 1.0.1, the dependency is added automatically for you.

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


Next, you need to apply a theme that extends the Theme.AppCompat to your application. So, you open the res/values/styles.xml file and create a theme as shown below.

  
<resources>
     <style name="MyTheme" parent=" MyTheme.Base">
     </style>
     <style name=" MyTheme.Base " parent="Theme.AppCompat">
        <item name="android:windowNoTitle">true</item>
        <item name="windowActionBar">false</item>
     </style>
   </resources>


To apply the theme to your application, you open AndroidManifest.xml file and assign the theme to the android:theme attribute of the application as shown below.

android:theme="@style/MyTheme"


Then you open the activity_main.xml file and add a Toolbar to the layout.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   tools:context=".MainActivity"
   android:orientation="vertical"
   >
   <android.support.v7.widget.Toolbar
     android:id="@+id/toolbar"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:minHeight="?attr/actionBarSize"
     android:background="#1565C0"
     />
</LinearLayout>


If you would like to replace the ActionBar by Toolbar, you write the following code to the onCreate() method of your activity. Otherwise, ignore the code.

// Replace action bar with toolbar
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
     if (toolbar != null) {
        setSupportActionBar(toolbar);
     }


toolbar example android studio

Posted by: Dara | post date: 04-21-2015 | Subject: Android Apps Development

write programming tip




This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.