0

I would like to change the text color of the active tab to white. Right now I'm using a selector and the inactive tabs are grey, but the active one is not white.

The XML Selector:

    <!-- Tab is active  -->
    <item   android:state_checked="true" 
            android:color="@color/tab_color_active" /> 

    <!-- Default -->
    <item   android:color="@color/tab_color_default"  /> 

</selector>

Layout part:

<android.support.v4.view.PagerTabStrip
    android:id="@+id/pager_tab_strip"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="top"
    android:background="#3D3D3D"
    android:paddingBottom="4dp"
    android:paddingTop="4dp"
    android:textColor="@drawable/tab_selector" />

This is the relevant code of my Fragment:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_community, container, false);

    viewPagerAdapter = new ViewPagerAdapter(getChildFragmentManager());
    viewPager = (ViewPager) rootView.findViewById(R.id.pager);
    viewPager.setAdapter(viewPagerAdapter);

    pagerTabStrip = (PagerTabStrip) rootView.findViewById(R.id.pager_tab_strip);
    pagerTabStrip.setDrawFullUnderline(true);
    pagerTabStrip.setTabIndicatorColor(Color.WHITE);


    return rootView;
}

The ViewPagerAdapter:

public class ViewPagerAdapter extends FragmentPagerAdapter {
    final int PAGE_COUNT = 4;
    private String tabTitles[] = new String[] {"Tab 1", "Tab 2", "Tab 3", "Tab 4"};

    public ViewPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        switch(position) {
        case 0:
            Fragment1 fragment1 = new Fragment1();
            return fragment1;
        case 1:
            Fragment2 fragment2 = new Fragment2();
            return fragment2;
        case 2:
            Fragment3 fragment3 = new Fragment3();
            return fragment3;
        case 3:
            MyImagesFragment fragment4 = new MyImagesFragment();
            return fragment4;
        }

        return null;
    }

    @Override
    public int getCount() {
        return PAGE_COUNT;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        Locale l = Locale.getDefault();

        return tabTitles[position].toUpperCase(l);
    }
}

Here is what it looks like: http://fs1.directupload.net/images/150409/3msfxnnl.png

I want the text color of Tab 1 to be white because is active. And furthermore I want the starting tab to be Tab 2 and not Tab 1. Does anyone know how to do that?

Black Kite
  • 13
  • 3
  • put picture, in my case i have white icon instead of text and it is not white even if it is active, could fix – Jemshit Apr 09 '15 at 12:11
  • Sorrry I don't understand what you mean. Can you explain more? – Black Kite Apr 09 '15 at 12:20
  • put screen of phone, what you have now – Jemshit Apr 09 '15 at 12:20
  • It's under the text. Here: http://fs1.directupload.net/images/150409/3msfxnnl.png Tab 1 is active but text color is not white. And also I want to start with Tab 2 when I open the fragment and not with Tab 1. – Black Kite Apr 09 '15 at 12:22
  • try changing `android:state_checked="true"` to `android:state_selected="true"` – Jemshit Apr 09 '15 at 12:27
  • Check this: http://stackoverflow.com/a/26955613/3736955 – Jemshit Apr 09 '15 at 12:30
  • I tried and even I tried other options like – Black Kite Apr 09 '15 at 12:31
  • In your PagerTabStrip class change mDefaultTabColorizer.setIndicatorColors(DEFAULT_SELECTED_INDICATOR_COLOR); – Mr Nice Apr 09 '15 at 13:05
  • Well I know now how to set the starting tab: viewPager.setCurrentItem(1); But still I don't know how to change the text color of the active tab. I tried the link but it still doesn't work. And I dont't have a PagerTabStrip class, also what is the mDefaultTabColorizer? – Black Kite Apr 09 '15 at 13:11
  • try setting white color in android:textColor – Harin Apr 09 '15 at 13:29
  • Then every text color will be white, but I just want the active ones to be white. – Black Kite Apr 09 '15 at 13:30
  • Hi, i had similar problem, and managed to solve it like this `PagerTabStrip mPagerTabStrip = (PagerTabStrip) findViewById(R.id.pgstrip); for (int i = 0; i < mPagerTabStrip.getChildCount(); ++i) { View nextChild = mPagerTabStrip.getChildAt(i); if (nextChild instanceof TextView) { TextView textViewToConvert = (TextView) nextChild; textViewToConvert.setTextColor(Color.WHITE); } }` I used [link] (http://stackoverflow.com/questions/13398471/custom-font-color-for-pagertabstrip-in-viewpager-for-my-android-app) to come to this solution.. – Dejan May 01 '15 at 09:03

0 Answers0