How to add a tooltip to a button in Android

How to add a tooltip to a Button in Android:

Tooltip is a helper text that appears near a view when user long press on it. This is available for API 26 and higher, or Android 8.0 and higher.

In this post, I will show you how to add tooltip to a Button.

Adding a tooltip:

We can add a tooltip directly in the xml file or dynamically. To add it in the xml file, android:tooltipText is used. We can provide one string for this. Also, we can change it using tooltipText property.

For example,

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:tooltipText="Dummy Button"
    android:text="Click Me"/>
</FrameLayout>

Here, I added a tooltip text to the Button.

It will show a tooltip if you long press on the button.

Android tooltip example

You might also like: