Tablayout with custom text view did not change selected state color on first launch

I tried to figure out something ridiculous about the custom tablayout text view. When I launch the application, the first text box of the tab has its default color. However, when I browse the other tabs and go back to the 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 has a default color when the application starts. after clicking on other tabs and returning to the first tab, it is selected in the selected color. But as it should be, when I launch the application, the first tab is selected and should be in the selected color.

+3


source to share


1 answer


"state_selected" is used when an item is selected using the keyboard / dpad / trackball / etc. Therefore, it is not selected when starting the application, it is selected when a tab is selected.



+7


source







All Articles