Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Popup Menu issue in Android ListView

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.

like image 843
Harshad Avatar asked Feb 16 '16 05:02

Harshad


3 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);
like image 96
dolphin Avatar answered Nov 06 '22 15:11

dolphin


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();
like image 43
Bhavnik Avatar answered Nov 06 '22 16:11

Bhavnik


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.

like image 2
frogEye Avatar answered Nov 06 '22 17:11

frogEye