Tooltip is a simple popup message tip shown near to a view. It is similar to its web version that shown when you mouse over it. It is helpful for giving special or extra information about a UI element in the page. From API Level 26 Android introduces new property called android:tooltipText
through this we achieve Tooltip funtionality.
Prerequisites
API Level : 26 (Android 8.0) and higher
Implementation
Let’s see how to set the Tooltip for a view in the layout.
Method 1: Define It In Layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center"
android:text="Hello Devdeeds.com"
android:textColor="#000"
android:textSize="18sp" />
<android.support.v7.widget.AppCompatButton
android:id="@+id/next_button"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:text="Next"
android:textAllCaps="false"
android:textSize="16sp"
android:tooltipText="This will take you to next page" />
</RelativeLayout>
Method 1: Set It Programmatically
Let’s see how to set the Tooltip in the Activity Class.
import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.AppCompatButton; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); AppCompatButton btnNext = (AppCompatButton) findViewById(R.id.next_button); btnNext.setTooltipText("This will take you to next page"); } }
How To Get The Tooltip Property Value
The API has getTooltipText()
method by which you can get the value of the tooltipText property.