How to Change the Tabs Images in the Tabhost

How to change the Tabs Images in the TabHost

If you wish to use different images for the selected and unselected states, then create 'selector' XML files in your drawables folder for each tab, e.g. tab1_selector.xml, tab2_selector.xml which should contain the following, replacing the drawable references to your images for selected and unselected states. i.e.

    <?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/tab1_selected_image" />
<item
android:state_selected="false"
android:drawable="@drawable/tab2_unselected_image" />
</selector>

Then using the .setIndicator method as bharath wrote above you should reference your new xml drawable resource.

How to add images as a tab indicator in android tab host?

Add images which you want to use in the tabs in the draw-able folder and get through this way.

tab1.setIndicator("",getResources().getDrawable(R.drawable.tab1));
tab2.setIndicator("",getResources().getDrawable(R.drawable.tab2));
tab3.setIndicator("",getResources().getDrawable(R.drawable.tab3));

How to set images in tabhost using fragment

spec = tabHost.newTabSpec("First").setIndicator("name",getResources().getDrawable(R.drawable.port))
.setContent(intent);

Try it.

How to change tabs images ( not in pressed )?

One way would be to remove all the views from tabhost first:

mainTabs =  ((TabActivityMy) getParent()).getTabHost(); 
mainTabs.removeAllViews()

mainTabs.getTabWidget().setDividerDrawable(null);
Intent intent = new Intent(this, c);
TabHost.TabSpec spec = mainTabs.newTabSpec(null);
View tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_indicator, ((TabActivityMy) getParent()).getTabHost().getTabWidget(), false);
ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);
icon.setImageResource(drawableId);
spec.setIndicator(tabIndicator);
spec.setContent(intent);
mainTabs.addTab(spec);

Try removing allviews from TabWidget:

mainTabs.getTabWidget().removeAllViews();

There is function:

mainTabs.clearAllTabs();

This should work in your case

How to set image in tabhost in android?

change this:

for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name)
.setTabListener(this));
}

to this:

for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText("")
.setTabListener(this).setIcon(R.drawable.YOUR_ICON));
}

Add Image or style to each tab

Try this :
Call this from your OnCreate() method:

 setTabs();

Then put this code

 private void setTabs()
{
addTab(R.drawable.ic_icon1, Activity.class, "All");
addTab(R.drawable.ic_icon2, Activity1.class, "Favorite");
}

private void addTab(int drawableId, Class<?> c, String labelId)
{
final TabHost tabHost = getTabHost();
Intent intent = new Intent(this, c);
TabHost.TabSpec spec = tabHost.newTabSpec("tab"+ labelId);

View tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_indicator, getTabWidget(), false);
TextView title = (TextView) tabIndicator.findViewById(R.id.title);
title.setText(labelId);

ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);
icon.setImageResource(drawableId);
spec.setIndicator(tabIndicator);
spec.setContent(intent);
tabHost.addTab(spec);

}

Then Create tab_indicator.xml in drawable folder and put this code.There you can set different colours when it is pressed,focused etc...

 <?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Non focused states -->
<item android:state_focused="false" android:state_selected="false"
android:state_pressed="false" android:drawable="@drawable/tab_unselected" />
<item android:state_focused="false" android:state_selected="true"
android:state_pressed="false" android:drawable="@drawable/tab_selected" />

<!-- Focused states -->
<item android:state_focused="true" android:state_selected="false"
android:state_pressed="false" android:drawable="@drawable/tab_focus" />
<item android:state_focused="true" android:state_selected="true"
android:state_pressed="false" android:drawable="@drawable/tab_focus" />

<!-- Pressed -->
<item android:state_selected="true" android:state_pressed="true"
android:drawable="@drawable/tab_focus" />
<item android:state_pressed="true" android:drawable="@drawable/tab_press" />
</selector>

In layouts create tab_indicator.xml and put this code:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="0dip"
android:layout_height="55dip"
android:layout_weight="1"
android:orientation="vertical"

android:background="@drawable/tab_indicator"
android:padding="5dp">

<ImageView android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:src="@drawable/icon"

/>

<TextView android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
style="?android:attr/tabWidgetStyle"
/>
</RelativeLayout>


Related Topics



Leave a reply



Submit