Different tabMode for TabLayout
Asked Answered
L

5

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?

Leathery answered 2/7, 2015 at 17:2 Comment(3)
what is your question?Godden
Possible Duplicated Question : #30616974Terracotta
Possible Answer : https://mcmap.net/q/193530/-android-support-design-tablayout-gravity-center-and-mode-scrollableTerracotta
G
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"

Gavel answered 11/1, 2016 at 15:30 Comment(0)
J
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

Jacobsohn answered 27/8, 2018 at 10:16 Comment(0)
L
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);
        }
    }
Limiting answered 28/3, 2017 at 13:32 Comment(0)
G
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.

Giguere answered 28/2, 2016 at 17:53 Comment(0)
P
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.

Prank answered 28/2, 2016 at 18:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.