Material-components-android: [TabLayout] set tag for TabItem in xml is useless

Created on 16 Jun 2020  路  5Comments  路  Source: material-components/material-components-android

Description: I want to index a tabitem with the attribute tag when use tablayout, but I can't get that tag runtime 'cause you guys ignored the tag when add a new tab into tablayout.

Source code:
//The tab should be add the tag from tabItem
private void addTabFromItemView(@NonNull TabItem item) {
final Tab tab = newTab();
if (item.text != null) {
tab.setText(item.text);
}
if (item.icon != null) {
tab.setIcon(item.icon);
}
if (item.customLayout != 0) {
tab.setCustomView(item.customLayout);
}
if (!TextUtils.isEmpty(item.getContentDescription())) {
tab.setContentDescription(item.getContentDescription());
}
addTab(tab);
}

Material Library version:1.1.0

bug

Most helpful comment

I agree with @zilchzz. It鈥檚 infuriating that you can鈥檛 identify a tab in code by anything from the XML.

The only way to identify a selected tab is by its position so when I reorder the tabs in XML, I have to remember to update the OnTabSelectedListener, too.

An identifier from TabItem should be passed on to the TabLayout.Tab so you can see it in onTabSelected.

Some kind of tab ID would be great, instead of plain int or string:

<com.google.android.material.tabs.TabItem
  android:tab_id="cats"
  // ...
/>

<com.google.android.material.tabs.TabItem
  android:tab_id="dogs"
  // ...
/>
override fun onTabSelected(tab: TabLayout.Tab?) {
  when (tab?.id) {
    R.tab.cats -> showCats()
    R.tab.dogs -> showDogs()
  }

All 5 comments

If you check the documentation of TabItem which you can find here.
they explicitly say that the TabItem is not a real view, and it is not added as a child of TabLayout like a ViewGroup would do.
So you can't use it for set and retrieve a tag and you can't use it like a regular view.

If you check the documentation of TabItem which you can find here.
they explicitly say that the TabItem is not a real view, and it is not added as a child of TabLayout like a ViewGroup would do.
So you can't use it for set and retrieve a tag and you can't use it like a regular view.

Thank you for reply anyway.
When I read the source code, I have found that TabItem is not a regular view.
In that case, how can I index a Tab within TabLayout.
I can only do that with hardcode in activity or fragment, which not so convenient.
It's there any good way for that?

Why you would need to get the index of a Tab?
If you create them programmatically, you probably need to use the index of the list you use for creating the TabItems

Why you would need to get the index of a Tab?
If you create them programmatically, you probably need to use the index of the list you use for creating the TabItems

I create them in the xml file , just like the code below:
android:layout_width="wrap_content"
android:layout_height="35dp">

    <com.google.android.material.tabs.TabItem
        android:layout_width="wrap_content"
        android:layout_height="match_parent"/>

    <com.google.android.material.tabs.TabItem
        android:layout_width="wrap_content"
        android:layout_height="match_parent"" />

    <com.google.android.material.tabs.TabItem
        android:layout_width="wrap_content"
        android:layout_height="match_parent" />

    <com.google.android.material.tabs.TabItem
        android:layout_width="wrap_content"
        android:layout_height="match_parent" />

</com.google.android.material.tabs.TabLayout>

In that case, attribute tag is useful.

I agree with @zilchzz. It鈥檚 infuriating that you can鈥檛 identify a tab in code by anything from the XML.

The only way to identify a selected tab is by its position so when I reorder the tabs in XML, I have to remember to update the OnTabSelectedListener, too.

An identifier from TabItem should be passed on to the TabLayout.Tab so you can see it in onTabSelected.

Some kind of tab ID would be great, instead of plain int or string:

<com.google.android.material.tabs.TabItem
  android:tab_id="cats"
  // ...
/>

<com.google.android.material.tabs.TabItem
  android:tab_id="dogs"
  // ...
/>
override fun onTabSelected(tab: TabLayout.Tab?) {
  when (tab?.id) {
    R.tab.cats -> showCats()
    R.tab.dogs -> showDogs()
  }
Was this page helpful?
0 / 5 - 0 ratings