Arthur Arthur - 1 month ago 14
Android Question

Android ActionBar custom action view tool-tip

I have an app that is using a SupportActionBar. I am usin a custom view for an action.

The thing is, that default actions do display a tool-tip when i long press, but my custom action does not.

So here is the default action (as you can see, there is a tool-tip):
Action item with tool-tip

And here is my custom action (no tool-tip for this one :/):
enter image description here

The xml for these 2:


android:title="Clear history"
android:icon="@drawable/ic_action_trash" />
android:title="Open chat"
custom:actionLayout="@layout/ab_chat" />

Can someone help?

Answer Source

I don't think there are any "official" API calls for this. I believe adding anView.OnClickListener to your custom view, as the other answers suggest, is about as good as possible.

What you can do a little better, though, is properly calculating the position for the tool-tip toast. I would recommend simply copying and pasting the relevant snippet from the ActionMenuItemView class (from the support library source code) since it deals with a few special cases:

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();

    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);
    return true;

You can find it in <android-sdk>\sources\android-21\android\support\v7\internal\view\menu.