1

my tab bar icon wont show up

TabHost tabHost = getTabHost();

TabSpec barcodeInsertSpec = tabHost.newTabSpec("Barcode Insert");
barcodeInsertSpec.setIndicator("Barcode Insert", getResources().getDrawable(R.drawable.home2));
barcodeInsertSpec.setContent(new Intent(getBaseContext(), BarcodeInsertActivity.class));

tabHost.addTab(barcodeInsertSpec);

drawable/home2.xml

<?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <!-- When not selected, use that-->
        <item android:drawable="@drawable/home22" />
    </selector>

and put picture in three folders in different sizes (48x48, 32x32, 24x24) drawable-hdpi , drawable-ldpi, ... as .png

like drawable-hdpi/home22.png

5 Answers5

1

try this code

<selector xmlns:android="http://schemas.android.com/apk/res/android" >
     <item  android:state_focused="true"
            android:state_pressed="true"
            android:drawable="@drawable/button0_click"></item>
    <item   android:state_focused="true"
            android:state_pressed="false"
            android:drawable="@drawable/button0_click"></item>
    <item
            android:state_focused="false"
            android:state_pressed="true"
            android:drawable="@drawable/button0_click" />
     <item 
            android:drawable="@drawable/button0"></item>

</selector>
ShreeshaDas
  • 2,032
  • 2
  • 15
  • 34
0

This code is work for me :)

Resources res=getResources();
TabHost Tabs = (TabHost) findViewById(R.id.your_id);
Tabs.setup();
TabHost.TabSpec spec;
spec = Tabs.newTabSpec("tag1");
spec.setContent(R.id.tab1);
spec.setIndicator("Home", res.getDrawable(R.drawable.home_icon));
Tabs.addTab(spec);
No_Rulz
  • 2,626
  • 1
  • 15
  • 32
0

Hi Use the drawable selector file like this.

<?xml version="1.0" encoding="utf-8"?>
<selector
  xmlns:android="http://schemas.android.com/apk/res/android">
  <item
    android:state_selected="true"
    android:drawable="@drawable/select" />
  <item
    android:state_selected="false"
    android:drawable="@drawable/deselct" />
</selector>
itsrajesh4uguys
  • 4,420
  • 3
  • 18
  • 31
0

I had the same problem using Xamarin.

This was a hack solution that shows the icon but NOT the text (looks like an android defect):

This solution shows both, but it hosed my app-wide styles, which indicates to me that I could probably set up a style override:

EDIT 10 OCT 2013 - I got it to work in c#! It should be straightforward to do the same in java

I was able to get tabs to show up with icons with code and a custom style. See below

tab_indicator_holo.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="@dimen/tab_host_default_height"
android:orientation="horizontal"
style="@style/TabBlood"
android:gravity="center">

<ImageView
    android:id="@android:id/icon"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:visibility="visible"
    android:contentDescription="@null" />

<TextView
    android:id="@android:id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    style="@style/TabTextBlood" />

</LinearLayout>

My Activity OnCreate:

...
var spec = this.TabHost.NewTabSpec(tag);
var drawableIcon = Resources.GetDrawable(drawableId);
View tabIndicator = LayoutInflater.From(this).Inflate(Resource.Layout.tab_indicator_holo, this.TabHost.TabWidget, false);
TextView title = tabIndicator.FindViewById<TextView>(Android.Resource.Id.Title);
ImageView img = tabIndicator.FindViewById<ImageView>(Android.Resource.Id.Icon);
title.Text = label;
img.SetImageDrawable(drawableIcon);
spec.SetIndicator(tabIndicator);
spec.SetContent(intent);
this.TabHost.AddTab(spec);
Community
  • 1
  • 1
0

Try This.

  TabHost th = getTabHost();
  TabHost.TabSpec sp;
  sp=th.newTabSpec("tab 1");
  sp.setIndicator("Home");
  sp.setContent(new Intent(this, Welcome.class));
  th.addTab(sp);
  tabhost.getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.YOURIMAGE);

*You can also use a custom item selector in place of drawable to make a focused and pressed effects. Cheers!

Ali
  • 817
  • 1
  • 10
  • 22
  • Hey, this may be a great solution - but could you give a bit of explanation about which bits you changed and why? don't forget - lots of newbies come to Stack overflow too, and they might not notice what is (for you) an obvious change. – Taryn East Jul 07 '14 at 06:13