7

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!

3
  • It seems to me OnLongClickListener is exactly what you want.
    – FoamyGuy
    Commented Feb 19, 2011 at 0:18
  • A. Tooltip's don't really fit in smart phones. Screen space is already so limited. A tooltip is going to take up 1/2 your screen, so you might as well use another activity or a dialog box. B. Users don't expect a tooltip. They expect a chooser when they long-click. I would take a step back and evaluate your design decision.
    – user432209
    Commented Feb 19, 2011 at 0:20
  • Hey, @user4, it most certainly does not take up half the screen! See my reply to Mark below. Point taken though, I'll find an alternative :) Commented Feb 20, 2011 at 21:28

1 Answer 1

12

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.