Google Kills Android Menu Button, Replaces It with Action Bar
But still so many devices have hardware menu button so now we can combine hardware menu button along with action bar option popup menu by just doinig a small trick.
When you clicks on the device's hardware menu button you can open a custom popup menu which has options pertaining to action bar. According to android documentation, only context menu and options menu can be made, and there is no way to access the hardware menu button's functions.
But here i can show you how to do that you just have to follow the code given below :-
here is the trick for xml -:
This is to add options to the menu -:
Create an XML file for action bar menu optionmenu.xml
res/menu/optionmenu.xml
Create a xml file for popupmenu dropdow popup.xml
res/menu/popup.xml
here is the trick for Activity Class
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.optionmenu, menu);
return true;
}
// this is to access hardware menu button
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
switch (keyCode) {
case KeyEvent.KEYCODE_MENU:
View menuItemView = findViewById(R.id.action_dropdown);
PopupMenu popupMenu = new PopupMenu(this, menuItemView);
popupMenu.getMenuInflater().inflate(R.menu.popup,
popupMenu.getMenu());
popupMenu
.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
Toast.makeText(Orders.this,
"You Clicked : " + item.getTitle(),
Toast.LENGTH_SHORT).show();
return true;
}
});
popupMenu.show();
return true;
}
return super.onKeyUp(keyCode, event);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
this.finish();
return true;
case R.id.action_dropdown:
View menuItemView = findViewById(R.id.action_dropdown);
PopupMenu popupMenu = new PopupMenu(this, menuItemView);
popupMenu.getMenuInflater().inflate(R.menu.popup,
popupMenu.getMenu());
popupMenu
.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
Toast.makeText(Orders.this,
"You Clicked : " + item.getTitle(),
Toast.LENGTH_SHORT).show();
return true;
}
});
popupMenu.show();
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
Hope this will help you all the best.
Cheers.
0 Comment(s)