Sunday, May 17, 2015

How to use Toolbar in Android

Table of contents
  1. Android Toolbar Example
    • Toolbar.inflateMenu()
    • Toolbar.setOnMenuItemClickListener()
    • onMenuItemClick()
    • switch case statement
    • Toolbar.setTitle()
    • Toolbar.setBackgroundColor()
    • ImageView.setImageResource()

1. Android Toolbar Example

activity_main.xml

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rl"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    tools:context=".MainActivity"
    android:background="#ffffff"
    >
    <Toolbar
        android:id="@+id/tb"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />
    <ImageView
        android:id="@+id/iv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/tb"
        />
</RelativeLayout>
res/menu/toolbar_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu
    xmlns:android="http://schemas.android.com/apk/res/android"
    >
    <item
        android:id="@+id/action_elephant"
        android:title="Elephant"
        android:showAsAction="ifRoom"
        />
    <item
        android:id="@+id/action_dog"
        android:title="Dog"
        android:showAsAction="ifRoom"
        />
    <item
        android:id="@+id/action_cat"
        android:title="Cat"
        android:showAsAction="ifRoom"
        />
    <item
        android:id="@+id/action_giraffe"
        android:title="Giraffe"
        android:showAsAction="ifRoom"
        />
</menu>
MainActivity.java

package com.cfsuman.me.androidcodesnippets;

/*
    compileSdkVersion 22
    minSdkVersion 21
    targetSdkVersion 22
 */

import android.graphics.Color;
import android.os.Bundle;
import android.app.Activity;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.Toolbar;

public class MainActivity extends Activity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //Get widgets reference from XML layout
        Toolbar tb = (Toolbar) findViewById(R.id.tb);
        final ImageView iv = (ImageView) findViewById(R.id.iv);

        //Set a title for Toolbar
        tb.setTitle("App Toolbar...");

        //Set the Toolbar background color
        tb.setBackgroundColor(Color.parseColor("#FF64FFB7"));

        //Inflate the Toolbar with a menu
        tb.inflateMenu(R.menu.toolbar_menu);

        //Define the toolbar menu item click listener
        tb.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                switch (item.getItemId()) {
                    //Change the ImageView image source depends on menu item click
                    case R.id.action_elephant:
                        iv.setImageResource(R.drawable.elephant);
                        return true;
                    case R.id.action_dog:
                        iv.setImageResource(R.drawable.dog);
                        return true;
                    case R.id.action_cat:
                        iv.setImageResource(R.drawable.cat);
                        return true;
                    case R.id.action_giraffe:
                        iv.setImageResource(R.drawable.giraffe);
                        return true;
                }
                //If above criteria does not meet then default is false;
                return false;
            }
        });
    }
}
More android examples