8

I`m using ViewPager and TabLayout. If tabs can be placed on the display tabMode they must be:

app:tabMode="fixed"

else

app:tabMode="scrollable"

How can I do this?

Tiberal
  • 410
  • 2
  • 6
  • 16

5 Answers5

7

I din't get your question but i may help you, if tabs count is static or fixed(you know number of tabs) then app:tabMode="fixed" if tabs count is dynamic(data coming from feed) then app:tabMode="scrollable"

Julien
  • 3,509
  • 20
  • 35
Ram Prakash Bhat
  • 1,308
  • 12
  • 21
5

There are two tabModes we can use with TabLayout:

app:tabMode="fixed"
app:tabMode="scrollable"

and programatically it is like

 tabLayout.setTabMode(TabLayout. MODE_FIXED);
tabLayout.setTabMode(TabLayout. MODE_SCROLLABLE);

above is same for MODE_SCROLLABLE

to understand TabLayout modes you can refer below Link https://developer.android.com/reference/android/support/design/widget/TabLayout

Yashoda Bane
  • 399
  • 4
  • 7
2

In the xml layout, I declared

fixed

and in the fragment java class, I made this:

    if (tabLayout == null) {
        tabLayout = (TabLayout) view.findViewById(R.id.tabs);

        DisplayMetrics metrics = new DisplayMetrics();
        WindowManager wm = (WindowManager) view.getContext().getSystemService(Context.WINDOW_SERVICE);
        wm.getDefaultDisplay().getMetrics(metrics);

        int width = metrics.widthPixels;
        int height = metrics.heightPixels;
        double wi = (double) width / (double) metrics.xdpi;
        double hi = (double) height / (double) metrics.ydpi;
        double x = Math.pow(wi, 2);
        double y = Math.pow(hi, 2);
        double screenInches = Math.sqrt(x + y);

        if (screenInches < 4) {
            tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
        }
    }
Sergio Rey
  • 83
  • 6
1

I didn't get your question but answer given in android documentation

when you should use app:tabMode="fixed" or app:tabMode="scrollable"

  1. tabMode="fixed" Fixed tabs display all tabs concurrently and are best used with content that benefits from quick pivots between tabs.

  2. app:tabMode="scrollable" Scrollable tabs display a subset of tabs at any given moment, and can contain longer tab labels and a larger number of tabs. They are best used for browsing contexts in touch interfaces when users don’t need to directly compare the tab labels. This mode is commonly used with a ViewPager.

Sunil Patel
  • 265
  • 3
  • 13
1

If the problem is that for certain screen configurations (e.g. small screens, portrait config, etc.) the tabs need to be scrollable because they don't fit the screen and, for other screen config/sizes the number of tabs fit perfectly in the screen, you could just use the resource qualifiers to define your tabs differently for different screen config/sizes.

As an example, imagine your tabs do not fit in portrait but do fit in landscape. Hence one might want the tabs to be scrollable in portrait and fixed in landscape.

res/layout/tabs.xml would contain the TablLayout to use in portrait:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.TabLayout xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/tabs"
    app:tabMode="scrollable"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

and res/layout-land/tabs.xml would contain the TabLayout used in landscape:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.TabLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/tabs"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

Then in your layout that contains the tabs, just include it like so:

<include layout="@layout/tabs" />

Hope this is what you're looking for.

Aitor Viana
  • 933
  • 6
  • 15