How to add Menu to a layout

android, menu

Solution

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>

Problem

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.!

Original source