Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

    <menu
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:custom="http://schemas.android.com/apk/res-auto">

        <item
            android:visible="false"
            android:title="Clear history"
            android:id="@+id/action_clear_history"
            custom:showAsAction="always"
            android:icon="@drawable/ic_action_trash" />
        <item
            android:title="Open chat"
            android:id="@+id/action_chat"
            custom:showAsAction="always"
            custom:actionLayout="@layout/ab_chat" />
    </menu>

Can someone help?

like image 969
Arthur Avatar asked Oct 31 '14 10:10

Arthur


2 Answers

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:

@Override
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;
}

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

like image 155
matiash Avatar answered Sep 20 '22 01:09

matiash


Easy and perfect way, just use android.support.v7.widget.TooltipCompat:

TooltipCompat.setTooltipText(view, "...");
like image 21
drakeet Avatar answered Sep 18 '22 01:09

drakeet