Material Tab - mayurparmar2/AlarmDemo GitHub Wiki
 .text.XML
<androidx.viewpager.widget.ViewPager
android:id="@+id/flFragment"
android:background="#ffffff"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@+id/tl_home"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.material.tabs.TabLayout
android:id="@+id/tl_home"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="@drawable/bg_shape"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:tabGravity="fill"
app:tabIndicator="@null"
app:tabMaxWidth="0dp"
app:tabMode="fixed"
app:tabPaddingBottom="@dimen/dp_8"
app:tabPaddingTop="@dimen/dp_14"
app:tabIconTint="@color/tabIconTint"
app:tabTextAppearance="@style/home_bottom_text"
app:tabSelectedTextColor="@color/white"
app:tabTextColor="@color/tabIconTint"
app:tabUnboundedRipple="true"
/>
bg_shape.xml
<?version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@color/alarm" />
<corners
android:topLeftRadius="@dimen/dp_16"
android:topRightRadius="@dimen/dp_16" />
</shape>
MainActivity.java
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabLayout);
tabLayout.setupWithViewPager(mPager);
tabLayout.getTabAt(0).setIcon(tabIcons[0]);
tabLayout.getTabAt(1).setIcon(tabIcons[1]);
this.viewPager = (ViewPager) findViewById(R.id.flFragment);
ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
viewPagerAdapter.add(new FirstHome(), "HOME");
viewPagerAdapter.add(new InsightsHome(), "INSIGHTS");
viewPagerAdapter.add(new Mefragment(), "SETTINGS");
this.mViewPagerAdapter = viewPagerAdapter;
this.viewPager.setAdapter(viewPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tl_home);
tabLayout.setupWithViewPager(viewPager);
tabLayout.getTabAt(0).setIcon(tabIcons[0]);
tabLayout.getTabAt(1).setIcon(tabIcons[1]);
tabLayout.getTabAt(2).setIcon(tabIcons[2]);
tabLayout.getTabAt(0).getIcon().setTint(ContextCompat.getColor(context,R.color.tabIconTint));
// tabLayout.selectTab(tabLayout.getTabAt(0));
// viewPager.setCurrentItem(0);
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
tab.getIcon().setTint(ContextCompat.getColor(context,R.color.white));
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
tab.getIcon().setTint(ContextCompat.getColor(context,R.color.tabIconTint));
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
this.viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrollStateChanged(int i) {
}
@Override
public void onPageScrolled(int i, float f, int i2) {
}
@Override
public void onPageSelected(int i) {
});
ViewPagerAdapter .java
public class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> fragments = new ArrayList<>();
private final List<String> fragmentTitle = new ArrayList<>();
public ViewPagerAdapter(@NonNull FragmentManager fm) {
super(fm);
}
public void add(Fragment fragment, String title) {
fragments.add(fragment);
fragmentTitle.add(title);
}
@NonNull
@Override
public Fragment getItem(int position) {
return fragments.get(position);
}
@Override
public int getCount() {
return fragments.size();
}
@Nullable
@Override
public CharSequence getPageTitle(int position) {
return fragmentTitle.get(position);
}
}