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?
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?
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"
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.
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); } }
I didn't get your question but answer given in android documentation
when you should use app:tabMode="fixed" or app:tabMode="scrollable"
tabMode="fixed" Fixed tabs display all tabs concurrently and are best used with content that benefits from quick pivots between tabs.
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.