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

3.4k Views Asked by At

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!

1

There are 1 best solutions below

0
On

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())