Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Rounded corners in Menu

in my MainActivity, in onCreateOptionsMenu method, I inflate a basic menu with four items (see menu_main.xml).
As it is possible to have rounded corners in DialogFragments, how can I get rounded corners for this menu?

As you can see in the screenshot, the menu appears as an overflow menu on top of the whole Activity (yes, the basic menu that Android gives you when tapping the three dots).
Screenshot here: MainActivity with menu opened

WHAT I TRIED
as for the Dialog, I added android:background="@drawable/basic_rounded_corners to both the menu and the item nodes in menu_main.xml ---> not working (cause I don't know how to set the backgroundDrawable of the menu programmatically, if it is even possible like for the DialogFragments)

WHAT I WANT
I want to know how to get rounded corners on my menu.

menu_main.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.beagleentertain.pillreminder.MainActivity"
android:background="@drawable/basic_rounded_corners">
<item
    android:id="@+id/action_settings"
    android:orderInCategory="100"
    android:title="@string/checkable_make7dayspause"
    android:checkable="true"
    android:checked="false"
    app:showAsAction="ifRoom"
    />
<item
    android:id="@+id/about_settings"
    android:orderInCategory="100"
    android:title="@string/text_menu_about"
    app:showAsAction="ifRoom"
    />
<item
    android:id="@+id/settings_settings"
    android:orderInCategory="100"
    android:title="@string/action_settings"
    app:showAsAction="ifRoom"
    />
<item
    android:id="@+id/settings_share"
    android:orderInCategory="100"
    android:title="@string/action_share"
    app:showAsAction="ifRoom"
    />

like image 919
BEAGLE ENTERTAIN Avatar asked Jan 03 '18 18:01

BEAGLE ENTERTAIN


1 Answers

First you need a toolbar in your activity in order to set a style for the popMenu.

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@color/colorPrimary"
    app:popupTheme="@style/ThemeOverlay.MyTheme"
    app:titleTextColor="#f0f0f0"/>

Then add this style and make sure you to set the colorBackground to transparent so the rounded corners show correctly and then use any drawable shape as the background.

 <style name="ThemeOverlay.MyTheme" parent="ThemeOverlay.AppCompat.Light">
        <item name="android:colorBackground">@android:color/transparent</item>
        <item name="android:textColor">#000000</item>
        <item name="android:background">@drawable/rounded</item>
    </style>

If you want to make a completely custom window you can follow this tutorial in this Article. just make sure to add the rounded shape as background in the root view.

like image 166
Alaa AbuZarifa Avatar answered Sep 27 '22 17:09

Alaa AbuZarifa