Is it possible to create a custom Tablayout in xamarin android where you set the min width?

So in Java the code to do this would look like this

public class CustomTabLayout extends TabLayout {

private static final int WIDTH_INDEX = 0;
private static final int DIVIDER_FACTOR = 3;
private static final String SCROLLABLE_TAB_MIN_WIDTH = "mScrollableTabMinWidth";

public CustomTabLayout(Context context) {
    super(context);
    initTabMinWidth();
}

public CustomTabLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
    initTabMinWidth();
}

public CustomTabLayout(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    initTabMinWidth();
}

private void initTabMinWidth() {
    int[] wh = Utils.getScreenSize(getContext());
    int tabMinWidth = wh[WIDTH_INDEX] / DIVIDER_FACTOR;

    Field field;
    try {
        field = TabLayout.class.getDeclaredField(SCROLLABLE_TAB_MIN_WIDTH);
        field.setAccessible(true);
        field.set(this, tabMinWidth);
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }
}

}

what would the xamarin android equivalent of especially thte initTabMinWidth() function be?

Best Answer

  • MasulukeIMasulukeI
    Accepted Answer

    How I eventually got it working

    class CustomThreeTabLayout : Android.Support.Design.Widget.TabLayout
    {
    public CustomThreeTabLayout(Context context) : base(context) { }

        public CustomThreeTabLayout(Context context, IAttributeSet attrs) : base(context, attrs) { }
    
        public CustomThreeTabLayout(Context context, IAttributeSet attrs, int defStyleAttr) : base(context, attrs, defStyleAttr) { }
    
        protected override void OnMeasure(int widthMeasureSpec, int heightMeasureSpec)
        {
    
            ViewGroup tabLayout = (ViewGroup)GetChildAt(0);
            int childCount = tabLayout.ChildCount;
    
            DisplayMetrics displayMetrics = Context.Resources.DisplayMetrics;
            int tabMinWidth = displayMetrics.WidthPixels / childCount;
    
            for (int i = 0; i < childCount; ++i)
            {
                tabLayout.GetChildAt(i).SetMinimumWidth(displayMetrics.WidthPixels / 3);
            }
    
            base.OnMeasure(widthMeasureSpec, heightMeasureSpec);
        }
    }
    

Answers

  • MasulukeIMasulukeI Member
    Accepted Answer

    How I eventually got it working

    class CustomThreeTabLayout : Android.Support.Design.Widget.TabLayout
    {
    public CustomThreeTabLayout(Context context) : base(context) { }

        public CustomThreeTabLayout(Context context, IAttributeSet attrs) : base(context, attrs) { }
    
        public CustomThreeTabLayout(Context context, IAttributeSet attrs, int defStyleAttr) : base(context, attrs, defStyleAttr) { }
    
        protected override void OnMeasure(int widthMeasureSpec, int heightMeasureSpec)
        {
    
            ViewGroup tabLayout = (ViewGroup)GetChildAt(0);
            int childCount = tabLayout.ChildCount;
    
            DisplayMetrics displayMetrics = Context.Resources.DisplayMetrics;
            int tabMinWidth = displayMetrics.WidthPixels / childCount;
    
            for (int i = 0; i < childCount; ++i)
            {
                tabLayout.GetChildAt(i).SetMinimumWidth(displayMetrics.WidthPixels / 3);
            }
    
            base.OnMeasure(widthMeasureSpec, heightMeasureSpec);
        }
    }
    
  • jezhjezh Member, Xamarin Team Xamurai

    Congrats, and thanks for sharing the solution.

Sign In or Register to comment.