HarshitVaish
10/1/2016 - 7:23 AM

Implementing Toolbar as ActionBar

Implementing Toolbar as ActionBar

<resources>

    <!-- Base application theme. -->
    <style name="Base.AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item> <!-- toolbar -->
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <!-- notification/status bar -->
        <item name="colorAccent">@color/colorAccent</item> <!-- Widgets when activated -->
        <item name="textColorPrimary"></item> <!-- Title text -->
        <item name="windowBackground"></item> <!-- Background of app screen -->
        <item name="textColorSecondary"></item> <!-- Navigation arrow/overflow icon -->
        <item name="colorControlHighlight"></item> <!-- Color of ripple animation -->
        <item name="colorButtonNormal"></item> <!-- Button background color -->
        <item name="colorControlActivated"></item> <!-- Widget when selected/activated-->
        <item name="navigationBarColor"></item> <!-- color of navigation bar -->
    </style>

    <style name="AppTheme" parent="Base.AppTheme">

    </style>

    <style name="ToolbarTheme" parent="ThemeOverlay.AppCompat.Light">
        <item name="android:textColorPrimary"></item> <!-- toolbar title -->
        <item name="android:textColorSecondary"></item> <!-- Overflow menu icon and navigation drawer -->
    </style>

    <style name="MenuTheme" parent="ThemeOverlay.AppCompat.Light">
        <item name="android:textColorPrimary"></item> <!-- Text of overflow menu -->
        <item name="android:background"></item> <!-- Background of overflow menu -->
    </style>

</resources>
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="@color/colorPrimary"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?attr/actionBarSize"
    android:elevation="5dp"
    app:theme="@style/ToolbarTheme" 
    app:popupTheme="@style/MenuTheme"
    />
<?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:background="@color/colorPrimary"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?attr/actionBarSize"
    app:theme="@style/ToolbarTheme" 
    app:popupTheme="@style/MenuTheme"
    />
<?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">

    <include layout="@layout/toolbar" android:id="@+id/toolbar" />

</RelativeLayout>
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;

public class MainActivity extends AppCompatActivity {
    
    Toolbar toolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        
        getSupportActionBar().setTitle("Title here");
        toolbar.setSubtitle("Subtitle here");
        
        toolbar.setLogo(android.R.drawable.btn_star_big_on); // Logo Image
        toolbar.setNavigationIcon(android.R.drawable.ic_media_previous); // Navigation icon image

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            toolbar.setElevation(10f);
        }

    }
}