how to add icon and Image in Tabhost android?
11,151
Create a selector xml store it in drawable folder and try this code :
TabHost tabHost = getTabHost();
TabHost.TabSpec spec;
Intent intent;
intent = new Intent().setClass(this,YourClass.class);
spec = tabHost.newTabSpec("tab_name").setIndicator("Tab Text", getResources().getDrawable(R.drawable.tab_selector)).setContent(intent);
tabHost.addTab(spec);
Xml for selector name it tab_selector:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true"
android:state_pressed="false"
android:drawable="@drawable/ic_tab_selected" />
<item android:drawable="@drawable/ic_tab_unselected" />
</selector>
ic_tab_selected and ic_tab_unselected these are the icon image which will be shown on selecting and deselecting the tab
Comments
-
Santosh Bhandary over 1 year
I am following this tutorial to add icon along with text using Tabhost.
I am writting following codes to add text and icon in tab .
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TabHost tabHost=(TabHost)findViewById(R.id.tabHost); tabHost.setup(); TabSpec spec1=tabHost.newTabSpec("Tab 1"); spec1.setContent(R.id.tab1); spec1.setIndicator("Tab 1",getResources().getDrawable(R.drawable.home)); TabSpec spec2=tabHost.newTabSpec("Tab 2"); spec1.setContent(R.id.tab2); spec2.setIndicator("Tab 2",getResources().getDrawable(R.drawable.home)); TabSpec spec3=tabHost.newTabSpec("Tab 3"); spec3.setIndicator("Tab 3",getResources().getDrawable(R.drawable.home)); spec3.setContent(R.id.tab3); tabHost.addTab(spec1); tabHost.addTab(spec2); tabHost.addTab(spec3); }
It works fine with adding text only in tab but throws following error message
java.lang.IllegalArgumentException: you must specify a way to create the tab content at android.widget.TabHost.addTab
need some help here please...
-
Santosh Bhandary almost 9 yearswhat is the purpose of Intent().setClass(this,YourClass.class); @Clairvoyant ....its throwing IllegalStateException. in my case