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
source share