Android Tabhost Change Text Color Style

Android tabhost change text color style

You may change color of Tabhost text as follow.

tabHost.setOnTabChangedListener(new OnTabChangeListener() {

@Override
public void onTabChanged(String tabId) {

for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++) {
tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#FF0000")); // unselected
TextView tv = (TextView) tabhost.getTabWidget().getChildAt(i).findViewById(android.R.id.title); //Unselected Tabs
tv.setTextColor(Color.parseColor("#ffffff"));
}

tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(Color.parseColor("#0000FF")); // selected
TextView tv = (TextView) tabhost.getCurrentTabView().findViewById(android.R.id.title); //for Selected Tab
tv.setTextColor(Color.parseColor("#000000"))

}
});

EDIT:

To set text color initially in your activity, you can use this code in onResume() function

TabHost tabhost = getTabHost();
for(int i=0;i<tabhost.getTabWidget().getChildCount();i++)
{
TextView tv = (TextView) tabhost.getTabWidget().getChildAt(i).findViewById(android.R.id.title);
tv.setTextColor(Color.parseColor("#000000"));
}

Change text color and selector in TabWidget

I finally find a way to do that. Using this code in the onCreateViewmethod of the Fragment

    for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++) {
View v = tabHost.getTabWidget().getChildAt(i);
v.setBackgroundResource(R.drawable.tabs);

TextView tv = (TextView) tabHost.getTabWidget().getChildAt(i).findViewById(android.R.id.title);
tv.setTextColor(getResources().getColor(R.color.white));
}

And setting the bakcground color of the TabWidgetto red

How to change the tabIndicator's text color?

Try to use like following

TabWidget tabWidget = getTabWidget();
TabHost tab = getTabHost();

tab.setOnTabChangedListener(new OnTabChangeListener() {

public void onTabChanged(String arg0) {
for (int i = 0; i < tab.getTabWidget().getChildCount(); i++) {
tab.getTabWidget().getChildAt(i)
.setBackgroundResource(R.drawable.tab_selected); // unselected
}
tab.getTabWidget().getChildAt(tab.getCurrentTab())
.setBackgroundResource(R.drawable.tab_unselected); // selected

}
});

Intent intent = new Intent(FromClass.this,
ToClass.class);
TabSpec tabpiechartActivity = tab.newTabSpec("Name")
.setIndicator(prepareIndicator("Name"))
.setContent(intent);

and your prepareIndicator will be

private View prepareIndicator(String string) {
View view = LayoutInflater.from(this).inflate(R.layout.customtab, null);
// ImageView iv = (ImageView) view.findViewById(R.id.TabImageView);
TextView tv = (TextView) view.findViewById(R.id.tabText);
// iv.setImageResource(resId);
tv.setText(string);
return view;
}

And your res/layout/customtab xml is:

<?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:background="@drawable/tab_selected"
android:orientation="vertical" >

<TextView
android:id="@+id/tabText"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:textColor="#ffffff"
android:textSize="15dp" />

</LinearLayout>

And res/drawable/tab_selected.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<gradient
android:angle="225"
android:endColor="#11029E"
android:startColor="#DD000000" />
<corners
android:bottomLeftRadius="0dp"
android:bottomRightRadius="7dp"
android:radius="0.1dp"
android:topLeftRadius="0dp"
android:topRightRadius="7dp" />

</shape>

And res/drawable/tab_selected.tab_unselected.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >

<stroke
android:width="1dp"
android:color="#FFFFFFFF" />

<gradient
android:angle="225"
android:endColor="#0F7801"
android:startColor="#DD000000" />

<corners
android:bottomLeftRadius="0dp"
android:bottomRightRadius="7dp"
android:radius="0.1dp"
android:topLeftRadius="0dp"
android:topRightRadius="7dp" />

</shape>

I hope this will help you

How to change TabWidget color?

put these lines in your xml-

app:tabSelectedTextColor="@android:color/white"
app:tabTextColor="@android:color/black"

<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabGravity="fill"
app:tabMode="fixed"
app:tabTextAppearance="@style/MyTabLayoutTextAppearance"
app:tabIndicatorColor="@color/black"
app:tabSelectedTextColor="@android:color/white"
app:tabTextColor="@android:color/black"/>

How to change selected Tab Text color using TabLayout from code in Android?

If you are using the design support library add this code to your tab activity.

tabLayout.setSelectedTabIndicatorColor(Color.parseColor("#FF0000"));
tabLayout.setSelectedTabIndicatorHeight((int) (5 * getResources().getDisplayMetrics().density));
tabLayout.setTabTextColors(Color.parseColor("#727272"), Color.parseColor("#ffffff"));

This will set the tab text color as well as tab indicator color in your tab activity.



Related Topics



Leave a reply



Submit