I tried to figure out something ridiculous in tablayout's custom textview. When I launch the app, the first tab's textview is in default color, However when I go through other tabs and return back to first tab, it works correctly.Here is the code.
selector.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true"
android:color="#FFFFFF"/> <!-- selected -->
<item android:color="@color/red_highlight"/> <!-- default -->
</selector>
MainActivity.java
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
tabLayout.getTabAt(0).setCustomView(R.layout.tab_custom_view);
tabLayout.getTabAt(1).setCustomView(R.layout.tab_custom_view);
tabLayout.getTabAt(2).setCustomView(R.layout.tab_custom_view);
tab_custom_view.xml
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tab_tittle"
android:text="Tab1"
android:textStyle="bold"
android:textColor="@color/selector"/>
The first tab is in default color when I launch the app. after clicking the other tabs and return back to first tab it is in selected color. But how supposed to be is, when I launch the app first tab is selected and should be in selected color.
"state_selected" is used when an item is selected using a keyboard/dpad/trackball/etc. So it isnt selected when you launch the app, its selected when you select the tab.
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