Skip to main content

How to use ActionBar in Android

Table of contents
  1. Android ActionBar Example
    • getActionBar()
    • ActionBar.setTitle()
    • ActionBar.setBackgroundDrawable [background color]
    • Menu Resource
    • onCreateOptionsMenu()
    • onOptionsItemSelected()
    • Menu Item Icon [ActionBar Icon]

1. Android ActionBar 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"
    >
    <ImageView
        android:id="@+id/iv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
</RelativeLayout>
res/menu/toolbar_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu
    xmlns:android="http://schemas.android.com/apk/res/android"
    >
    <!--
        Icon Size
        HDPI - 48*48
        MDPI - 32*32
        XHDPI - 64*64
        XXHDPI - 92*92
    -->
    <item
        android:id="@+id/action_elephant"
        android:title="Elephant"
        android:icon="@drawable/elephant"
        android:showAsAction="always|withText"
        />
    <item
        android:id="@+id/action_dog"
        android:title="Dog"
        android:icon="@drawable/dog"
        android:showAsAction="ifRoom|withText"
        />
    <item
        android:id="@+id/action_cat"
        android:title="Cat"
        android:icon="@drawable/cat"
        android:showAsAction="ifRoom"
        />
    <item
        android:id="@+id/action_giraffe"
        android:title="Giraffe"
        android:icon="@drawable/giraffe"
        android:showAsAction="ifRoom"
        />
</menu>
MainActivity.java

package com.cfsuman.me.androidcodesnippets;

/*
    compileSdkVersion 22
    minSdkVersion 21
    targetSdkVersion 22
 */

import android.app.ActionBar;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.app.Activity;
import android.view.MenuItem;
import android.widget.ImageView;
import android.view.Menu;
import android.view.MenuInflater;

public class MainActivity extends Activity{

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

        // Get the app ActionBar
        ActionBar ab = getActionBar();

        //Set the title of Actionbar
        ab.setTitle("Action Bar....");

        // Set action bar background color as drawable
        ab.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#FFFF5A47")));
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu){
        // Inflate the menu item for use in the action bar
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.toolbar_menu, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item){
        // Get ImageView reference from XML layout
        ImageView iv = (ImageView) findViewById(R.id.iv);
        // Handle presses on the action bar items
        switch (item.getItemId()) {
            // Change the ImageView image source depends on menu item click
            case R.id.action_elephant:
                iv.setImageResource(R.drawable.elephant_big);
                return true;
            case R.id.action_dog:
                iv.setImageResource(R.drawable.dog_big);
                return true;
            case R.id.action_cat:
                iv.setImageResource(R.drawable.cat_big);
                return true;
            case R.id.action_giraffe:
                iv.setImageResource(R.drawable.giraffe_big);
                return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
More android examples

Popular posts from this blog

How to use NumberPicker in Android

Table of contentsNumberPicker ExamplesetOnValueChangedListeneronValueChange()setWrapSelectorWheel()setMinValue()setMinValue()setMaxValue()NumberPicker with String ArraysetDisplayedValues()String Array1. NumberPicker 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" > <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="25dp" android:text="Select a number..." /> <NumberPicker android:id="@+id/np" …

TextView new line (multiline) in android

TextView new line (multiline) TextView display text on android app. by default, TextView show text on a single line, and if it is long then TextView take more lines to display its text. android developers can generate a new line on TextView both programmatically and syntactically. android developers can make a multiline TextView without splitting text to multiline by android:minLines attribute.

the following android example code demonstrate us how can we syntactically create a new line on TextView widget by xml layout file and string resource file.

the simplest way to create a new line on TextView is android:text attribute. android:text attribute allow us to display text on android app. we can add a simple '\n' to TextView text where we want to start a new line. in this way we can create a multiline TextView widget in android app. we can assign android:text attribute value by this way android:text="Line1 \n Line2 \n Line3" for a TextView widget. this value will…

How to change TextView font size in android

TextView font size TextView widget display text on android application. we can set or change TextView font size statically by declarative syntax in xml layout file or programmatically at run time in java file. even we can use an xml file source to define font size.

the following example code demonstrate us how can we define TextView font size in xml layout file and how can we uses dimens.xml to reference font size. in this example we did not changes any coding in java file, so here we only include the layout xml file and dimens.xml file. activity_main.xml <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" android:orientation="vertical" android:layout_margin="25dp" tools:context=".MainActivity" > <TextView android:id=&…