Mohsen Shakiba
Mohsen Shakiba

Reputation: 1872

android - Why my popup menu is black?

I'm using built-in android popup menu but the result is this:
enter image description here

PopupMenu popupMenu = new PopupMenu(context, holder.menu);
popupMenu.getMenuInflater().inflate(R.menu.item_menu, popupMenu.getMenu());

and the item_menu.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/one"
        android:title="item1"/>
    <item
        android:id="@+id/two"
        android:title="item2"/>
    <item
        android:id="@+id/three"
        android:title="item3"/>
</menu>  

and the styles for the theme

<style name="AppTheme" parent="Theme.AppCompat.Light">
    <item name="popupMenuStyle">@style/PopupMenu</item>
</style>

<style name="PopupMenu" parent="Widget.AppCompat.PopupMenu">
    <item name="android:popupBackground">#FFFFFF</item>
</style>

Upvotes: 5

Views: 3508

Answers (5)

Mattia Ferigutti
Mattia Ferigutti

Reputation: 3708

Just add the popupMenuBackground attribute to define the background color of your PopupMenu in your main theme.

 <style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
     ...

     <item name="popupMenuBackground">@color/screen_white_background</item>
 </style>

Upvotes: 0

saksham
saksham

Reputation: 3324

People should understand how android works:

    <style name="AppTheme.Toolbar" parent="Theme.AppCompat.DayNight.DarkActionBar">

    //This line changes the color of text in Toolbar
    <item name="android:textColorPrimary">@color/black_color</item>

    //This line changes the color of icons in toolbar (back, overfmenu_convmenu icons)
    <item name="android:textColorSecondary">@color/white</item>

</style>

This is a toolbar theme. Now see the parent parent="Theme.AppCompat.DayNight.DarkActionBar"

This is a dark android theme so here android internally plot a white popup and white icons. If you used light the theme like Theme.AppCompat.NoActionBar Pop up will occur black. You can control + click and go inside these themes and check background of popupMenu already defined. So if you want white background use theme

Theme.AppCompat.DayNight.DarkActionBar

Now must focus on these two lines.

//This line changes the color of text in Toolbar
<item name="android:textColorPrimary">@color/black_color</item>

//This line changes the color of icons in toolbar (back, overfmenu_convmenu icons)
<item name="android:textColorSecondary">@color/white</item>

android:textColorPrimary is what you need to make black as your text will not be visible if you set it to white on white background.

Upvotes: 0

ultra.deep
ultra.deep

Reputation: 1809

Likely to be the Context type:

Parent of my AppTheme is Theme.AppCompat.Light.DarkActionBar and parent of my activity is AppTheme

// Background = black and textColor = black
PopupMenu popupMenu = new PopupMenu(getBaseContext() , view); // don't use getBaseContext()

// Background = black and textColor = white
PopupMenu popupMenu = new PopupMenu(getApplicationContext() , view); // don't use getApplicationContext()

so you must just use Activity Context:

// Background = light and textColor = dark
PopupMenu popupMenu = new PopupMenu(YourActivity.this , view); // this is ok

Upvotes: 5

se_bastiaan
se_bastiaan

Reputation: 1704

Add the following in values-v14/styles.xml:

<style name="AppTheme" parent="Theme.AppCompat.Light">
    <item name="popupMenuStyle">@style/PopupMenu</item>
    <item name="android:popupMenuStyle">@style/PopupMenu</item>
</style>

And change the PopupMenu style:

<style name="PopupMenu" parent="Widget.AppCompat.Light.PopupMenu">
    <item name="android:popupBackground">#FFFFFF</item>
</style>

Upvotes: 1

Nilay Dani
Nilay Dani

Reputation: 896

Check your app theme in manifest file It affects the views...

Upvotes: 1

Related Questions