10

Can we set a background image to tabs like this?

alt text

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
neha
  • 6,327
  • 12
  • 46
  • 78

3 Answers3

18

Following code worked for me:

 tabHost.getTabWidget().setBackgroundResource(R.drawable.tabhost_bg);
neha
  • 6,327
  • 12
  • 46
  • 78
  • 1
    in the picture tabs seems to be transparent... how did you do this? I manage to set background but my tabs are visible on top with grey colour... :( – Farhan Mar 21 '11 at 11:07
  • 2
    You need to set view with transparent background (or w/o any) as an indicator to your tabs to make this solution work. – ernazm Oct 17 '11 at 09:29
10

try this

             getTabHost().addTab(getTabHost().newTabSpec("A")
                    //set the tab name
                    .setIndicator("A")
                    //Add additional flags to the intent (or with existing flags value).
                    .setContent(new Intent(this, A.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));      

            //for creating tab we need to pass tabSpec and tab name to setIndicator and pass intent to its 
                //setContent  to Tab Activity predefined method getTabHost then it will create tab
             getTabHost().addTab(getTabHost().newTabSpec("B")
                    //set the tab name
                        .setIndicator("B")

                         //Add additional flags to the intent (or with existing flags value).
                       .setContent(new Intent(this,B.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)) );

getTabHost().addTab(getTabHost().newTabSpec("C")
                    //set the tab name
                        .setIndicator("C")

                         //Add additional flags to the intent (or with existing flags value).
                       .setContent(new Intent(this,C.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)) );    


             getTabHost().getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.a);
 getTabHost().getTabWidget().getChildAt(1).setBackgroundResource(R.drawable.b);
 getTabHost().getTabWidget().getChildAt(2).setBackgroundResource(R.drawable.c);
Pinki
  • 21,723
  • 16
  • 55
  • 88
1
TabSpec generalTab = mTabHost.newTabSpec("general");
generalTab.setIndicator("General", getResources().getDrawable(android.R.drawable.ic_menu_preferences)).setContent(R.id.tabGeneral);

I have used default android drawable u can use what you want

for setting background of tabhost

this.mTabHost = (TabHost)this.findViewById(R.id.tabHost);
    this.mTabHost.setBackgroundResource(R.drawable.back);
ingsaurabh
  • 15,249
  • 7
  • 52
  • 81
  • This will set image as background to individual tabs. I want to set background image to background of all the tabs [part below the tabs]. – neha Jan 21 '11 at 07:46
  • This' setting the background image to entire screen and not just background of tabs. – neha Jan 21 '11 at 08:11
  • actually what you wanted is done in this way only it will set the image to whole background but when you put different images in different tabs then this image is hided by tabs image this is the only workaround I know hope any other can have better answer for this – ingsaurabh Jan 21 '11 at 08:19