Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change icon and title color when selected in android design library TabLayout

I am using TabLayout of design library what i want to acheive is

I want to achieve

I have tried many tutorial and i am able to achieve it by customizing the tab but there is an limitation that is occurring when the tab is selected i want to change the text color as well as the image of the icon which is not possible by referring any of the tutorial i read so far. I have tried this so far by adding this in the FragmentStatePagerAdapter

public View getTabView(int position) {
    View tab = LayoutInflater.from(mContext).inflate(R.layout.tabbar_view, null);
    TextView tabText = (TextView) tab.findViewById(R.id.tabText);
    ImageView tabImage = (ImageView) tab.findViewById(R.id.tabImage);
    tabText.setText(mFragmentTitles.get(position));
    tabImage.setBackgroundResource(mFragmentIcons.get(position));
    if (position == 0) {
        tab.setSelected(true);
    }
    return tab;
}
like image 429
Vishwajit Palankar Avatar asked Nov 17 '15 11:11

Vishwajit Palankar


People also ask

How to set TabLayout title in Android?

The title and icon of Tabs are set through setText(int) and setIcon(int) methods of TabListener interface respectively. Tabs of layout are attached over TabLayout using the method addTab(Tab) method. We can also add tab item to TabLayout using TabItem of android design widget.

How to change TabLayout underline color in Android?

Use app:tabIndicatorColor . Documentation: https://developer.android.com/reference/android/support/design/widget/TabLayout.html#attr_android.support.design:tabIndicatorColor. "app:tabIndicatorColor" works.

How to design TabLayout in Android?

This example demonstrates how do I create a Tab Layout in android app. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 3 − Add the following code to res/layout/activity_main. xml.

How to set text color to the toolbar title in Android?

In the activity’s onCreate () method, call the activity’s setSupportActionBar () method, and pass the activity’s toolbar. This method sets the toolbar as the app bar for the activity. Add below codes in your Activity to set the text color to the Toolbar title.

How to change the tab's label or icon in Android?

From there you can change the tab's label or icon via TabLayout.Tab.setText (int) and TabLayout.Tab.setIcon (int) respectively. To display the tab, you need to add it to the layout via one of the addTab (Tab) methods. For example:

How to change the color of the text on the tab?

Use attribute app:tabTextColor to set Tab normal text color and use attribute app:tabSelectedTextColor to set Tab selected text color.

How do I set the selection indicator color for a tablayout?

Sets the drawable resource to use as the selection indicator for this TabLayout. By default, this is a line along the bottom of the tab. If tabIndicatorColor is specified via the TabLayout's style or via setSelectedTabIndicatorColor (int) the selection indicator will be tinted that color.


1 Answers

The Design Library were updated to match the material design "Tabs with icons and text" specs, so you don't need a custom tab view.

But in the current version (23.1.1), only the text colors match the spec (tab focused - #fff, tab unfocused - 70% #fff). So you can use the ColorStateList returned by getTabTextColors() to tint the icons using DrawableCompat.setTintList(ColorStateList).

Try to use this gist https://gist.github.com/mikovali/7a89b505cd6306bb94a8. Removing the line tabs.setTabTextColors(Color.RED, Color.GREEN) should be enough to match the spec for both text and icon on dark toolbars.

like image 195
Pedro Avatar answered Oct 12 '22 07:10

Pedro