Search code examples
javaandroidandroid-fragmentsfragmentandroid-pageradapter

Do Not Display Tabs if There is Only One Fragment Enabled


I have an activity that has two fragments, in two tabs: description tab and comments tab. Comments tab is optional, so when the admin adds a description he can enable or disable comments. If he disables it I would like to display description, but make it look like it not tabbed, ie. hide the tab from activity and still show the content.

So, if both fragments are enabled it should look like this:

enter image description here

Right now if I disable second tab I get this:

enter image description here

But THIS is what I would like to have:

enter image description here

Pager adapter:

class RestauranPagerAdapter extends FragmentStatePagerAdapter {
private int mNumOfTabs;

    RestauranPagerAdapter(FragmentManager fm, int NumOfTabs) {
        super(fm);
        this.mNumOfTabs = NumOfTabs;
    }

    @Override
    public Fragment getItem(int position) {

        switch (position) {
            case 0:
                return new RestauranFragmentDescription();
            case 1:
                return new RestauranFragmentComments();
            default:
                return null;
        }
    }

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

Activity:

TabLayout tabLayout = findViewById(R.id.RestauranTabs);
tabLayout.addTab(tabLayout.newTab().setText(R.string.description));
// tabLayout.addTab(tabLayout.newTab().setText(R.string.comments));
if (!Restauran.getComments_enabled()) {
    tabLayout.addTab(tabLayout.newTab().setText(R.string.comments));
}
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
tabLayout.setTabMode(TabLayout.MODE_FIXED);

final ViewPager viewPager = findViewById(R.id.RestauranViewpager);
final RestauranPagerAdapter adapter = new RestauranPagerAdapter(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
viewPager.setOffscreenPageLimit(3);

FloatingActionButton fabGoToMap = findViewById(R.id.fabGoToMap);
fabGoToMap.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {

    }
});

Solution

  • You can hide the tab layout when you have the only description enabled like below

    tabLayout.setVisibility(View.GONE);
    

    so your code should look like this

    TabLayout tabLayout = findViewById(R.id.RestauranTabs);
    tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
    tabLayout.setTabMode(TabLayout.MODE_FIXED);
    
    //adding the default one
    tabLayout.addTab(tabLayout.newTab().setText(R.string.description));
    
    if (!Restauran.getComments_enabled()) {
        tabLayout.addTab(tabLayout.newTab().setText(R.string.comments));
    }else{
        tabLayout.setVisibility(View.GONE);
    }
    
    //rest goes as it is
    final ViewPager viewPager = findViewById(R.id.RestauranViewpager);
    final RestauranPagerAdapter adapter = new RestauranPagerAdapter(getSupportFragmentManager(), tabLayout.getTabCount());
    viewPager.setAdapter(adapter);
    viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    viewPager.setOffscreenPageLimit(3);
    
    FloatingActionButton fabGoToMap = findViewById(R.id.fabGoToMap);
    fabGoToMap.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
    
        }
    });