How to add images to Tabhost

896 views Asked by At

I want to add background images for Tabhost which i already added in selector xml. But not sure how to add resource while initiating Tab. Below is tab :

Code to add : getResources().getDrawable(R.drawable.tabicon)

/* Tabs */
        Bundle bundle = getIntent().getExtras();
        TabHost tabHost = getTabHost();
        TabHost.TabSpec spec;
        Intent intent;

        // First Activity
        intent = new Intent().setClass(this, InfoListView.class);
        spec = tabHost.newTabSpec("some_things").setIndicator("Info").setContent(intent);
        tabHost.addTab(spec);

        // Second Activity
        intent = new Intent().setClass(this, LogListView.class);
        spec = tabHost.newTabSpec("top_things").setIndicator("Sync Log").setContent(intent);
        tabHost.addTab(spec);

        tabHost.getTabWidget().getChildAt(0).getLayoutParams().height = 95;
        tabHost.getTabWidget().getChildAt(1).getLayoutParams().height = 95;
        /* Tabs ends */
3

There are 3 answers

2
Abhishek Shukla On

Create a TextView , set text to that textView, set background drawable to it, and set this textview as indicator

0
Abhishek Shukla On

Somewhat like this:

TextView mTv = new TextView(mContext);
mTv.setText(/*Your-text*/);
mTv.setBackgroundDrawable(mContext.getResources.getDrawable(/*id-of-your-image*/));
spec = tabHost.newTabSpec("top_things").setIndicator(mTv).setContent(intent);
tabHost.addTab(spec);
1
Shakeeb Ayaz On

After your tabhost is intilize with indicator and other things add background to it like this

 tabHost.getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.tabicon); //fro first tab 
 tabHost.getTabWidget().getChildAt(1).setBackgroundResource(R.drawable.tabicon); //for second tab