android image button tooltip android image button tooltip xml xml

android image button tooltip


With api level 26 you can use the built-in TooltipText:Via XML:

android:toolTipText="yourText"

ViewCompatDocs

If your minSdk is below 26, use ToolTipCompat, for example:

TooltipCompat.setTooltipText(yourView, "your String");

Unfortunately, there's is no way to do this in your XML-File.


This is the code that Support Library v7 uses to show "cheat sheet"s for action menu items:

public boolean onLongClick(View v) {    if (hasText()) {        // Don't show the cheat sheet for items that already show text.        return false;    }    final int[] screenPos = new int[2];    final Rect displayFrame = new Rect();    getLocationOnScreen(screenPos);    getWindowVisibleDisplayFrame(displayFrame);    final Context context = getContext();    final int width = getWidth();    final int height = getHeight();    final int midy = screenPos[1] + height / 2;    int referenceX = screenPos[0] + width / 2;    if (ViewCompat.getLayoutDirection(v) == ViewCompat.LAYOUT_DIRECTION_LTR) {        final int screenWidth = context.getResources().getDisplayMetrics().widthPixels;        referenceX = screenWidth - referenceX; // mirror    }    Toast cheatSheet = Toast.makeText(context, mItemData.getTitle(), Toast.LENGTH_SHORT);    if (midy < displayFrame.height()) {        // Show along the top; follow action buttons        cheatSheet.setGravity(Gravity.TOP | GravityCompat.END, referenceX, height);    } else {        // Show along the bottom center        cheatSheet.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, height);    }    cheatSheet.show();    return true;}


Not exactly what you are looking for but it does something very similar.

1) Insure your view is android:longClickable="true" (should be by default) and has a defined content description android:contentDescription="@string/myText":

<ImageButton android:id="@+id/button_edit"     android:contentDescription="@string/myText"     android:src="@drawable/ic_action_edit"     android:onClick="onEdit"     android:longClickable="true"/>

2) Register a callback to be invoked when the view is long pressed. Handler will display the content description as a toast message.

findViewById(R.id.button_edit).setOnLongClickListener(new View.OnLongClickListener() {            @Override            public boolean onLongClick(View view) {                Toast.makeText(context,view.getContentDescription(), Toast.LENGTH_SHORT).show();                return true;            }        });