code to trigger menu after button click in Android


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();
}
});
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);
}
}

Comments

Popular posts from this blog

Custom Dialog with no title and full screen in Android

Best working code to set Date limits on Date Picker Dialog,in Android

code to display .pdf,.text,.DOC,.DOCX,.doc extension files exists in SDCard