Search code examples
kotlinstylespopupmenu

How to change Popup menu background color


I've implemented a popup menu in my recyclerview adapter, but I have a problem with the popup menu background color.

enter image description here

As you can see the background makes the menu look unappealing. All I want to do is make the background white and leave the text color as it is.

I've tried other solutions, but they haven't worked for me.

This is one of them

  <style name="CustomPopUpStyle" parent="Widget.AppCompat.PopupMenu">
    <item name="android:textColor">@android:color/white</item>
    <item name="android:itemBackground">@color/white</item>
</style>

 holder.mMenu.setOnClickListener(View.OnClickListener { //creating a popup menu
        val popup = PopupMenu(context, holder.mMenu, R.style.CustomPopUpStyle)
        //inflating menu from xml resource
        popup.inflate(R.menu.options_menu)

These don't yield any results. My popup menu still looks the same as the above image.


Solution

  • I got my answer. It turns out all I had to do was change my main theme's item background.

        <style name="Theme.MyNotepad" parent="Theme.MaterialComponents.Light.NoActionBar">
        <!-- Primary brand color. -->
        <item name="colorPrimary">@color/purple_500</item>
        <item name="colorPrimaryVariant">@color/purple_700</item>
        <item name="colorOnPrimary">@color/white</item>
        <!-- Secondary brand color. -->
        <item name="colorSecondary">@color/white</item>
        <item name="colorSecondaryVariant">@color/teal_700</item>
        <item name="colorOnSecondary">@color/white</item>
    
        <item name="rippleColor">@color/colorAccent</item>
    
        <item name="android:textColor">@color/textColor</item>
        <!-- Status bar color. -->
        <item name="android:statusBarColor" >?attr/colorPrimaryVariant</item>
        <item name="android:itemBackground">@color/white</item> <!-- Made the change here -->