I created a tablayout with viewpager and 3 fragments. When I run the application I can see correctly everything except of the tab titles that do not show, what am I missing?
My code in mainactivity.oncreate:
tabLayout=findViewById(R.id.tab_layout);
viewPager= findViewById((R.id.view_pager));
final ViewPagerAdapter viewPagerAdapter= new ViewPagerAdapter(getSupportFragmentManager());
viewPagerAdapter.addFragment(RulesFragment.getInstance(), "TREE RULES");
viewPagerAdapter.addFragment(TreeFragment.getInstance(), "REGRESSION TREE");
viewPagerAdapter.addFragment(PredictionFragment.getInstance(), "PREDICTION");
rulesFragment= (RulesFragment) viewPagerAdapter.getItem(0);
treeFragment= (TreeFragment) viewPagerAdapter.getItem(1);
predictionFragment= (PredictionFragment) viewPagerAdapter.getItem(2);
viewPager.setAdapter(viewPagerAdapter);
tabLayout.setupWithViewPager(viewPager);
viewPager.setOffscreenPageLimit(3);
.xml file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.viewpager.widget.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.tabs.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabIndicatorAnimationDuration="1000"
app:tabIndicatorColor="@android:color/holo_blue_dark"
app:tabIndicatorGravity="top"
app:tabSelectedTextColor="@android:color/holo_purple"
app:tabTextColor="@android:color/white"
app:tabBackground="@android:color/holo_orange_light">
</com.google.android.material.tabs.TabLayout>
</androidx.viewpager.widget.ViewPager>
</RelativeLayout>
In .xml I added the viewpager and the tablayout only, I didn't add any textview or other things for the tab titles since I thought it would do that automatically with tabLayout.setupWithViewPager(viewPager). Is it wrong?
Implement getPageTitle inside your ViewPagerAdapter!
Like-
// Returns the page title for the top indicator
@Override
public CharSequence getPageTitle(int position) {
return "Page " + position;
}
Reference you can check -
https://guides.codepath.com/android/viewpager-with-fragmentpageradapter
Also, your ViewPagerAdapter should not hold onto a List<Fragment>
as that will cause crashes after process death in production. The correct way to implement a FragmentPagerAdapter is:
public class ViewPagerAdapter extends FragmentPagerAdapter {
public ViewPagerAdapter(FragmentManager fragmentManager) {
super(fragmentManager);
}
@Override
public Fragment getItem(int position) {
if(position == 0) return new RulesFragment();
if(position == 1) return new TreeFragment();
if(position == 2) return new PredictionFragment();
throw new IllegalStateException("Unexpected position " + position);
}
@Override
public int getCount() {
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
if(position == 0) return "TREE RULES";
if(position == 1) return "REGRESSION TREE";
if(position == 2) return "PREDICTION";
throw new IllegalStateException("Unexpected position " + position);
}
}
To get a reference to a Fragment created by a ViewPager, use the following findFragmentByTag
scheme:
Fragment fragment = supportFragmentManager.findFragmentByTag("android:switcher:" + viewPager.getId() + ":" + fragmentPosition)