4

I have a menu which has sharp rectangle background. I have tried a lot i can't change it to rounded background. popup_menu.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#1F2026"
    android:orientation="horizontal"
    >

<TextView
    android:id="@+id/details"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="10dp"
    android:fontFamily="@font/quicksand_regular"
    android:text="@string/add_to_favourite"
    android:textColor="@color/icon_color_dark" />
</LinearLayout>

The above given is my popup menu xml.

 public static void showAlbumPopupOptions(final Activity activity, ImageView listMore,
                                         final Song song, String songId, int playingState) {

    final boolean isLogged = AppController.getBooleanPreference(Constants.LOGGED_IN, false);

    String[] listItems;

    listItems = activity.getResources().getStringArray(R.array.popup_menu_add_album);

    ArrayAdapter<String> mPopupAdapter = new ArrayAdapter<>(activity, R.layout.popup_menu, R
            .id.details, listItems);

    final ListPopupWindow albumPopup = new ListPopupWindow(activity);
    albumPopup.setContentWidth(Utils.measureContentWidth(mPopupAdapter, activity));

    albumPopup.setAdapter(mPopupAdapter);
    albumPopup.setHeight(ListPopupWindow.WRAP_CONTENT);
    albumPopup.setAnchorView(listMore);
    albumPopup.setModal(true);
    albumPopup.setDropDownGravity(Gravity.END);
    final LoadingProgressDialog loadingProgressDialog = new LoadingProgressDialog(activity, R
            .style
            .DialogThemeProgress, false);

    albumPopup.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
            if (position == 0) {
                if (isLogged) {
                    DialogPlayList dialogPlayList = new DialogPlayList(activity,
                            activity, String.valueOf(song.getId()), loadingProgressDialog);
                    dialogPlayList.show();
                    loadingProgressDialog.setDimDialog(false);
                    loadingProgressDialog.showProgress();
                } else {
                    redirectLogin(activity);
                }
                albumPopup.dismiss();
            } else if (position == 2) {
                showAudioShare(song, activity);
                albumPopup.dismiss();
            } else if (position == 1) {
                AppController.addToQueueSongs(activity, song, songId,
                        playingState);
                albumPopup.dismiss();
            }
        }
    });
    albumPopup.show();
}

The above given method is called when user clicks the more option in the list. I am adding datas to the list from this method. As a result I am getting this.

screenshot of the menu which has rectangle background

But actually what I want is - image given below.

expected output

I have tried setting background as rounded corners but no use. I literally have no idea how to achieve. Please suggest me some solutions. Thanks in advance.

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
  • You can also make a custom dialog box when you clicked on the dots and make a screen just like you expected @Chandramohan. – Brahma Datta Jun 29 '20 at 06:21

4 Answers4

6

With the a Material Components Theme you have a default rounded background with a corner radius of 4dp.

enter image description here

You can customize it using the listPopupWindowStyle attribute in your app theme:

<style name="AppTheme" parent="Theme.MaterialComponents.DayNight">
   <item name="listPopupWindowStyle">@style/myListPopupWindow</item>
</style>

with:

  <style name="myListPopupWindow" parent="Widget.MaterialComponents.PopupMenu.ListPopupWindow">
    <item name="android:popupBackground">@drawable/popupMenuBackground</item>
  </style>

where the drawable background is something like:

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="?attr/colorSurface"/>

    <corners
        android:bottomLeftRadius="8dp"
        android:bottomRightRadius="8dp"
        android:topLeftRadius="8dp"
        android:topRightRadius="8dp"/>
    
    <padding
        android:bottom="8dp"
        android:top="8dp"/>

</shape>

enter image description here

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
  • ?attr/colorSurface produce Resources$NotFoundException: If the resource you are trying to use is a vector resource, you may be referencing it in an unsupported way [duplicate] – Lobs Jun 16 '21 at 17:18
  • Also I tried to find out how to change the color of this popup. Use something like this: @color/modal_color –  Feb 18 '22 at 11:41
1

I found the answer. Add this code with ListPopupWindow before setting the adapter.

Drawable background = ContextCompat.getDrawable(activity, R.drawable
            .res_black_menuroundfilled_corner);
    albumPopup.setBackgroundDrawable(background);

Remove android:background="#1F2026" in popup_menu.xml

Below given code is for drawable file. res_black_menuroundfilled_corner

    <?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="#1F2026"/>
   
    <corners android:radius="15dp"/>

</shape>
1

It's very simple. Just add the code below to your themes file. Then create a popup menu and see the difference.

First create a background that will look round and put it in the drawable file

Here is the drawable code ->

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="@color/white"/>
    <corners android:radius="15dp"/>
</shape>

And here is the code you need to add to the themes file ->

<item name="popupMenuBackground">@drawable/round_corner</item>
0
ListPopupWindow listPopupWindow = new ListPopupWindow(this);
        listPopupWindow.setAdapter(popUpAdapter);
        listPopupWindow.setAnchorView(itemView);
        listPopupWindow.setDropDownGravity(Gravity.NO_GRAVITY);
        listPopupWindow.setBackgroundDrawable(ContextCompat.getDrawable(this,R.drawable.round_corner));
        listPopupWindow.setWidth(ListPopupWindow.WRAP_CONTENT);
        listPopupWindow.setHeight(ListPopupWindow.WRAP_CONTENT);
        listPopupWindow.show();
vishal pathak
  • 71
  • 1
  • 2