mouhcine
mouhcine

Reputation: 129

How to trigger a menu button click event through code in Android

I want to set a button that will emulate click event of menu button standard of android.

Upvotes: 0

Views: 4836

Answers (3)

Daniel
Daniel

Reputation: 944

You can open the menu by using a Button with the following code

Button button = (Button)findViewById(R.id.my_bytton);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
    openOptionsMenu();
}
});

Upvotes: 1

Pranav Jagtap
Pranav Jagtap

Reputation: 1

just add on click listener event to the button.http://developer.android.com/reference/android/view/View.OnClickListener.html

Upvotes: 0

User0911
User0911

Reputation: 1582

Here is the onCreateOptionsMenu method :-

@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_1, menu);
return true;
}

And to handle the click event, use

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.new_menu:
    // do part 1
    return true;
case R.id.help_menu:
    // do part 2
    return true;
default:
    return super.onOptionsItemSelected(item);
}
}

Upvotes: 1

Related Questions