Home » Android » android – Tab icon not showing

android – Tab icon not showing

Posted by: admin June 15, 2020 Leave a comment

Questions:

I’m trying to do a simple tab app in android with two tabs. My problem is that when I put this code, in the tab, only is shown the text, but no the icons.
If I put the text to “” the icon is shown.

Could someone help me? My android version is 4.0.3.

Thanks a lot.

<?xml version="1.0" encoding="utf-8"?>

<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/tabhost"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent" >
<LinearLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

     <TabWidget android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@android:id/tabs" />

     <FrameLayout android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@android:id/tabcontent" >

        <LinearLayout android:id="@+id/tab1"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >
            <TextView android:id="@+id/textView1"
                android:text="Contenido Tab 1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
    </LinearLayout>

        <LinearLayout android:id="@+id/tab2"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >
            <TextView android:id="@+id/textView2"
                android:text="Contenido Tab 2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
    </LinearLayout>

     </FrameLayout>
</LinearLayout>
</TabHost>

and the activity code is

public class TabTestActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Resources res = getResources();

    TabHost tabs=(TabHost)findViewById(R.id.tabhost);
    tabs.setup();

    TabHost.TabSpec spec=tabs.newTabSpec("mitab1");
    spec.setContent(R.id.tab1);
    spec.setIndicator("sss",
            res.getDrawable(android.R.drawable.ic_btn_speak_now));
    tabs.addTab(spec);

    spec=tabs.newTabSpec("mitab2");
    spec.setContent(R.id.tab2);
    spec.setIndicator("TAB2",
            res.getDrawable(android.R.drawable.ic_dialog_map));
    tabs.addTab(spec);



    tabs.setCurrentTab(0);
}

as you can see is very simple. But when I write spec.setIndicator("",
res.getDrawable(android.R.drawable.ic_dialog_map));

I can see the icon, bu when I write spec.setIndicator("TAB2",
res.getDrawable(android.R.drawable.ic_dialog_map));

I can only see TAB2, but no both of them.

It seems that there are no enougth space to show both. So I’ve tried to get increase the tab height with this

tabs.getTabWidget().getChildAt(1).getLayoutParams().height = 150; 

but not seems to work.

How to&Answers:

//your are over loading the 1st one so you can see only the last added one

 TabHost.TabSpec spec=tabs.newTabSpec("mitab1");

        spec.setIndicator("sss",
                res.getDrawable(android.R.drawable.ic_btn_speak_now));
 Intent sssIntent = new Intent(this, First.class);
 spec.setContent(sssIntent);
        tabs.addTab(spec);

TabHost.TabSpec spec2=tabs.newTabSpec("mitab2");
        spec2=tabs.newTabSpec("mitab2");
        spec2.setIndicator("TAB2",
                res.getDrawable(android.R.drawable.ic_dialog_map));
Intent sssIntent2 = new Intent(this, Second.class);
 spec2.setContent(sssIntent2 );
        tabs.addTab(spec2);

Answer:

I replaced the label name with null value.
Now I can see the icon alone..
Could not find out any other solution.

TabHost.TabSpec spec=tabs.newTabSpec("mitab1");

spec.setIndicator("",
                  res.getDrawable(android.R.drawable.ic_btn_speak_now));
Intent sssIntent = new Intent(this, First.class);
spec.setContent(sssIntent);
tabs.addTab(spec);

Answer:

The visibility of the icon (together with the label) in the tab depend on the target device and the android platform version.

I had a deeper look into this issue and added more details and a solution at your other (quite similiar) question about this problem; It can be found here:

https://stackoverflow.com/a/11379708/414581

Answer:

Adding this in AndroidManifest.xml solved the issue.

<application 
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar">
</application>