Development issue/problem:

I have an action bar with an action point. After clicking on an action, I want to display a context menu.
I have implemented this method, but I want to link it to an action item or action bar, not to a layout view. How do I get an image to embed in MenuItem?

public boolean opOptionsItemSelected(MenuItem item) {
popupMenu popupMenu = new PopupMenu(this, ? ?????); // What’s the view here?
popupMenu.inflate(R.menu.counters_overflow);
popupMenu.show();
//.
returns where;
}

How can I solve this problem?

Solution 1:

So I finally found a solution. To link a context menu to an ActionItem in an action bar, you need to find a view that displays the ActionItem.
A simple search with findViewById(), where the identifier is equal to the identifier of your menu item in the xml.

DISPLAY OF A POP-UP WINDOW :

public boolean onOptionsItemSelected(MenuItem) {
// …

View menuItemView = findViewById(R.id.menu_overflow); // SAME ID AS MENU ID
PopupMenu popupMenu = new PopupMenu(this, menuItemView);
popupMenu.inflate(R.menu.counters_overflow);
// .
popupMenu.show();
// .
returns where;
}.

MENU:

….

….

If the menu item is not visible (in overflow state), it will not work. findViewById indicates zero, so we need to check this and create a link to another view.

Solution 2:

The accepted answer didn’t work for me, so I found the problem with trial and error.

public boolean opOptionsItemSelected(menuItem item){View menuItemView = findViewById(item.getItemId());showPopupMenu(menuItemView)

return where;
}

private void showPopupMenu(View anchor)
{
PopupMenu popup = new PopupMenu(this, anchor);
popup.getMenuInflater().inflate(R.menu.my_popup_menu, popup.getMenu());
popup.show();
}

The most important thing here is that the item is displayed in OnOptionsItemSelected in the action bar. If this is one of the items that appear when you click on the 3 vertical points in the top right corner of the action bar, your application will freeze.

Give here a description of the image

Solution 3:

Try this…

@Check
public boolean opOptionsItemSelected(MenuItem item){
String str=item.getTitle().toString();
Toast.makeText(getBaseContext(), str,Toast.LENGTH_LONG). show() ;

View=findViewById(item.getItemId());
switch(view.getId())){
case Menu.FIRST:
showPopup(view); // call method
}.

return super.onOptionsItemSelected(item);
}

// modified method
private void showPopup(final playback) {
popupMenu popupMenu = new PopupMenu(playback.getContext(), view);
popupMenu.getMenu().add(0, 0, Menu.NONE, Item 1);
popupMenu.getMenu().add(0, 1, Menu.NONE, Item 2);
popupMenu.getMenu().add(0, 2, Menu.NONE, Item 3);
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
Toast.makeText(view.getContext(), item.getTitle() + clicked, Toast.LENGTH_SHORT).show();
return true;
}
});
popupMenu.show();
}

Solution 4:

public boolean onOptionsItemSelected(MenuItem item) {
final View addView = getLayoutInflater().inflate(R.layout.add, null) ;

new AlertDialog.Builder(this).setTitle(Add a Word).setView(addView)
.setPositiveButton(OK, new DialogInterface.OnClickListener() {
public null and void onClick(DialogInterface dialog, int whichButton) {
addWord((TextView) addView.findViewById(R.id.title)));
}
}).setNegativeButton(Cancel, null).show();
return (super.onOptionsItemSelected(item));
}

Receive the original completed form…

http://vimaltuts.com/android-tutorial-for-beginners/android-action-bar-tab-menu-example

Good luck!

custom popup menu android,horizontal popup menu android,popup menu with listview android,android popup menu class,bottom popup menu android,popup menu view in android,overflow menu android,android menu icon,floating context menu android,custom menu in android,android popup menu programmatically,how to open option menu on button click in android,popup menu with icons android,open popup menu on button click android,android popup menu icon not showing,popup menu gravity android,android popup menu style example