Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add Menu to a layout

Tags:

android

menu

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

like image 826
Abhishek Avatar asked Jul 11 '14 05:07

Abhishek


People also ask

How do I open the pop up menu on android?

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.


2 Answers

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);
    }
}
like image 149
Rhn Bhadani Avatar answered Sep 22 '22 04:09

Rhn Bhadani


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>
like image 44
DCoder Avatar answered Sep 21 '22 04:09

DCoder