Alexandre Alves
Alexandre Alves

Reputation: 421

Android Layout background color change

I've search quite a bit in google and extensively in this site also, if I missed the question that answer mine I'm sorry. But here it goes:

            public void onClick(View v){
            Button btt= (Button) findViewById(R.id.bttROnOff);
            LinearLayout ll = (LinearLayout) findViewById(R.id.layScreen);
            if ((btt.getText()).toString().compareToIgnoreCase("Reading Mode OFF")==0) {
                ll.setBackgroundColor(R.color.paleYellow);
                WindowManager.LayoutParams lp = getWindow().getAttributes();
                lp.screenBrightness = originalBrightness;
                getWindow().setAttributes(lp); 
                btt.setText("Reading Mode ON");
            }
            else {
                ll.setBackgroundColor(Color.WHITE);
                WindowManager.LayoutParams lp = getWindow().getAttributes();
                lp.screenBrightness = 1;
                getWindow().setAttributes(lp); 
                btt.setText("Reading Mode OFF");
            }
            }

I have a button to change the background color to white and then to "paleYellow" which is defined in the strings.xml file. In my xml layout file, it starts with this color and when I press the button it changes to white. But if I press the button to return to the previous one, what I get is a black background. If I use instead:

ll.setBackgroundColor(Color.Yellow);

It works, but:

ll.setBackgroundColor(R.color.paleYellow);

Does not :S

Upvotes: 0

Views: 6918

Answers (3)

JohnnyB
JohnnyB

Reputation: 48

Have you tried something like...

Resources myRes = getResources();
int colorPaleYellow = myRes.getColor(R.color.paleYellow);
setBackgroundColor(colorPaleYellow);

or maybe something like...

setBackgroundcolor(this.getColor(R.color.paleYellow));

Upvotes: 1

DonGru
DonGru

Reputation: 13710

this should do it:

ll.setBackgroundColor(getResources().getColor(R.color.paleYellow));

Upvotes: 0

MByD
MByD

Reputation: 137272

setBackgroundColor takes an int that represent a color in sRGB, while R.color.paleYellow is an id of color, but not in the same representation. to use it, you should call setBackgroundResource(R.color.paleYellow)

Upvotes: 2

Related Questions