Android Can anyone know about Actionbar item options long click , I want to show text on LongClick on actionbar menu option like a hint on long press of actionBar long press
6 Answers
Do you want to capture long press on menu item on action bar? As for me, after finding 2,3 hour, I found this solution. This is perfectly work 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;
}

- 219
- 4
- 8
-
but first time its working fine.second time when we clik on menu item it shows same empty toast message – Pinki Jul 01 '14 at 07:25
-
1@ YeeKhin . Thanks a lot . – Oct 04 '15 at 07:21
-
but findviewbyid always return null, why? – hyyou2010 Jul 26 '17 at 07:16
For me, the following approach works fine for newer Android versions - I tested it with Android 4.2 and Android 5.0.1.
The idea is that I replace the action icon view by a custom view. Here, I have to handle the single click, and I can handle the long click.
If I want the appearance to be exactly like normal action bar icons, the following works.
First, create a layout containing just an 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 into 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 remark: this will only work if the item appears in the action bar. So, whatever you want to do on long click will not be accessible in this way if the option appears in the menu dropdown.

- 1,299
- 13
- 7
user1206890, you do not need listen long click event. If you want show action hint, will be sufficient set title in menu add. Checked on 2.3 and 4.0.

- 5,134
- 7
- 34
- 57
If you create your own action view via android:actionLayout
, you are welcome to set up listeners on your own widgets for long-click events. You do not have access to widgets in the action bar that you do not create yourself.

- 986,068
- 189
- 2,389
- 2,491
-
And then place Toast where you want, according to http://stackoverflow.com/a/21026866/630833, which takes size of Toast into consideration, which was helpful. – jayeffkay Nov 06 '14 at 11:50
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;
}
}
);

- 21
- 2
This is the code of function that works with me thanks to @YeeKhin change "main" to your menu name and "action_refresh" to your action name and "Activity" to your activity name
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
new Handler().post(new Runnable() {
@Override
public void run() {
final View v = findViewById(R.id.action_refresh);
if (v != null) {
v.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
Toast.makeText(Activity.this,"Long Press!!",Toast.LENGTH_LONG).show();
return false;
}
});
}
}
});
return true;
}

- 11
- 2