Tabs of TabLayout not showing
Asked Answered
S

7

11

I have a main activity, which hosts a fragment, which in turn hosts a TabLayout (with a ViewPager). The tab bar is shown, baut the tabs themselves are not shown.

Here is my code in the main activity for displaying the host fragment:

        Fragment fragment = new BMITabsFragment();

        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).addToBackStack(Constants.BMI_TABS_FRAGMENT).commit();

Here is my the Fragment which hosts the TabLayout, which is BMITabsFragment (s.a.):

public class BMITabsFragment extends Fragment {
...
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
        mParam1 = getArguments().getString(ARG_PARAM1);
        mParam2 = getArguments().getString(ARG_PARAM2);
    }

}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    // Get the ViewPager and set it's PagerAdapter so that it can display items
    ViewPager viewPager = (ViewPager) view.findViewById(R.id.viewpager);
    viewPager.setAdapter(new BMIFragmentPagerAdapter(getActivity().getSupportFragmentManager(),
            getActivity()));

    // Give the TabLayout the ViewPager
    TabLayout tabLayout = (TabLayout) view.findViewById(R.id.sliding_tabs);
    tabLayout.setupWithViewPager(viewPager);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_bmitabs, container, false);
    return view;
}
...
}

This is my FragmentPagerAdapter:

public class BMIFragmentPagerAdapter extends FragmentPagerAdapter {

final int PAGE_COUNT = 2;
private FragmentManager fragmentManager;
private Context context;

public BMIFragmentPagerAdapter(FragmentManager fm, Context context) {
    super(fm);
    this.context = context;
    this.fragmentManager = fm;

}

public BMIFragmentPagerAdapter(FragmentManager fm) {
    super(fm);
    fragmentManager = fm;

}

@Override
public CharSequence getPageTitle(int position) {
    String[] pageTitles = context.getResources().getStringArray(R.array.page_titles_array);
    return pageTitles[position];
}

@Override
public Fragment getItem(int position) {
    SharedPreferences prefs = context.getSharedPreferences(Constants.SHARED_PREFS_FILE, 0);
    long patientId = prefs.getLong(Constants.SELECTED_PATIENT_ID, 1);
    Fragment fragment = null;
    switch (position){
        case 0:
            return BMITabelleFragment.newInstance(patientId);

        case 1:
            return BMIChartFragment.newInstance(patientId);

        default:
            return BMITabelleFragment.newInstance(patientId);
    }
}

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

And this is the fragment_bmitabs.xml:

<?xml version="1.0" encoding="utf-8"?>

<android.support.design.widget.TabLayout
    android:id="@+id/sliding_tabs"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:tabMode="scrollable" />

<android.support.v4.view.ViewPager
    android:id="@+id/viewpager"
    android:layout_width="match_parent"
    android:layout_height="0px"
    android:layout_weight="1"
    android:background="@android:color/white" />

</LinearLayout>

My code is based on the Google Android Guide at https://github.com/codepath/android_guides/wiki/Google-Play-Style-Tabs-using-TabLayout

What I am missing here?

Note: I am using AppCompatActivity and the support libraries v4 & v7 and the com:android:support:design library

Sellars answered 21/7, 2015 at 16:26 Comment(1)
a simple update in styles.xml - along the lines of <style name="TextAppearance.Widget.TabWidget" parent="@style/AppTheme"> .. worked for me - answer here: https://mcmap.net/q/303721/-how-to-change-the-color-of-the-tabs-indicator-text-in-androidMetaphor
O
51

This fixed it for me:

tabLayout.post(new Runnable() {
    @Override
    public void run() {
        tabLayout.setupWithViewPager(viewPager);
    }
});

https://code.google.com/p/android/issues/detail?id=180462

Orthogonal answered 22/7, 2015 at 22:0 Comment(2)
Absurd... documentation says "This view also supports being used as part of a ViewPager's decor, and can be added directly to the ViewPager in a layout resource file like so" suggesting that if you use this view in this way you don't need to call that method.Tinctorial
not working on me. fragments of tab still not showingRodrickrodrigez
P
9

Set tab icons after setupWithViewPager()

private void setTabs {
   tabLayout.setupWithViewPager(viewPager);
   setupTabIcons();
} 

private void setupTabIcons() {
   tabLayout.getTabAt(0).setIcon(tabIcons[0]);
   tabLayout.getTabAt(1).setIcon(tabIcons[1]);
   tabLayout.getTabAt(2).setIcon(tabIcons[2]);
}
Precisian answered 14/10, 2015 at 13:34 Comment(2)
You forgot to fix the link in this answer. Right now it points to 'enter link description here'.Emigration
That worked for me, the viewPager allready initialize the Tabs, just grabbing the tab with getTabAt() and set Text, Icon or CustomView worked!Fatuous
W
6

like @Nathaniel Ford said,this should a bug, I change to use design library 23.0.1。google fixed it,so change build.gradle to compile 'com.android.support:design:23.0.1'. ps:you also must change your compileSdkVersionto 23

Wake answered 16/9, 2015 at 12:5 Comment(1)
Thank you... this worked for me. The poor quality of Google's API keeps surprising me, it's just astonishing.Apia
D
2

None of the other answers worked for me, I tried all of them

However, this one did: TabLayout not showing tabs after adding navigation bar

According to that answer, you have to enclose your TabLayout in a AppBarLayout. Why? Who knows. But at least it works.

Example Code (taken from that post):

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="200dp"
    android:id="@+id/appBarLayout2">


    <android.support.design.widget.TabLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/tabLayout2"
        app:tabMode="fixed"
        app:tabGravity="fill"

        ></android.support.design.widget.TabLayout>

</android.support.design.widget.AppBarLayout>

<android.support.v4.view.ViewPager
    android:layout_width="match_parent"
    android:layout_height="300dp"
    android:layout_alignParentStart="true"
    android:layout_alignParentBottom="true"
    android:id="@+id/viewPager2"
    android:layout_below="@+id/appBarLayout2">

</android.support.v4.view.ViewPager>
Decasyllabic answered 2/7, 2018 at 1:8 Comment(0)
A
1

If you are using android:tabPadding attribute in Tablayout of xml file,remove it.

Auberge answered 16/5, 2017 at 12:42 Comment(1)
As this could be a valid answer, please elaborate it a bit more and explain why. Thank youUnterwalden
D
0

You can use FragmentStatePagerAdapter instead of FragmentPagerAdapter. It may help you.

Demineralize answered 10/9, 2019 at 10:11 Comment(0)
E
0

If someone, in future, works with TabLayout along with ViewPager2 and faces same issue. Just add .attach(); at end of new TabLayoutMediator().

new TabLayoutMediator(tabLayoutRef, viewPager2Ref, (tab, position) -> tab.setText("Tab No. "+position)).attach();
Extensometer answered 6/8, 2021 at 11:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.