Showing posts with label ActionBar. Show all posts
Showing posts with label ActionBar. Show all posts

Monday, April 13, 2015

Set icon and background of ActionBar

Android programming example to set icon and background of ActionBar. (Assume your activity extends android.support.v7.app.ActionBarActivity, with android.support.v7.app.ActionBar)




package com.example.androidactionbartitle;

import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.content.res.Resources;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;

public class MainActivity extends ActionBarActivity {
 
 Button btn1, btn2, btn3, btn4, btn5;;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        btn1 = (Button)findViewById(R.id.button1);
        btn2 = (Button)findViewById(R.id.button2);
        btn3 = (Button)findViewById(R.id.button3);
        btn4 = (Button)findViewById(R.id.button4);
        btn5 = (Button)findViewById(R.id.button5);

        btn1.setOnClickListener(new OnClickListener(){

   @Override
   public void onClick(View v) {
    ActionBar actionBar = getSupportActionBar();
    
    Drawable drawable;
    Resources resources = getResources();
    
    //deprecated in API level 22
    //drawable = resources.getDrawable(R.drawable.ic_launcher);
    
    //API Level 21
    drawable = resources.getDrawable(R.drawable.ic_launcher, null);
    
    actionBar.setBackgroundDrawable(drawable);
   }});
        
        btn2.setOnClickListener(new OnClickListener(){

   @Override
   public void onClick(View v) {
    ActionBar actionBar = getSupportActionBar();
    
    Drawable drawable;
    drawable = new ColorDrawable(0xA0500050);
    
    actionBar.setBackgroundDrawable(drawable);
   }});
        
        btn3.setOnClickListener(new OnClickListener(){

   @Override
   public void onClick(View v) {
    ActionBar actionBar = getSupportActionBar();
    actionBar.setBackgroundDrawable(null);
   }});
        
        btn4.setOnClickListener(new OnClickListener(){

   @Override
   public void onClick(View v) {
    ActionBar actionBar = getSupportActionBar();
    
    Drawable drawable;
    Resources resources = getResources();
    
    //deprecated in API level 22
    //drawable = resources.getDrawable(R.drawable.ic_launcher);
    
    //API Level 21
    drawable = resources.getDrawable(R.drawable.ic_launcher, null);
    
    actionBar.setIcon(drawable);
    actionBar.setDisplayShowHomeEnabled(true);
   }});
        
        btn5.setOnClickListener(new OnClickListener(){

   @Override
   public void onClick(View v) {
    //enable display HOME, but with icon of null
    ActionBar actionBar = getSupportActionBar();
    actionBar.setIcon(null);
    actionBar.setDisplayShowHomeEnabled(true);
   }});
    }

}

<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.androidactionbartitle.MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:autoLink="web"
        android:text="http://android-er.blogspot.com/"
        android:textStyle="bold" />
    
    <Button 
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button 1 (background - drawable icon)"/>
    
    <Button 
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button 2 (background - color)"/>
    
    <Button 
        android:id="@+id/button3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button 3 (background - null)"/>
    
    <Button 
        android:id="@+id/button4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button 4 (set icon)"/>
    
    <Button 
        android:id="@+id/button5"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button 5 (set icon null)"/>

</LinearLayout>