8

I am new to Android and I am creating Listview Popup Menu. But I am having it's width and height problem. The Popup Menu can take more height and width. There are many questions in SO, but none of these helped me.

To create Popup Menu I have tried the following method.

1 ] Using Popup menu with the code below:

 private void showPopupMenu(View view){
        Context wrapper = new ContextThemeWrapper(this, R.style.PopupMenu);
        PopupMenu popupMenu = new PopupMenu(wrapper,view);

        popupMenu.getMenuInflater().inflate(R.menu.popup_menu,popupMenu.getMenu());
        popupMenu.show();

        popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener(){
            @Override
        public boolean onMenuItemClick(MenuItem item){
                switch (item.getItemId()){
                    case R.id.install:
                        Intent intent = new Intent(ViewAllRelationActivity.this,EditRelativeActivity.class);
                        startActivity(intent);
                        break;
                    case R.id.addtowishlist:
                        break;

                }
                return false;
            }
        });
    }

It gives this output :

enter image description here

2 ] Using ContextMenu it displays the following output :

We can maintain width and height in ContextMenu But it always show in Centre not each row of our Listview Data.

enter image description here

But I want below Image type Popup menu. Width and height are small.

enter image description here

Please provide the solution for this.

SMR
  • 6,628
  • 2
  • 35
  • 56
Harshad
  • 1,344
  • 1
  • 10
  • 25

5 Answers5

2

You can add those options programmatically without using xml file as below, may be this could help you out.

Here ONE and TWO is showing the index for options which you are giving in popup menu. Like on 1st position EDIT POST on 2nd position REMOVE POST etc.

1) on image click open popup menu:

private final static int ONE = 1;
private final static int TWO = 2;

PopupMenu popupMenu = new PopupMenu(this, findViewById(R.id.img_detail_information_options));
popupMenu.getMenu().add(Menu.NONE, ONE, Menu.NONE, getResources().getString(R.string.detail_information_edit_post));
popupMenu.getMenu().add(Menu.NONE, TWO, Menu.NONE, getResources().getString(R.string.detail_information_remove_post));
popupMenu.setOnMenuItemClickListener(this);
popupMenu.show();
JoxTraex
  • 13,423
  • 6
  • 32
  • 45
Bhavnik
  • 2,020
  • 14
  • 21
  • ONE and TWO not found and also popupMenu.setOnMenuItemClickListener(this); giving me error. – Harshad Feb 16 '16 at 07:12
  • ONE and TWO is showing the index for options menu, you can set int number in place of ONE and TWO, and popupMenu.setOnMenuItemClickListener(this); is giving error because you've not implemented PopupMenu.OnMenuItemClickListener in your activity or you can just simply replace that with this one popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener()); – Bhavnik Feb 16 '16 at 07:19
  • edit the Answer and write full description I can't understand you. – Harshad Feb 16 '16 at 07:21
  • I Tried popupMenu.setOnMenuItemClickListener(this); line give me red signal. – Harshad Feb 16 '16 at 11:29
2

If you are using adapter, you can put in getView(...) method in adapter

    imvMore.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            showPopupMenu(act,v);
        }
    });

and put method

private void showPopupMenu(Activity act, View view){
    PopupMenu popupMenu = new PopupMenu(act,view);
    popupMenu.getMenuInflater().inflate(R.menu.menu_popup, popupMenu.getMenu());
    popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            switch (item.getItemId()) {

            }
            return true;
        }
    });
    popupMenu.show();
}

in your adapter class.

Note: act is Activity you have to bind when create constructor adapter, example:

public YourAdapter(Activity act, ArrayList<ItemOfYourModel> data){
    this.data = data;
    this.act = act;
}

In Activity, you can code:

ArrayList<ItemOfYourModel> listData = new ArrayList<ItemOfYourModel>();
listData.add(new YourItemOfYourModel(...));
YourAdapter adapter = new YourAdapter(this,listData);
dolphin
  • 333
  • 1
  • 5
2

You can use ListPopupWindow to get what you want to achieve. You can set the more option icon or menu icon as anchor for the popup window.

ListPopupWindow mListPopupWindow;
mListPopupWindow = new ListPopupWindow(this, null);
mListPopupWindow.setWidth(300);
mListPopupWindow.setAnchorView(menuIcon);
mListPopupWindow.setHeight(200);
mListPopupWindow.setAdapter(yourAdapter);
mListPopupWindow.show();

You will get what you want.

frogEye
  • 364
  • 3
  • 14
1

The best solution of this I think is to use PopupWindow ,you can control every thing of it and it is very easy to create the menu you want.

wngxao
  • 72
  • 5
1

Google Play uses the Holo theme for PopupMenu. You can do the same by creating a custom style in your styles.xml:

<style name="PopupMenuStyle" parent="android:Theme.Holo.Light">
        <!-- Your custom attributes must be put here. This is optional, but the parent must not be changed -->
</style>

And then change the PopupMenu style in your code:

Context wrapper = new ContextThemeWrapper(this, R.style.PopupMenuStyle);
PopupMenu popupMenu = new PopupMenu(wrapper,view);
Piyush
  • 1,744
  • 1
  • 15
  • 28
  • I Used this But Menu Display in the Center like ContextMenu I want it each Row of ListView. – Harshad Mar 02 '16 at 13:09
  • You must be setting this on the whole view. Instead, add a button like Google play has in the list item. Then it will not appear in the center. – Piyush Mar 02 '16 at 13:13
  • How can I set Width and Height is Give My First Conditon of Question. – Harshad Mar 02 '16 at 13:32
  • For PopupMenu, you can't. The white background for each item you see in the menu is drawable of a fixed size. The size is the ideal size of a menu, but if you still want to change the size, use PopupWindow. – Piyush Mar 02 '16 at 15:44