Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tablayout with custom textview, didn't change selected state color on first launch

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.

like image 745
Vrangle Avatar asked Feb 10 '23 09:02

Vrangle


1 Answers

"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.

like image 82
Berry Berry Avatar answered Apr 30 '23 17:04

Berry Berry