8

I have an ImageButton, when I did some test for my app, I found that I could not show the tooltipText in my Xiaomi Note 4 with Android N device. But it shows up perfectly fine in my Samsung S8 with Android O, any idea why this can happen and how to mitigate this? The minimum reproducible code is really simple, just create a basic android project in android studio and replace the default HelloWorld Textview to

<ImageButton
        android:layout_width="36dp"
        android:layout_height="36dp"
        android:tooltipText="Show tooltip"
        android:src="@mipmap/ic_launcher"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

I think there must be some way I can make it work because the auto generated toolbar obviously has tooltip. So there must be something I'm missing to make it work in Xiaomi device.

Bertram Gilfoyle
  • 9,899
  • 6
  • 42
  • 67
litaoshen
  • 1,762
  • 1
  • 20
  • 36

1 Answers1

19

setTooltipText (CharSequence tooltipText) is added in added in API level 26. To use it in devices with API level prior 26, you can use TooltipCompat#setTooltipText(View view, CharSequence tooltipText) from v7 support library.

To use it, add this in gradle

    implementation "com.android.support:appcompat-v7:27.1.1"

Example:


`TooltipCompat.setTooltipText(view, "Tooltip text");`
Divya Gupta
  • 494
  • 8
  • 24
Bertram Gilfoyle
  • 9,899
  • 6
  • 42
  • 67
  • 1
    It works! But I'm wondering if we can just do something in the xml? Or we have to use `TooltipCompat` to set manually? – litaoshen Aug 20 '18 at 19:40
  • Wondering how the toolbar gets the tooltips? It looks like it is not set in xml nor code. – litaoshen Aug 20 '18 at 19:43
  • "how the toolbar gets the tooltips?" : I don't understand. You are setting it to ImageButton, right? – Bertram Gilfoyle Aug 21 '18 at 08:09
  • Yes, but I noticed that the autogenerated toolbar has tooltips. So I was wondering how did the toolbar gets the tooltips. I could not see it in the xml or toolbar class – litaoshen Aug 21 '18 at 12:55
  • 3
    I think you mean `ActionBar` by "autogenerated toolbar". In that case, it's the icons (items) comes with tooltip text, not the `ActionBar` itself. And that text is taken from the `android:title` attribute of the menu. – Bertram Gilfoyle Aug 21 '18 at 13:17