1

I am using the tablayout in fragment. This is my main fragment for tabs

public class TabFragment extends Fragment {
private TabLayout tabLayout;
private ViewPager viewPager;

public TabFragment () {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View v = inflater.inflate(R.layout.fragment_tab, container, false);
    viewPager = (ViewPager) v.findViewById(R.id.viewpager);
    setupViewPager(viewPager);

    tabLayout = (TabLayout) v.findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(viewPager);
    setupTabIcons();
    tabLayout.setSelectedTabIndicatorColor(ContextCompat.getColor(getActivity(), android.R.color.transparent));

    return v;
}

private void setupTabIcons() {
    TextView tabOne = (TextView) LayoutInflater.from(getActivity()).inflate(R.layout.tab_text, null);
    tabOne.setText("\"fragment 1\"");
    tabLayout.getTabAt(0).setCustomView(tabOne);

    TextView tabTwo = (TextView) LayoutInflater.from(getActivity()).inflate(R.layout.tab_text, null);
    tabTwo.setText("\"fragment 2\"");
    tabLayout.getTabAt(1).setCustomView(tabTwo);
}


private void setupViewPager(ViewPager viewPager) {
    ViewPagerAdapter adapter = new ViewPagerAdapter(getChildFragmentManager());
    adapter.addFragment(new Fragment1(), "fragment 1");
    adapter.addFragment(new Fragment2(), "fragment 2");
    viewPager.setAdapter(adapter);
}

class ViewPagerAdapter extends FragmentPagerAdapter {
    private final List<Fragment> mFragmentList = new ArrayList<>();
    private final List<String> mFragmentTitleList = new ArrayList<>();

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

    @Override
    public Fragment getItem(int position) {
        return mFragmentList.get(position);
    }

    @Override
    public int getCount() {
        return mFragmentList.size();
    }

    public void addFragment(Fragment fragment, String title) {
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mFragmentTitleList.get(position);
    }
}
}

The fragment XML

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.TabFragment">

<!-- TODO: Update blank fragment layout -->
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        app:tabBackground="@drawable/tab_color_selector"
        app:tabGravity="fill"
        app:tabMode="fixed"
        android:layout_margin="10dp"
        android:background="@drawable/tab_rectangle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

</FrameLayout>

Text view for the tabs

<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:text="XXXX"
android:textColor="@color/tab_text_color"
android:gravity="center"
android:textSize="18sp"
android:layout_height="wrap_content" />

Color selector - tab_text_color

   <?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@android:color/white" android:state_selected="true" />
<item android:color="@android:color/white" android:state_focused="true" />
<item android:color="@android:color/white" android:state_pressed="true" />
<item android:color="@color/colorAppBlue" />
</selector>

When the app is opened I don't get the selected tab text color as white.

This is the tab selector

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/colorAppBlue" android:state_selected="true"/>
<item android:drawable="@android:color/white"/>
</selector>
WISHY
  • 11,067
  • 25
  • 105
  • 197
  • Apply style to your tablayout – Suhas Bachewar Feb 03 '16 at 09:06
  • @SuhasB it doesn't work.. – WISHY Feb 03 '16 at 09:18
  • http://stackoverflow.com/questions/30921722/cannot-change-active-text-color-on-tablayout – IntelliJ Amiya Feb 03 '16 at 09:28

4 Answers4

1

try this:

On onCreateView

tableLayout.getChildAt(0).setSelected(true);

Sabari
  • 1,963
  • 1
  • 13
  • 10
1

Finally solved it Just needed to add this line....

tabLayout.getTabAt(0).getCustomView().setSelected(true);
WISHY
  • 11,067
  • 25
  • 105
  • 197
0

Maybe, your textColor is overrided by android or missing something in code that i can't see. But i'll give you another way to use this way to change tablayout text color.

<style name="TabLayoutTextAppearance" parent="TextAppearance.AppCompat.Widget.ActionBar.Title.Inverse">
    <item name="android:textSize">14sp</item>
    <item name="android:textColor">//your text color</item>
    <item name="android:textAllCaps">true</item>
</style>

And add this style to your tablayout

app:tabTextAppearance="@style/TabLayoutTextAppearance"
topcbl
  • 799
  • 6
  • 20
  • These pieces of code work well on my current project. please try it again in clean way without custom anything first. After it success, you can do more stuff with this @WISHY – topcbl Feb 03 '16 at 09:22
0
You can try this for changing the tab background color or text color


LinearLayout tabsContainer = (LinearLayout) tabLayout.getChildAt(0);
        for (int i = 0; i < tabLayout.getTabCount(); i++) {
                    LinearLayout item = (LinearLayout) tabsContainer.getChildAt(section - 1);
                    TextView tv = (TextView) item.getChildAt(1);
                    item.setBackgroundColor(getResources().getColor(R.color.color00DF4C));
                    tv.setTextColor(getResources().getColor(R.color.colorWhite));
                }
Radhika bajaj
  • 147
  • 1
  • 8