Reputation: 51
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
Upvotes: 5
Views: 11407
Reputation: 46
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;
}
Upvotes: 1
Reputation: 21
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;
}
}
);
Upvotes: 1
Reputation: 1319
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.
Upvotes: 6
Reputation: 219
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;
}
Upvotes: 10
Reputation: 5364
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.
Upvotes: 5
Reputation: 1007554
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.
Upvotes: 4