I have a situation in which there are different layouts and each layout has a menu. How do I do it? For reference, you may visit Youtube Mobile App and on the right side of the video, there appears 3 dots, on clicking them, a menu will be opened. I have the screen shot but inadequate credits stop me from uploading it.Please help me out. Thanks in Advance.!
Go to app > res > right-click > New > Android Resource Directory and give Directory name and Resource type as menu. Now, we will create a popup_menu file inside that menu resource directory. Go to app > res > menu > right-click > New > Menu Resource File and create a menu resource file and name it as popup_menu.
Remember you don't need to design layout for ActionBar.
Just create a file @ res/menu/main_activity_actions.xml and add the item you want to use in ActionBar.
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/action_search"
android:icon="@drawable/ic_action_search"
android:title="@string/action_search"/>
<item android:id="@+id/action_compose"
android:icon="@drawable/ic_action_compose"
android:title="@string/action_compose" />
Inflate the layout in onCreateOptionsMenu
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
return super.onCreateOptionsMenu(menu);
}
Handle the click events for items.
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.action_search:
openSearch();
return true;
case R.id.action_compose:
composeMessage();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
As mentioned by user1632209 you can use android's menu but if you want to create your own pop menu you can do it as follows:
PopupMenu popup = new PopupMenu(context, btnSettings); //you can use image button
// as btnSettings on your GUI after
//clicking this button pop up menu will be shown
popup.getMenuInflater().inflate(R.menu.settings_menu, popup.getMenu());
popup.setOnMenuItemClickListener(this);
popup.show();
you can add listener to your menu option like:
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.option1:
//Code for option 1
break;
case R.id.option2:
//Code for option 2
break;
default:
break;
}
return false;
}
Create settings_menu.xml in res->menu directory like:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/option1"
android:icon="@drawable/icon_for_option1"
android:orderInCategory="100"
android:showAsAction="never"
android:title="Option 1"/>
<item
android:id="@+id/option2"
android:icon="@drawable/icon_for_option1"
android:orderInCategory="200"
android:showAsAction="never"
android:title="Option 2"/>
</menu>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With