I am using TabLayout
of design library what i want to acheive is
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;
}
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.
Use app:tabIndicatorColor . Documentation: https://developer.android.com/reference/android/support/design/widget/TabLayout.html#attr_android.support.design:tabIndicatorColor. "app:tabIndicatorColor" works.
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.
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.
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:
Use attribute app:tabTextColor to set Tab normal text color and use attribute app:tabSelectedTextColor to set Tab selected text color.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With