ToolBar

ToolBar is complete replacement of Android ActionBar. It provides greater controls to user to customize its appearance unlike Old ActionBar. Its introduced in Android Lollipop, API 21, hence its available to use in Support Design Library

I encourage you guys to hiding ActionBar and switch to ToolBar from now since new support design library with wonderful features designed to work together with ToolBar not ActionBar. Let's do that. First Create New Project

Its easy to switch to ToolBar. Just hide ActionBar from style.xml
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
</style>
Now run your project. You see below output without ActionBar:
In below image, you can see some Material theme attributes for colors. You can choose best theme for your app depends on that.
From Image, you can choose best theme for your app. Here I also refer to these links to choose some awesome and material based colors.
www.materialpalette.com
www.materialui.co
coolors.co
Let's start to create ToolBar.
Create one appbar.xml layout in res->layout folder like these:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary"
    android:minHeight="?attr/actionBarSize"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />
Open your layout main file, activity_main.xml and adding appbar layut using include.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <include layout="@layout/appbar"></include>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:text="Hello World" />
    </LinearLayout>
</RelativeLayout>
We added appbar in layout, now we have to define in our MainActivity java file. Import toolbar package
import android.support.v7.widget.Toolbar;
private Toolbar toolbar;
toolbar = (Toolbar) findViewById(R.id.toolbar);
if(toolbar!=null){
 toolbar.setTitle("My Application");
 setSupportActionBar(toolbar);
}
Now run project, you see output same as you created project first time:
That's it. You have done to use new ToolBar feature. You can put your Toolbar at any direction, by set gravity to you include layout like center, bottom, etc. Have a great coding ahead :)
Next, I'll teach to use of Andriod's New NavigationView

Comments

Popular posts from this blog

SQLiteDatabase (Without SqliteOpenHelper class)

Set current date to Button and Datepicker dialog

MVC Architecture in Android