Long-click Android ActionBar Options

Android Can anyone find out about the parameters of the Actionbar element by a long press, I want to show the text in LongClick in the menu of the action bar menu, for example, a hint for a long press of the actionBar long key

+4
source share
5 answers

Do you want a long press on a menu item in the action bar? As for me, after detecting 2.3 hours, I found this solution. This works great for me.

@Override public boolean onCreateOptionsMenu(final Menu menu) { getMenuInflater().inflate(R.menu.menu, menu); new Handler().post(new Runnable() { @Override public void run() { final View v = findViewById(R.id.action_settings); if (v != null) { v.setOnLongClickListener(new CustomLongOnClickListener()); } } }); return true; } 
+11
source

user1206890, you do not need to listen to the long click event. If you want to show a hint of actions, it will be enough to set the title in the menu add. Tested on 2.3 and 4.0.

+5
source

For me, the following approach is great for new versions of Android - I tested it using Android 4.2 and Android 5.0.1.

The idea is that I will replace the appearance of the action icon with a custom view. Here I have to handle one click, and I can handle a long click.

If I want the look to be exactly like the usual action bar icons, the following is done.

First create a layout containing only ImageButton with your icon.

 <?xml version="1.0" encoding="utf-8"?> <ImageButton xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/myButton" style="?android:attr/actionButtonStyle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="@layout/text_view_initializing" android:src="@drawable/ic_action_plus" /> 

Then put this ImageButton in the action bar and attach listeners to it.

 MenuItem myItem = menu.findItem(R.id.my_action); myItem.setActionView(R.layout.my_image_button); myItem.getActionView().setOnClickListener(new OnClickListener() { @Override public void onClick(final View v) { // here, I have to put the stuff that normally goes in onOptionItemSelected } }); myItem.getActionView().setOnLongClickListener(new OnLongClickListener() { @Override public boolean onLongClick(final View v) { // here, I put the long click stuff } }); 

Important note: this will only work if the item appears in the action bar. Thus, everything that you want to do with a long press will not be available this way if an option appears in the drop-down menu.

+5
source

If you create your own action view via android:actionLayout , you can configure listeners to their own widgets for long-click events. You do not have access to widgets in the action bar that you yourself do not create.

+4
source

I think "findViewById" is the easiest way to find. Just do

 View action_example = findViewById(R.id.action_example); if(action_example!=null)action_example.setOnLongClickListener( new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { Toast.makeText(MainActivity.this, "action_example", Toast.LENGTH_SHORT).show(); return true; } } ); 
0
source

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


All Articles