6

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?

Arthur
  • 1,433
  • 1
  • 18
  • 35

6 Answers6

9

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.

matiash
  • 54,791
  • 16
  • 125
  • 154
7

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

TooltipCompat.setTooltipText(view, "...");
drakeet
  • 2,685
  • 1
  • 23
  • 30
0

As you're using a custom layout for the button, the following code should work:

view.setOnLongClickListener(new OnLongClickListener() {
    public boolean onLongClick(View view) {
        Toast toast = Toast.makeText(v.getContext(), "Open chat", Toast.LENGTH_SHORT);
        toast.show();
        return true;
    }
}

Just set this as the long click listener of the button in the action bar.

rymate1234
  • 448
  • 4
  • 16
0

Try adding the value withText to the showAsAction feature. Something like this

android:showAsAction="ifRoom|always|withText"
migue02
  • 130
  • 12
0

Use the findItem method on Menu to get your views, and set your OnLongClickListener on your view.

One change in your xml file of Menu that is add line android:actionViewClass="android.widget.ImageButton" (you can change that View):

<item
     android:title="Open chat"
     android:id="@+id/action_chat"
     custom:showAsAction="always"
     android:actionViewClass="android.widget.ImageButton"
     custom:actionLayout="@layout/ab_chat" />

May you get error here for getting your Parent View I think it should be you Main View of custom layout. you can use it first then after use actionViewClass

Now get in onCreateOptionMenu() for Long Press Event:

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // TODO Auto-generated method stub
        getSupportMenuInflater().inflate(R.menu.menu_xml, menu);
        /*
        As per your defined view in XML of MENU
        */ 
        ImageButton view = (ImageButton) menu.findItem(R.id.action_chat).getActionView();
        view.setOnLongClickListener(new OnLongClickListener() {

            @Override
            public boolean onLongClick(View arg0) {
                // TODO Auto-generated method stub
                Display display = getWindowManager().getDefaultDisplay();
                Point size = new Point();
                display.getSize(size);
                int width = size.x;
                Toast toast = Toast.makeText(context, "Open Chat", Toast.LENGTH_SHORT);
                toast.setGravity(Gravity.TOP, width - 200, 50); //your width and height
                toast.show();
                return false;
            }
        });

        return super.onCreateOptionsMenu(menu);
    }

Display Toast with Gravity on Top

You can see: Playing with Toast

Pratik Butani
  • 60,504
  • 58
  • 273
  • 437
  • Looks a bit hacky. And why do i need `android:actionViewClass`? – Arthur Nov 05 '14 at 10:26
  • Because it will help you to get as any `View` to control your `onLongPress` click event. – Pratik Butani Nov 05 '14 at 11:50
  • Well, actually you don't need `androidLactionViewClass` to get to it. `MenuItem item_chat = menu.findItem(R.id.action_chat);` is working without that. And then i can just put a `onLongPress` listener on the action view itself `View item_chat_action_view = MenuItemCompat.getActionView(item_chat);`. – Arthur Nov 05 '14 at 12:39
  • I will wait some more to see if anyone comes up with a more official answer since there is more to that tool-tip than just placing a toast at fixed coordinates (the exact position of the item + vibration when the tooltip is shown). Maybe there is an API call for this. If there will be no better answer, i will accept yours. – Arthur Nov 05 '14 at 12:43
  • Its okay, No problem but you can do that vibration with drawable of image with selector. – Pratik Butani Nov 05 '14 at 12:45
0

Create a custom layout and set this layout on action bar, i hope its may work for you

View view     = getLayoutInflater().inflate(R.layout.actionbar_demo,null);
    btn   = (ImageView) view.findViewById(R.id.btn_action);
    btn.setOnLongClickListener(new OnLongClickListener()
    {

        @Override
        public boolean onLongClick(View v) {

            return true;
        }
    });



  getActionBar() . setCustomView(view);

and you also implement like this

   ActionBar actionBar = getSupportActionBar();
            actionBar.setCustomView(R.layoutcustom_action_bar);
            actionBar.setDisplayShowCustomEnabled(true);
            mLogo = (ImageView) actionBar.getCustomView().findViewById(
                    R.id.logo);
            mLogo.setOnLongClickListener(new OnLongClickListener()
              {

                  @Override
                  public boolean onLongClick(View v) {

                     return true;
                   }
              });
Lokesh
  • 3,247
  • 2
  • 33
  • 62