Android – Change Tab bar background image

By -




Hi,

Same as my previous article Android – Change Tab bar background color, if you want to set background image, then we just need to use the setBackgroundResource() method for selected and unselected tab instead of setBackgroundColor() image.

Problem: How do i change Tab Bar background image in android?

Android - Change Tab bar background image

Android - Change Tab bar background image

Solution:
Step 1: Write the below code in onCreate() method to define initial background image for the selected and unselected tab.

  for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
		{
        	tabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.bg_blue_matte);
		}
        tabHost.getTabWidget().setCurrentTab(1);
        tabHost.getTabWidget().getChildAt(1).setBackgroundResource(R.drawable.bg_green_matte);

Step 2: implement the OnTabChangeListener to current activity and then override the onTabChanged() method. In that method, write the below code to define color for selected tab and unselected tab.

@Override
public void onTabChanged(String tabId) {
	// TODO Auto-generated method stub
    for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
    {
    	tabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.bg_blue_matte);
    }
    tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundResource(R.drawable.bg_green_matte);
}

For example:

package com.technotalkative.tabbarexample;

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;

public class TabBarExampleActivity extends TabActivity implements OnTabChangeListener{
    /** Called when the activity is first created. */
	  TabHost tabHost;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        tabHost = getTabHost();
        tabHost.setOnTabChangedListener(this);

        TabHost.TabSpec spec;
        Intent intent;

        // Create an Intent to launch an Activity for the tab (to be reused)
        intent = new Intent().setClass(this, FirstActivity.class);
        spec = tabHost.newTabSpec("First").setIndicator("First")
                      .setContent(intent);
        tabHost.addTab(spec);

        intent = new Intent().setClass(this, SecondActivity.class);
        spec = tabHost.newTabSpec("Second").setIndicator("Second")
                      .setContent(intent);
        tabHost.addTab(spec);

        intent = new Intent().setClass(this, ThirdActivity.class);
        spec = tabHost.newTabSpec("Third").setIndicator("Third")
                      .setContent(intent);
        tabHost.addTab(spec);

        intent = new Intent().setClass(this, FourthActivity.class);
        spec = tabHost.newTabSpec("Fourth").setIndicator("Fourth")
                      .setContent(intent);
        tabHost.addTab(spec);

  for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
		{
        	tabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.bg_blue_matte);
		}
        tabHost.getTabWidget().setCurrentTab(1);
        tabHost.getTabWidget().getChildAt(1).setBackgroundResource(R.drawable.bg_green_matte);

     }

@Override
	public void onTabChanged(String tabId) {
		// TODO Auto-generated method stub
	    for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
		{
	    	tabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.bg_blue_matte);
		}

		tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundResource(R.drawable.bg_green_matte);
	}
}

Used Images:



Paresh Mayani

Paresh Mayani is a lead android developer from India. Currently, He is Sr. Software engineer at InfoStretch Solutions Pvt. Ltd. He has been developing android apps since very 1st version i.e. Android 1.5, he says he has seen days and nights of Android history. He is the Head/Organizer of Google Developers Group (GDG), Ahmedabad

Loading Facebook Comments ...
Loading Disqus Comments ...