I have made a custom TabLayout with a ViewPager
and am using the TabLayout in scrollable mode:
I need it to be scrollable as the number of dates can vary to as many as 15-20:
<com.example.project.recommendedapp.CustomScrollableTabLayout
android:id="@+id/tab_layout_movie"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:elevation="6dp"
android:minHeight="?attr/actionBarSize"
app:tabGravity="fill"
app:tabMode="scrollable"
app:tabTextAppearance="?android:textAppearanceMedium"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
The custom class i used as per another similar question: TabLayout not filling width when tabMode set to 'scrollable'
The custom tablayout class is:
package com.example.project.recommendedapp;
import android.content.Context;
import android.graphics.Point;
import android.support.design.widget.TabLayout;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Display;
import android.view.WindowManager;
import java.lang.reflect.Field;
public class CustomScrollableTabLayout extends TabLayout {
private Context mContext;
Point size;
public CustomScrollableTabLayout(Context context) {
super(context);
mContext=context;
size = new Point();
}
public CustomScrollableTabLayout(Context context, AttributeSet attrs) {
super(context, attrs);
mContext=context;
size = new Point();
}
public CustomScrollableTabLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
mContext=context;
size = new Point();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
try {
if (getTabCount() == 0) {
return;
}else {
Display display = ((WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
display.getSize(size);
int width = size.x;
Field field = TabLayout.class.getDeclaredField("mScrollableTabMinWidth");
field.setAccessible(true);
field.set(this, (width / getTabCount()));
Log.d("FragmentCreate",String.valueOf(width / getTabCount()));
}
} catch (Exception e) {
Log.e("FragmentCreate","Error while setting width",e);
}
}
}
TabLayout
inside a layout and setting gravity to center_horizontal. – Wolfy