Tooltip for Android Action Custom ActionsBar

I have an application that uses a SupportActionBar. I use custom view for action.

The thing is, actions by default display a hint when I press for a long time, but my custom action does not work.

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

And here is my custom action (without a hint for this: /): enter image description here

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 anyone help?

+6
source share
6 answers

I donโ€™t think there are โ€œofficialโ€ API requests for this. I believe adding the View.OnClickListener to your custom view, as the other answers indicate, is about as good as possible.

What you can do a little better, however, correctly calculates the position for a toast for clues. I would recommend simply copying and pasting the corresponding fragment from the ActionMenuItemView class (from the source code of the support library), since it deals with several 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 .

+9
source

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

 TooltipCompat.setTooltipText(view, "..."); 
+1
source

Since you are 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 listen button for a long click on the action bar.

0
source

Try adding the cText value to the showAsAction function. Something like that

 android:showAsAction="ifRoom|always|withText" 
0
source

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

One change in your menu xml file that adds the line android:actionViewClass="android.widget.ImageButton" (you can change this View ):

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

Can you get an error here to get the parent View element? I think this should be your main view of the custom layout. you can use it first, after using actionViewClass

Now enter onCreateOptionMenu() for the 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 a toast

0
source

Create your own layout and set this layout in the action bar, I hope it can 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 this as

  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; } }); 
0
source

Source: https://habr.com/ru/post/977534/


All Articles