Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show affordance/hover/tooltip on a long-clicked View

I would like to show a tooltip, i.e. additional non-essential information about a View when the user long-clicks on it.

The two options I see in front of me are using an OnLongClickListener to construct a custom tooltip in front of the clicked View; or abusing an OnCreateContextMenuListener to create a context menu that isn't.

Neither seems like the best way to go about things, and I'm not sure whether either will work. I've scoured the web and haven't found any hints. Any alternatives, or should I be wet-fish-slapped for trying to do this? Thanks!

like image 571
Cheezmeister Avatar asked Feb 19 '11 00:02

Cheezmeister


1 Answers

Android Oreo has introduced the android:tooltipText attribute in order to display a simple Toast-like tooltip when user long-presses on a View:

<Button
    // ...
    android:tooltipText="@string/share_button_tooltip"/>

Although it has been introduced in API 26, you can still use it through the Support Library's TooltipCompat helper class:

TooltipCompat.setTooltipText(shareButton, getString(R.string.share_button_tooltip))

My suggestion is to set android:contentDescription and then use it as the tooltip text to kill 2 stones with 1 bird:

<Button
    // ...
    android:contentDescription="@string/share_button_tooltip"/>

TooltipCompat.setTooltipText(shareButton, shareButton.getContentDescription())
like image 91
Egor Neliuba Avatar answered Sep 20 '22 09:09

Egor Neliuba