Home » Android » Popup Menu issue in Android ListView

Popup Menu issue in Android ListView

Posted by: admin June 16, 2020 Leave a comment

Questions:

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.

How to&Answers:

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);

Answer:

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();

Answer:

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.

Answer:

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.

Answer:

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);