Android ActionBar custom action view tool-tip
Asked Answered
W

6

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?

Wilber answered 31/10, 2014 at 10:38 Comment(3)
You got answer or not? @WilberMisquote
I'll post an update as soon as i get time to check this out ;) Priorities you know :DWilber
see my answer, its easy for implement and its work my endGreengrocer
S
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.

Salpingectomy answered 8/11, 2014 at 22:1 Comment(0)
K
7

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

TooltipCompat.setTooltipText(view, "...");
Kweisui answered 24/9, 2017 at 11:11 Comment(2)
Available in API 8+ onlyLandry
This should be the accepted answer. Works also with androidx.appcompat.widget.TooltipCompat and on api 26 and above we could simply do view.setTooltip("....")Dispread
G
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.

Gabe answered 3/11, 2014 at 9:53 Comment(0)
M
0

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

android:showAsAction="ifRoom|always|withText"
Methylamine answered 3/11, 2014 at 10:4 Comment(0)
M
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

Misquote answered 3/11, 2014 at 10:30 Comment(5)
Looks a bit hacky. And why do i need android:actionViewClass?Wilber
Because it will help you to get as any View to control your onLongPress click event.Misquote
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);.Wilber
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.Wilber
Its okay, No problem but you can do that vibration with drawable of image with selector.Misquote
G
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;
                   }
              });
Greengrocer answered 7/11, 2014 at 11:25 Comment(2)
Hi! But what this has to do with the tooltip?Wilber
My weekend is started so I reply Monday if it is possible with tooltip and This answer is also solution for we question. Try thisGreengrocer

© 2022 - 2024 — McMap. All rights reserved.