PopupMenu that show grid instead of list

1.4k views Asked by At

I created a PopupMenu that shows up when I click a button but it shows a list of menu items. I want to know if there is a way to make it grid instead of list.

button = (Button) findViewById(R.id.menu_btn);

    final PopupMenu popupMenu = new PopupMenu(MainActivity.this, button);
    popupMenu.getMenu().add("item 1");
    popupMenu.getMenu().add("item 2");
    popupMenu.getMenu().add("item 3");

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            popupMenu.show();
        }
    });
4

There are 4 answers

0
mnagy On

I actually figured out some way to do it

static final String TITLE = "title";
List<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();
PopupWindow popupMessage;
ImageButton popupButton;
GridView gridview;
View view;

// Use this to add items to the list that the ListPopupWindow will use
private void addItem(String title) {
    HashMap<String, Object> map = new HashMap<String, Object>();
    map.put(TITLE, title);
    data.add(map);
}

public void init() {
    popupButton = (ImageButton) findViewById(R.id.menu_btn_btn);

    view = LayoutInflater.from(this).inflate(R.layout.popup_window, null);
    gridview = (GridView) view.findViewById(R.id.popup_window_gridview);
    gridview.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Log.e("clicked", "ok");

        }
    });

    SimpleAdapter adapter = new SimpleAdapter(
            this,
            data,
            android.R.layout.activity_list_item, // You may want to use your own cool layout
            new String[]{TITLE}, // These are just the keys that the data uses
            new int[]{android.R.id.text1}); // The view ids to map the data to

    addItem("one");
    addItem("two");
    addItem("three");
    addItem("four");
    addItem("five");
    addItem("six";


    gridview.setAdapter(adapter);

}

public void popupInit() {
    popupButton.setOnClickListener(this);

    popupMessage = new PopupWindow(view, LinearLayout.LayoutParams.WRAP_CONTENT,
            LinearLayout.LayoutParams.WRAP_CONTENT);
    popupMessage.setContentView(view);


}

after than all you have to is to init() and popupInit() in onCreate()

Unfortunately, the gridView onItemClickListner doesn't work actually and I still did not figured out why!

0
Gabriella Angelova On

You could create a custom PopupWindow layout that is exactly what you want and inflate it before the show() function. Here http://developer.android.com/guide/topics/ui/menus.html#PopupMenu or here How to create a custom PopupMenu in Android you could find some examples.

0
Jignesh Jain On

yes it is possible create custom popup menu and take grid view with it's item

check below examples

link1

link2

link3

0
A.S On

you can use a custom dialog instead to achieve what you are seeking, check this out Custom dialog

hoper this will help.