Popup menu on click of a button in action Bar

38,281

Solution 1

I found a solution to this. Instead to using the menu XML to inflate the popup menu, I made a XML layout file.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#8b8989"
android:orientation="vertical"
android:padding="10dip" >

<TextView
    android:id="@+id/menuItem1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dip"
    android:text="@string/menu1" />

<TextView
    android:id="@+id/menuItem2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dip"
    android:text="@string/menu2" />
<TextView
    android:id="@+id/menuItem3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dip"
    android:text="@string/menu3" />
</LinearLayout> 

and i changed the onClick method

public void showPopup(View v) {
    LayoutInflater inflater = (LayoutInflater) MainActivity.this
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        PopupWindow pw = new PopupWindow(inflater.inflate(
                R.layout.overflow_layout, null, false), 300, 400, true);
        pw.showAtLocation(findViewById(R.id.container), Gravity.CENTER, 0,
                0);
}

This solved the issue

Solution 2

I found this here: http://developer.android.com/guide/topics/ui/menus.html

<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:id="@+id/selectImg"
        android:icon="@android:drawable/ic_dialog_dialer"
        android:showAsAction="always">

        <menu>
            <item android:id="@+id/top"
                android:title="@string/topimg"/>
            <item android:id="@+id/bottom"
                android:title="@string/botimg" />
        </menu>

    </item>
</menu>

You can put a menu within a menu to present sub-menus when the item is clicked. Then, in Java, you can use the same methods as usual.

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu items for use in the action bar
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main, menu);
        return super.onCreateOptionsMenu(menu);
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle presses on the action bar items
       // View v = findViewById(R.id.f);
        switch (item.getItemId()) {
            case R.id.top:
                //action
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

The id of 'top' in the xml is still recognized even though it is a sub menu. This worked for me and it looks just like the popup menu.

Solution 3

Hi every one, that's my own solution : i created the showPopup method, and then i called it in the onOptionsItemSelected like this :

public void showPopup(){
    View menuItemView = findViewById(R.id.menu_save);
    PopupMenu popup = new PopupMenu(getActivity(), menuItemView);
    MenuInflater inflate = popup.getMenuInflater();
    inflate.inflate(R.menu.popup, popup.getMenu());
    popup.show();

}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {
    case R.id.menu_save:    
        showPopup();
        return true;
    default:
        return super.onOptionsItemSelected(item);
}

}

popup.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
    android:id="@+id/decon"
    android:showAsAction="ifRoom"
    android:title="@string/decon"/>

<item
    android:id="@+id/mRes"
    android:showAsAction="ifRoom"
    android:title="@string/mesRes"/>

</menu>

main.xml => it's called onCreateOptionsMenu

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

<item
    android:id="@+id/menu_save"
    android:enabled="true"
    android:icon="@drawable/action_save"
    android:showAsAction="ifRoom|withText"
    android:title="@string/action_save"
    android:visible="true"/>

</menu>

Finally i

implements PopupMenu.OnMenuItemClickListener to @Override onMenuItemClick method.

Solution 4

As the popup menu is a MENU, you have to handle this by implementing the "onOptionsItemSelected". You'll be able to say what to do for each menu option. It will replace the "onClick" option you defined and will be called automatically.

Solution 5

Try to change 'this' to getActivity().

public void showPopup(View v) {
    PopupMenu popup = new PopupMenu(getActivity(), v);
    MenuInflater inflater = popup.getMenuInflater();
    inflater.inflate(R.menu.overflow, popup.getMenu());
    popup.show();
}

Hope it helps..!!

Share:
38,281
darsh
Author by

darsh

Android programmer

Updated on July 09, 2022

Comments

  • darsh
    darsh almost 2 years

    I am trying to implement an action bar in which one of the buttons on click shows a popup menu. Here's the menu. XML (menu items in the action bar)

    <menu xmlns:android="http://schemas.android.com/apk/res/android" >
    
    <item
        android:id="@+id/search"
        android:icon="@drawable/ic_action_search"
        android:orderInCategory="0"
        android:showAsAction="always"
        android:title="@string/menu_search"/>
    <item
        android:id="@+id/refresh"
        android:icon="@drawable/ic_action_refresh"
        android:orderInCategory="1"
        android:showAsAction="always"
        android:title="@string/menu_refresh"/>
    
    
     <Item
        android:id="@+id/popup"
        android:icon="@drawable/ic_action_search"
        android:onClick="showPopup"
        android:orderInCategory="1"
        android:showAsAction="always"
        android:title="@string/menu_search" />
    

    I wish to show a popup menu on the click of the item having id "@+id/popup".

    here is the XML for the popup menu

    <menu xmlns:android="http://schemas.android.com/apk/res/android" >
    
    <item
        android:id="@+id/item1"
        android:icon="@drawable/ic_action_search"
        android:orderInCategory="0"
        android:showAsAction="always"
        android:title="@string/menu_search"/>
    <item
        android:id="@+id/item2"
        android:icon="@drawable/ic_action_search"
        android:orderInCategory="1"
        android:showAsAction="always"
        android:title="@string/menu_search"/>
    

    here is the onClick method for the button

    public void showPopup(View v) {
        PopupMenu popup = new PopupMenu(this, v);
        MenuInflater inflater = popup.getMenuInflater();
        inflater.inflate(R.menu.overflow, popup.getMenu());
        popup.show();
    }
    

    And the problem is that no popup shows up on click of that button. Need help folks.

    • Gophermofur
      Gophermofur almost 12 years
      Are you sure that showPopup is being called? Maybe the onClick isn't registering properly?
    • darsh
      darsh almost 12 years
      I am not sure whether the onClick is registering properly.
    • Gophermofur
      Gophermofur almost 12 years
      Set a log.d or breakpoint in the showPopup method and see if they print anything out/get hit. If that works, then you can focus on the code within showPopup.
  • darsh
    darsh almost 12 years
    oh sorry that was a mistake in the question. Its actually one and the same, I am editing the question
  • Rookie
    Rookie almost 12 years
    developer.android.com/resources/samples/ApiDemos/src/com/… . Try this but i guess u r doing the same thing..
  • Rijul Gupta
    Rijul Gupta about 9 years
    Thanks a lot, really helped.
  • Nlinscott
    Nlinscott almost 9 years
    inflating a layout every single time is expensive. there is a much cleaner, efficient and robust way to do this. Look at my answer