3

I imagine someone has had this question before, I just don't quite know what the right keywords are to find the answer? I am making an android app with an activity that includes tabs using TabLayout. Nothing fancy, just really standard stuff. In fact, so far I've done literally nothing but make a completely new application with a single tabbed activity using the auto-generated code from Android Studio. Everything works fine, but there is one feature I cannot figure out how to turn off -- when I long click on any tab, a little rectangular alt text or something with the title of the tab pops up on screen just above the tab. It's not the end of the world if I can't eliminate it, I just find it to be irritating and incompatible with the overall desired feel of my app given that it's literally just duplicating the tab title. I can't find any code that is causing this to appear, so I don't know how to delete it. The picture below shows what I'm talking about circled in red.

If anyone needs me to post code to help answer, I can... but you can also just make a new tabbed activity in a throwaway application in Android Studio and get exactly the same boilerplate code I have.

annoying tab title popup

Edit: I added the term "tooltip" to the title so others can find the relevant thread more easily if they have the same problem.

  • 5
    Those are tooltips, and unfortunately it looks like `TabLayout` doesn't offer any method specifically to disable them. You could try doing it manually – e.g., `for (int i = 0; i < tabLayout.getTabCount(); i++) TooltipCompat.setTooltipText( tabLayout.getTabAt(i).view, null);` – but it looks like they're reset pretty much any time the `TabLayout` is updated, so you'd have to take care with that. (Also, that `view` field is marked to be hidden eventually, but it's been like that for years, and there are other options if they ever do that.) – Mike M. Oct 22 '21 at 01:51
  • 1
    Yessss this worked! Thank you, Mike! You are correct that the any updates to the TabLayout result in a reset of the tooltip text value. I put your suggested for loop in the onTabSelectedListener. Now, any time any tab is selected (ie tablayout is updated), all of them have their tooltips turned off. It's not elegant, but it sure gets the job done. – David Jantz Oct 22 '21 at 02:35

2 Answers2

1

Kudos to Mike M. for the answer, shown in comments above. I implemented it successfully, so if anyone comes back here looking for the answer, here's the successful java code, which is placed in the onCreate() method of the activity containing the tabLayout:

    // turn off that tooltip text thing immediately on activity creation
    for (int i=0; i<tabs.getTabCount(); i++) {
        TooltipCompat.setTooltipText(Objects.requireNonNull(tabs.getTabAt(i)).view, null);
    }

    tabs.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            int tabPosition = tab.getPosition(); // syntactic sugar
            viewPager2.setCurrentItem(tabPosition, true);

            // Repeat of the code above -- tooltips reset themselves after any tab relayout, so I
            // have to constantly keep turning them off again.
            for (int i=0; i<tabs.getTabCount(); i++) {
                TooltipCompat.setTooltipText(Objects.requireNonNull(tabs.getTabAt(i)).view, null);
            }
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {

        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {

        }
    });
1

Extending Mike M.'s answer, if you use ViewPager2 and set your tabs text using TabLayoutMediator, than all you need to do is to remove tooltip text while setting tab text in your onCreate.

Here's example in Kotlin:

TabLayoutMediator(binding.tabLayout, binding.viewPager2) { tab, position ->
    // setting tab text
    tab.text = getString(when (position) {
        0 -> R.string.tab_text_1
        1 -> R.string.tab_text_2
        // ...
        else -> R.string.tab_text_n
    })

    // "removing" tooltip
    TooltipCompat.setTooltipText(tab.view, null)
}.attach()

It's a bit more elegant solution in my opinion rather than looping every time user selects a tab