1

From the action bar documentation:

If the action item appears with only the icon, a user can long-press the item to reveal a tool-tip that displays the action item's title. The android:icon is always optional, but recommended.

But Android Menu item's tool-tip in the ToolBar doesn't work properly in my case.

This is what I have in styles.xml:

<style name="AppTheme.Base" parent="Theme.AppCompat.NoActionBar">
    <!-- Actionbar color -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <!--Status bar color-->
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <!--Window color-->
    <item name="android:windowBackground">@null</item>

    <!--drawerArrowStyle-->
    <item name="drawerArrowStyle">@style/DrawerArrowStyle</item>

    <!--Activity enter and exit animation-->
    <item name="android:windowAnimationStyle">@style/TranslateEnterExitAnimation</item>

    <!--<item name="colorAccent">#EC9290</item>-->
    <item name="android:autoCompleteTextViewStyle">@style/CursorColor</item>
</style>

And My ToolBar:

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary"
    android:minHeight="?attr/actionBarSize" />

And menu_feedback.xml

<item
    android:id="@+id/menu_item_action_send"
    android:title="@string/send_text"
    android:icon="@drawable/ic_actionbar_send"
    app:showAsAction="always"/>

I get this on HTC devices, PS: Nexus 5 is ok. enter image description here

But the normal toast is ok. enter image description here

So, is there an elegant solution to make the tool-tip work properly? thx.

Xiaozou
  • 1,655
  • 1
  • 15
  • 29

2 Answers2

0

I am not sure if you can override the default behavior with ease, but as for a work-around you can create your own items as hinted here, and then you will be able to set up listeners for you own items, and show toast under the items on long press events.

Community
  • 1
  • 1
Behnam
  • 6,510
  • 6
  • 35
  • 65
  • Yes, i also found the custom action view tool-tip, but i have many Activities with MenuItems, it's a lot of work in this way, thanks Campiador. – Xiaozou Jun 19 '15 at 08:11
0

Finally, i solved the problem as follows.

First, Customizing ActionMenuItemView which using

final Context context = getContext().getApplicationContext();

instead of

final Context context = getContext();

to show a Toast like below.

public class ActionMenuItemView extends android.support.v7.internal.view.menu.ActionMenuItemView{
public ActionMenuItemView(Context context) {
    super(context);
}

public ActionMenuItemView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public ActionMenuItemView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

@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().getApplicationContext();
    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, getItemData().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 false;
}}

Second, create abc_action_menu_item_layout.xml and place it in layout folder instead of abc_action_menu_item_layout.xml in support-v7 library.

<com.example.widget.ActionMenuItemView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:gravity="center"
    android:focusable="true"
    android:paddingTop="4dip"
    android:paddingBottom="4dip"
    android:paddingLeft="8dip"
    android:paddingRight="8dip"
    android:textAppearance="?attr/actionMenuTextAppearance"
    android:textColor="?attr/actionMenuTextColor"
    style="?attr/actionButtonStyle"/>

It works like a charm although i don't know the difference between getContext().getApplicationContext() and getContext() in the ActionMenuItemView.

Xiaozou
  • 1,655
  • 1
  • 15
  • 29