8

How can I customize the menuItems in a popup menu? I need a switch for the first menuitem. Here is what I got so far:

menu.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="android.oli.com.fitnessapp.activities.LiveSelectActivity">

    <item
        android:icon="@drawable/ic_access_alarm_white_24dp"
        android:orderInCategory="100"
        app:showAsAction="always"
        android:visible="true"
        android:title="@string/action_settings"
        android:onClick="showPopup"/>

</menu>

menu_popup.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@+id/one"
        android:title="One"/>

    <item
        android:id="@+id/setTime"
        android:title="Two"
        android:onClick="showTimePickerDialog"/>


</menu>

Activity snippet

public void showPopup(MenuItem menuItem){
        View view = findViewById(R.id.action_alarm);
        PopupMenu popup = new PopupMenu(this, view);
        MenuInflater inflater = popup.getMenuInflater();
        inflater.inflate(R.menu.menu_popup, popup.getMenu());
        popup.show();
    }
Oliver U.
  • 336
  • 1
  • 4
  • 14

1 Answers1

31

You can use popupwindow since it allows to use custom layouts.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" 
    android:padding="5dp">

<Switch
    android:id="@+id/mySwitch"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginTop="20dp"
    android:text="Play with the Switch" />

<TextView
    android:id="@+id/switchStatus"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/mySwitch"
    android:layout_marginTop="22dp"
    android:text="Medium Text"
    android:textAppearance="?android:attr/textAppearanceMedium" />

</RelativeLayout>

and in ur activity implement this method:

/* you should refer to a view to stick your popup wherever u want.
** e.g. Button button  = (Button) findviewbyId(R.id.btn);
**     if(popupWindow != null)
**         showPopup(button); 
**/
    public void showPopup(View v) {


            LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            final View popupView = layoutInflater.inflate(R.layout.popup_filter_layout, null);

            popupWindow = new PopupWindow(
                    popupView,
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT);

            popupWindow.setBackgroundDrawable(new BitmapDrawable());
            popupWindow.setOutsideTouchable(true);
            popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
                @Override
                public void onDismiss() {
                    //TODO do sth here on dismiss
                }
            });

            popupWindow.showAsDropDown(v); 
}
ugur
  • 3,604
  • 3
  • 26
  • 57
  • BitmapDrawable() is deprecated. The window does not show up. how can I position it that it looks like a popupmenu? – Oliver U. Mar 30 '16 at 23:40
  • 2
    I edited my answer. You should call popupWindow.showAsDropDown(v) to make it visible. And thats true BitmapDrawable() deprecated but it is a trick to make it disapper on key pressed. More details refer to http://stackoverflow.com/a/3122696/5923606 – ugur Mar 31 '16 at 00:19
  • 2
    And to position it like popupmenu input your view "View view = findViewById(R.id.action_alarm)" into popupWindow.showAsDropDown(view ) – ugur Mar 31 '16 at 00:22
  • I will give it a try! :) – Oliver U. Mar 31 '16 at 00:44
  • Thanks @ugur for a good idea to create popup menu with custom layout, +1 for you – Basant Oct 02 '20 at 14:07