Android Bottom Navigation View long press is not being called after first menu item selection

104 views Asked by At

I was trying to disable the tooltip popping up when you long-press a bottom navigation view item. Here is the sample view of the default behavior of the nav view when you long press:

Tooltip Sample

I know that question was asked several times on SO but what I encountered is a bit different and the proposed solutions did not work in my case.

Referring to the similar issue mentioned in the GitHub page of the material components, please see Material-Component Issue, I tested the following two approaches on API 24 and API 30.

Approach 1: Overwriting Long Click Listener

binding.bottomNavView.menu.forEach { menuItem ->
    findViewById<NavigationBarItemView>(menuItem.itemId).setOnLongClickListener {
        Toast.makeText(this, "Long click listener", Toast.LENGTH_SHORT).show()
        true
    }
}

Here, I try to take control of the long press listener so that it cannot show the tooltip. For the API 30, it works as expected and everything is fine. However, on API 24, it only works at first initialization, and the listener is totally ignored after the first item selection as also mentioned on the Github issue I shared above.

Here is the gif for the API 24:

API 24 Long Click Listener

As you can see, it works as intended before I select an item, but then it gets broken after the first pick.

Here is how it looks on API 30

API 30 Long Click Listener

As can be seen on the gif, long click listener still works even if I select an item.

I didn't get a chance to test on all API levels, but seems like it is an issue for devices < 26

Approach 2: Setting Tooltip Text Manually

binding.bottomNavView.menu.forEach { menuItem ->
    TooltipCompat.setTooltipText(findViewById<NavigationBarItemView>(menuItem.itemId), null)
}

Regardless of the API level, this approach just works until I select an item.

Here is the gif

Set Tooltip Text

Out of context, I wondered why this approach behaves like that and deep dived the source code of the menu item view to catch the reason behind it. Apparently, each and every time you select the nav menu item, updateMenuView is being called and causing the menu item to be initialized again.

Here is the source code for the initialize:

NavigationBarItemView-> initialize

By default, this function sets the tooltip by the title when it is null. Since my written code is being called after the first initialization, it is working fine for the first run, however, it is getting broken after the first selection since this initialization is being called but my code is not.

Also, see the stacktrace when I select an item:

Stacktrace

To overcome this issue, I tried to set the tooltip text on setOnItemSelectedListener callback, but this one also seems like being called before the nav view update item view call, therefore, again, it is overwritten by the source code.

Any thoughts on this matter are appreciated!

0

There are 0 answers