0

I am trying to change the color of the background app when I click a menu for example change background black. The color it is changing for the first time into transparent so like grey and than stays blue.

I want if the color is red to take red and other colors. This is my code.

 mPopupMenu = new PopupMenu(this, settings);
        MenuInflater menuInflater = mPopupMenu.getMenuInflater();
        menuInflater.inflate(R.menu.main_settings, mPopupMenu.getMenu());
        settings.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPopupMenu.show();
                mPopupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem item) {
                        int id = item.getItemId();
                        if(id == R.id.menu_back_white) {
                            setActivityBackgroundColor(R.color.white);
                        } else if (id == R.id.menu_back_black) {
                            setActivityBackgroundColor(R.color.material_grey_900);
                        }
                        return false;
                    }
                });

            }
        });

 public void setActivityBackgroundColor(int color) {
        View view = this.getWindow().getDecorView();
        view.setBackgroundColor(color);
    }

colors

<color name="material_grey_900">#ff212121</color>
<color name="white">#FFFFFF</color>

styles

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="colorPrimaryDark">@color/blue_900</item>
        <item name="colorPrimary">@color/blue_300</item>
        <item name="colorAccent">@color/blue_300</item>
        <item name="colorControlNormal">@color/white</item>
    </style>

enter image description here

TheCoderGuy
  • 771
  • 6
  • 24
  • 51

1 Answers1

2

view.setBackgroundColor expects a color value but you give it a resource id.

As stated in Get color-int from color resource you have to convert it with e.g. (deprecated variant)

getResources().getColor(color);

or

ContextCompat.getColor(context, R.color.your_color);

(since support library 23)

Michael Butscher
  • 10,028
  • 4
  • 24
  • 25