How to Open the Options Menu Programmatically

Android: Open menu programmatically

Use:

MainActivity.this.openOptionsMenu();

Then, check your toolbar if is this okay.

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.acercade_activity);

toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
// MenuInflater inflater = getMenuInflater();
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
//case R.id.action_settings:
// return true;

case R.id.perfil:
drawerLayout.openDrawer(Gravity.LEFT);
return true;
default:
return super.onOptionsItemSelected(item);

}

}

Open (overflow) menu programmatically

If you're using the new Toolbar class of the Support Library, do this:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.showOverflowMenu();

Android code for getting Options Menu programmatically?

I was able to open the menu by hooking the onAttachedToWindow() event. This would fire automatically after the view was created. I used the following code:

@Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
try {
((Activity) this).openOptionsMenu();
} catch (Exception ex) {
Log.e("ERR", "Error: " + ex.getMessage());
}
}

When I attempted to open the Option Menu in the "onCreate(...)" or "onPostCreate(...)" events, I received the following error: "Unable to add window -- token null is not valid; is your activity running?" It seems that the Option Menu is not available until the view is being drawn and registered to the Window.

As an aside, it may be preferable to open the Options Menu by hooking the "onTouchEvent(Motion event)" as in:

@Override
public boolean onTouchEvent(MotionEvent event) {
((Activity) this).openOptionsMenu();
return super.onTouchEvent(event);
}

This way, the Options Menu is displayed if the user taps or swipes the activity. To round out the discussion, the Option Menu may be dismissed via the following command:

((Activity) mContext).closeOptionsMenu();

Thus, the Options Menu can be "toggled" by using the following commands:

    ((Activity) this).openOptionsMenu();
((Activity) this).closeOptionsMenu();

Well, I hope this helps.

how to open a menu programmatically in python tkinter?

The invoke command is equivalent to tearoff. If you allowed tearoff you would see that work.

The command you are looking for is 'postcascade'. There is no tkinter binding to this command, and if you call it manually (root.tk.eval(str(mb)+' postcascade 1') nothing happens, which is probably why there is no binding.

I tried many other things but I could not get this to work.

However, tk has a Menubutton widget too, and that responds to the <<Invoke>> event. So (if you really really want this functionality) you can make your own menubar:

import Tkinter as tk
import ttk

def log(command):
print 'running {} command'.format(command)

class MenuBar(tk.Frame):
def __init__(self, master=None):
tk.Frame.__init__(self, master, bd=1, relief=tk.RAISED)

file_btn = tk.Menubutton(self, text='File')
menu_file = tk.Menu(file_btn, tearoff=False)
menu_file.add_command(label='save', command=lambda: log('save'))
menu_file.add_command(label='open', command=lambda: log('open'))
file_btn.config(menu=menu_file)
file_btn.pack(side=tk.LEFT)
master.bind('f', lambda e: file_btn.event_generate('<<Invoke>>'))

edit_btn = tk.Menubutton(self, text='Edit')
menu_edit = tk.Menu(edit_btn, tearoff=False)
menu_edit.add_command(label='add', command=lambda: log('add'))
menu_edit.add_command(label='remove', command=lambda: log('remove'))
edit_btn.config(menu=menu_edit)
edit_btn.pack(side=tk.LEFT)
master.bind('e', lambda e: edit_btn.event_generate('<<Invoke>>'))

m = tk.Tk()
m.geometry('300x300')
mb = MenuBar(m)
mb.pack(side=tk.TOP, fill=tk.X)
m.mainloop()

Once the menu is opened with the hotkey, it can be navigated with the arrow keys, the selected option can be run with the enter key, or closed with the escape key.

Android Create a simple menu programmatically

A--C's method works, but you should avoid setting the click listeners manually. Especially when you have multiple menu items.

I prefer this way:

private static final int MENU_ITEM_ITEM1 = 1;
...
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(Menu.NONE, MENU_ITEM_ITEM1, Menu.NONE, "Item name");
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case MENU_ITEM_ITEM1:
clearArray();
return true;

default:
return false;
}
}

By using this approach you avoid creating unecessary objects (listeners) and I also find this code easier to read and understand.

How to open mat-menu programmatically?

You need to get hold of MatMenuTrigger instance from [matMenuTriggerFor] element

#menuTrigger="matMenuTrigger"  [matMenuTriggerFor]="menu"

where

  • #menuTrigger is the template reference variable

  • matMenuTrigger is exportAs property of MatMenuTrigger metadata

and then simply call

(click)="menuTrigger.openMenu()"

Stackblitz example

Read more about template reference variable here

  • What is #auto attribute here and why it is required


Related Topics



Leave a reply



Submit