2

I'm trying to customize the settings menu or create something with that functionality. I currently have a Toolbar with the title and a Settings button (with custom icon). This button opens a PopupMenu this way:

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

        PopupMenu popupMenu = new PopupMenu(HomeActivity.this, view){
            @Override
            public void setOnMenuItemClickListener(@Nullable OnMenuItemClickListener listener) {
                super.setOnMenuItemClickListener(listener);
            }
        };

        popupMenu.inflate(R.menu.menu);
        popupMenu.show();


    }
});

The popup menu is working but I need now to customize the view, at least to make it full width and center the text, as shown in the image:

dropdown menu

Is it possible to do this with a popup menu? how can I extend the width of the menu and make it full width?

Thanks in advance.

2 Answers 2

2

You can use ListPopUpWindow for this. The advantage is you can customize the layout inside it to put list of strings with images also and etc. Try using this code.

ListPopupWindow listPopupWindow;
setUpListpopupWindow();
btnOpenMenu.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {

        listPopupWindow.show();
    } 
});

Set your list popup window here.

public void setUpListpopupWindow() {
    listPopupWindow = new ListPopupWindow(BluetoothActivity.this);
    listPopupWindow.setAnchorView(btnOpenMenu);
    DisplayMetrics metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);
    listPopupWindow.setWidth(metrics.widthPixels); //sets width as per the screen.
    listPopupWindow.setHeight(ListPopupWindow.WRAP_CONTENT);
    listPopupWindow.setModal(true);

    View filterLayout = getLayoutInflater().inflate(R.layout.your_awesome_custom_layout, null);

    listPopupWindow.setPromptView(filterLayout);
}
0
1

Instead using pop menu for making it full width and center the text. Which I dont think is really easy.

Why dont you use Bottom Sheets.

There are lots of open source libraries available.Make use of it.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.