6

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!

Cheezmeister
  • 4,895
  • 3
  • 31
  • 37
  • It seems to me OnLongClickListener is exactly what you want. – FoamyGuy Feb 19 '11 at 00: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 Feb 19 '11 at 00: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 :) – Cheezmeister Feb 20 '11 at 21:28

1 Answers1

11

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())
Egor Neliuba
  • 14,784
  • 7
  • 59
  • 77