Android: Change Tab Text Color Programmatically

Android: Change Tab Text Color Programmatically

To change the text color of tabs, you need to get the view i.e TextView which is set as title of tabs and you can change it like this:

    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(.....);
}

hope this helps....

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.

Change tabs text color in TabLayout to different colors programmatically

Try this and let me know if this works for you:

tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
if (tab.getPosition() == 0) {
tabLayout.getTabAt(0).getIcon().setAlpha(255);
tabLayout.getTabAt(1).getIcon().setAlpha(100);
tabLayout.getTabAt(2).getIcon().setAlpha(100);
} else if (tab.getPosition() == 1) {
tabLayout.getTabAt(0).getIcon().setAlpha(100);
tabLayout.getTabAt(1).getIcon().setAlpha(255);
tabLayout.getTabAt(2).getIcon().setAlpha(100);

} else if (tab.getPosition() == 2) {
tabLayout.getTabAt(0).getIcon().setAlpha(100);
tabLayout.getTabAt(1).getIcon().setAlpha(100);
tabLayout.getTabAt(2).getIcon().setAlpha(255);

}
}

@Surya Prakash Kushawah your way is better.

Is there a way to set a particular Tablayout.Tab text color programmatically, without using a state list?

The easiest way is to get the TextView from a specified TabLayout.Tab and then set the text color using TextView.SetTextColor(Color color), which you can do as followed:

TabLayout tabLayout = new TabLayout(this);
int wantedTabIndex = 0;

TextView tabTextView = (TextView)(((LinearLayout)((LinearLayout)tabLayout.GetChildAt(0)).GetChildAt(wantedTabIndex)).GetChildAt(1));

var textColor = Color.Black;
tabTextView.SetTextColor(textColor);

How to Change Text Color of tab Layout?

You can customize your TabLayout's text.

Create a TextView from Java Code or XML like this

<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:textSize="15sp"
android:textColor="@color/tabs_default_color"
android:gravity="center"
android:layout_height="match_parent"
/>

Make sure to keep the id as it is here because the TabLayout check for this ID if you use custom TextView

Then from code inflate this layout and set the custom Typeface on that TextView and add this custom view to the tab.

for (int i = 0; i < tabLayout.getTabCount(); i++) {
//noinspection ConstantConditions
TextView tv=(TextView)LayoutInflater.from(this).inflate(R.layout.custom_tab,null)
tv.setTextColor(customColor)
tabLayout.getTabAt(i).setCustomView(tv);

}

How to set unselected Text Color for TabLayout.Tab - Android

Use app:tabTextColor for default tab color and app:tabSelectedTextColor for selected tab color.

        <android.support.design.widget.TabLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabTextColor="#000000"
app:tabSelectedTextColor="#FFFFFF"/>

Is there a way to change the TabLayout.Tab text color without a state list?

If you are using the Design Support Library, you can use TabLayout.SetTabTextColors(int normalColor, int selectedColor). (source)

This does not require a ColorStateList as mentioned but just two integers pointing to color resources. First parameter being the default color and the second parameter being the text color of a selected tab.



Related Topics



Leave a reply



Submit