I am working with an android tabhost, the problem is , I would like to customized the text and the icon of the tab, but in my attempt the outcome is just one icon and unclickable too. Are there any problem in my code? Thanks for helping
tabHost.setup(ctx, getSupportFragmentManager(), R.id.realtabcontent);
for (String tag : tags) {
if (tag.equals("home")) {
tabIndicator = LayoutInflater.from(ctx).inflate(R.layout.custom_tab, tabHost.getTabWidget(), false);
((TextView) tabIndicator.findViewById(R.id.icon_txt)).setText("news");
((ImageView) tabIndicator.findViewById(R.id.icon)).setImageResource(R.drawable.test_menu);
tabHost.addTab(tabHost.newTabSpec(tag).setIndicator(tabIndicator),Home.class, null);
} else if (tag.equals("news")) {
tabIndicator = LayoutInflater.from(ctx).inflate(R.layout.custom_tab, tabHost.getTabWidget(), false);
((TextView) tabIndicator.findViewById(R.id.icon_txt)).setText("news");
((ImageView) tabIndicator.findViewById(R.id.icon)).setImageResource(R.drawable.test_menu);
tabHost.addTab(tabHost.newTabSpec(tag).setIndicator(tabIndicator),NewsFragment.class, null);
} else if (tag.equals("info")) {
tabIndicator = LayoutInflater.from(ctx).inflate(R.layout.custom_tab, tabHost.getTabWidget(), false);
((TextView) tabIndicator.findViewById(R.id.icon_txt)).setText("news");
((ImageView) tabIndicator.findViewById(R.id.icon)).setImageResource(R.drawable.test_menu);
tabHost.addTab(tabHost.newTabSpec(tag).setIndicator(tabIndicator),InfoFragment.class, null);
} else {
tabIndicator = LayoutInflater.from(ctx).inflate(R.layout.custom_tab, tabHost.getTabWidget(), false);
((TextView) tabIndicator.findViewById(R.id.icon_txt)).setText("news");
((ImageView) tabIndicator.findViewById(R.id.icon)).setImageResource(R.drawable.test_menu);
tabHost.addTab(tabHost.newTabSpec(tag).setIndicator(tabIndicator),PageFragment.class, null);
}
}
tabHost.getTabWidget().setDividerDrawable(null);
And the xml to customize the tab
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:orientation="vertical" >
<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/icon_txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp" />
</LinearLayout>
Please take a look at the demonstrated implementation provided in the following link. I am sure that it will solve your problem :)
http://maxalley.wordpress.com/2012/10/27/android-styling-the-tabs-in-a-tabwidget/
It provides an implementation of customised TabWidget.
I hope this helps.