material-components-android icon indicating copy to clipboard operation
material-components-android copied to clipboard

[TabLayout] Margin for each tab item

Open danielgomezrico opened this issue 4 years ago • 4 comments

Currenlty you can not change a Tab item from the TabLayout from the styles, you have to do some hacky thing like:

The issue is when you want to change the tab background shape into something like: image

But since this is not possible from the styles it will appear like this since: image

Possible Solution

One atribute tab_margin_start and each margin for each item maybe?

Current hacky solution

Hacky programaticaly solution source:

for(int i=0; i < mTabLayout.getTabCount(); i++) {
    View tab = ((ViewGroup) mTabLayout.getChildAt(0)).getChildAt(i);
    ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) tab.getLayoutParams();
    p.setMargins(0, 0, 50, 0);
    tab.requestLayout();
}

danielgomezrico avatar Jul 23 '20 21:07 danielgomezrico

The workaround you suggested doesn't seem hacky to me.

I'm confused about your proposal, are you suggesting add an attribute named tab_margin_start that is applied to every tab item's view?

wcshi avatar Jul 30 '20 19:07 wcshi

Im worried about (ViewGroup) mTabLayout.getChildAt(0) that may change in some android version and then break the feature.

An attribute like tab_margin_start may work, or tab_separation, or something make it from the xml and not only moving the internal views of the tabs.

danielgomezrico avatar Jul 30 '20 19:07 danielgomezrico

The workaround you suggested doesn't seem hacky to me.

I'm confused about your proposal, are you suggesting add an attribute named tab_margin_start that is applied to every tab item's view?

Hello, setting a custom indentation for each tab would be helpful. For example, in some cases, instead of using the menu, it might be helpful to create another tab and set it apart from the others.

If I use my own layout for the tab and use padding or margin inside the layout, then I will get an indicator shift (please, see a picture)

TabLayout ex

kirkaDev avatar Aug 13 '21 07:08 kirkaDev

val tabs = binding.tabFilter.getChildAt(0) as ViewGroup for (i in 0 until tabs.childCount) { val tab = tabs.getChildAt(i) val layoutParams = tab.layoutParams as LinearLayout.LayoutParams layoutParams.marginEnd = 13.dpToPx tab.layoutParams = layoutParams binding.tabFilter.requestLayout() }

lapdv3012 avatar Jul 21 '22 04:07 lapdv3012

I encounterred same situation when I want to have margin between tablayout items. One possible solution is setting custom view for Tab, but it require some extra effort to bind to the text and image. It's good if we could have some attribute support that spacing.

dung-ngviet avatar Oct 05 '23 06:10 dung-ngviet

val tabs = binding.tabFilter.getChildAt(0) as ViewGroup for (i in 0 until tabs.childCount) { val tab = tabs.getChildAt(i) val layoutParams = tab.layoutParams as LinearLayout.LayoutParams layoutParams.marginEnd = 13.dpToPx tab.layoutParams = layoutParams binding.tabFilter.requestLayout() }

Đinh Văn Lập

Vào Th 5, 5 thg 10, 2023 vào lúc 13:42 Dung Nguyen < @.***> đã viết:

I encounterred same situation when I want to have margin between tablayout items. One possible solution is setting custom view for Tab, but it require some extra effort to bind to the text and image. It's good if we could have some attribute support that spacing.

— Reply to this email directly, view it on GitHub https://github.com/material-components/material-components-android/issues/1507#issuecomment-1748186602, or unsubscribe https://github.com/notifications/unsubscribe-auth/AGDSVAWHR3ZOLDZBSQDLJODX5ZJHBAVCNFSM4PGEX24KU5DIOJSWCZC7NNSXTN2JONZXKZKDN5WW2ZLOOQ5TCNZUHAYTQNRWGAZA . You are receiving this because you commented.Message ID: <material-components/material-components-android/issues/1507/1748186602@ github.com>

lapdv3012 avatar Oct 05 '23 10:10 lapdv3012