Android Studio - Inflate PopupMenu with custom layout?

0

I try to inflate the popupmenu with a custom layout, I do not want to load it from the resources "menu". I have the following code:

imageButtonMasOpciones.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {


            //Creating the instance of PopupMenu
            PopupMenu popup = new PopupMenu(getContext(), imageButtonMasOpciones);

            //Inflating the Popup using xml file
            popup.getMenuInflater().inflate(R.menu.popup_menu, popup.getMenu());

            //registering popup with OnMenuItemClickListener
            popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                @Override
                public boolean onMenuItemClick(MenuItem menuItem) {
                    Toast.makeText(getContext(),"Item : " + menuItem.getTitle(),Toast.LENGTH_SHORT).show();
                    return true;
                }
            });
            popup.show();
        }
    });

In that code the popupMenu is created well, but when I replace this line of code:

popup.getMenuInflater().inflate(R.menu.popup_menu, popup.getMenu());

for this:

popup.getMenuInflater().inflate(R.layout.popup, popup.getMenu());

the application closes

    
asked by Leonidas 09.12.2018 в 21:48
source

1 answer

0

In your menu directory:

there you must create your own popup_menu and inside:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
    android:id="@+id/popupBorrar"
    android:title ="Borrar"
    />
</menu>

Each item is an option of your popup menu, if you want something with more view and more design I think you could implement DialogFragment

    
answered by 10.12.2018 в 01:22