0

I want to change the background image of tabs when they're selected and unselected. I've followed a few ways to do it from other questions but they didn't seem to work, could you check my code and see what I'm doing wrong? (The question I talked of - How to change the Tabs Images in the TabHost )

Below is the tab in MainActivity

TabSpec specs = th.newTabSpec("Tab1");
    specs.setContent(R.id.tab1);
    specs.setIndicator("Tab 1",
            res.getDrawable(R.drawable.tab1_selector));
    th.addTab(specs);

Below is the xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- When selected, use icon1 -->
<item android:drawable="@drawable/selected"
  android:state_selected="true" />
<!-- When not selected, use icon2-->
<item android:drawable="@drawable/unselected"
  android:state_selected="false" />
</selector>

Thanks, Tom.

Community
  • 1
  • 1
Tssomas
  • 362
  • 1
  • 4
  • 16

1 Answers1

1

Updated answer..You should rather use this method TabSpecs.setIndicator(View view) and provide it a View instance (probably a LinearLayout with TextView in it) and set the drawable as a background of the view.

Create a new XML layout resource containing LinearLayout with one nested TextView as a layout for your tab..Then you should create a function which Inflates that XML to a View instance and sets the TextView content (title of the tab)..then you should call the setIndicator() method with this view..It will effectively replace whole content of your tab with that view.

View v = LayoutInflater.from(this).inflate(R.layout.tab_layout, tabHost.getTabWidget(), false);
TextView label = (TextView) v.findViewById(R.id.textView);
label.setText("Title of your tab");

The method you've used only sets the drawable as an icon (according to docs), so that could be the issue..

Alternatively you can set the theme this way. It should work.

 <style name="MyCustomTheme" parent="@android:style/Theme.Holo.Light">
    <item name="android:tabWidgetStyle">@style/CustomTabWidget</item>
</style>

<style name="CustomTabWidget" parent="@android:style/Widget.TabWidget">
    <item name="android:background">@drawable/your_drawable</item>
</style>

Or you can just try to get direct reference to tabs from your code. Somehow like this.

for(int i=0;i<tabHost.getTabWidget().getChildCount();i++) {
    tabHost.getTabWidget().getChildAt(i).setBackground(R.drawable.your_drawable); 
}
simekadam
  • 7,334
  • 11
  • 56
  • 79
  • It uses tabHost not actionBar for tabs. – Tssomas Feb 09 '14 at 09:28
  • Apprently it was too early:) sorry about that.. I will alter the answer once i will be back at my desk – simekadam Feb 09 '14 at 10:09
  • Sorry, I'm a bit confused.. Could you show an example please? Thank you by the way. – Tssomas Feb 09 '14 at 16:56
  • so I made the answer little bit broader:) you have there three ways which all should work.. – simekadam Feb 09 '14 at 17:17
  • I tried the third method before but it didn't seem to work for some reason, the second method works but looks like this - https://app.box.com/s/80px01bgd5few5ynsafe is it possible to set each tab's icon? – Tssomas Feb 09 '14 at 17:34