material-components / material-components/material-components-android

[Tabs] Add a function to set the tooltip text for a Tab

Open
#3,627 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature request Widget: Tab
Dominant language
Java
Stars
17.4k
Forks
3.2k
PR merge metrics
No merged PRs in 30d

Description

**Is your feature request related to a problem? Please describe.**
The tooltip text for a tab can be set using `TooltipCompat.setTooltipText(tab.view, "Tooltip")`, however this text gets reset to the `Tab`'s text whenever the `Tab`'s layout changes and hence workarounds are required to change this text (listening to every `Tab`'s layout changes for example). The tooltip text may not always be the same as the `Tab`'s text, for example the text may be a short-form and the tooltip may want to display the full-form.

_Originally from https://github.com/material-components/material-components-android/issues/3621_

**Describe the solution you'd like**
A good solution to this would be to add a function (`TabLayout.Tab.setTooltipText()`) to set the tooltip text. If this function is not called then the tooltip text will fallback to the `Tab`'s text as it does currently.

**Describe alternatives you've considered**
As a workaround I've added a layout change listener to every single tab to set the tooltip text again.

```java
public static void mediateTabLayout(TabLayout tabLayout, ViewPager2 viewPager, String[] tabTexts, String[] tabDescriptions) {
new TabLayoutMediator(tabLayout, viewPager, (tab, position) -> {
tab.setText(tabTexts[position]);
tab.setContentDescription(tabDescriptions[position]);
TooltipCompat.setTooltipText(tab.view, tabDescriptions[position]);
}).attach();

for (int i = 0; i < tabLayout.getTabCount(); ++i) {
TabLayout.Tab tab = tabLayout.getTabAt(i);
int position = i;

if (tab == null) {
continue;
}

tab.view.addOnLayoutChangeListener((view, i0, i1, i2, i3, i4, i5, i6, i7) -> {
TooltipCompat.setTooltipText(view, tabDescriptions[position]);
});
}
}
```

**Additional context**
None.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the TabLayout.Tab API and trace how a tab's tooltip currently follows its text during layout changes. Add the requested tooltip-setting behavior with the stated fallback to the tab text, then verify that the custom tooltip remains correct after layout changes.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
mobile
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.