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>