Edivan Edivan - 6 months ago 31
Java Question

How make proportional tab with tablayout?

I am not able to make the tabs have a proportional size on the device screen. How would I do that?
Here I am setting for tablayout.

<LinearLayout
android:id="@+id/main_layout"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
xmlns:app="http://schemas.android.com/apk/res-auto">

<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:tabMode="scrollable" />

<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_below="@id/tab_layout"/>

</LinearLayout>


The code below is within a fragment, which in turn is within a NavigationView

View view = inflater.inflate(R.layout.fragment_extract, container, false);

String details = getResources().getString(R.string.aba_details);
String people = getResources().getString(R.string.aba_people);
String documents = getResources().getString(R.string.aba_documents);

TabLayout tabLayout = (TabLayout) view.findViewById(R.id.tab_layout);
tabLayout.addTab(tabLayout.newTab().setText(details));
tabLayout.addTab(tabLayout.newTab().setText(people));
tabLayout.addTab(tabLayout.newTab().setText(documents));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

final ViewPager viewPager = (ViewPager) view.findViewById(R.id.pager);
final ProcessPagerAdapter adapter = new ProcessPagerAdapter
(getFragmentManager(), getActivity(), process);
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabUnselected(TabLayout.Tab tab) {

}

@Override
public void onTabReselected(TabLayout.Tab tab) {

}
});

Answer

Add this following code in your android.support.design.widget.TabLayout.

app:tabMaxWidth="0dp"
app:tabGravity="fill"
app:tabMode="fixed"
Comments