简体   繁体   中英

How to set text color in code to a android widget?

I have an android widget that updates and chose a random int from 4 numbers, if the number equals 1 i want it to set the text color to a specific color i tried:

if (colornumb == 1)
        {
        view.setTextColor(R.id.text1, Color.rgb(200, 0, 0));
        }

and it don't work i get white not red. I will appreciate a good answer, Thanks.

UPDATE: I solved the problem thanks for your answers.(It did not update because i had a configure activity. It just copy/paste the color changer code in the configure activity and updated the views from there.)

just use this one

view.setTextColor(activity.getResources().getColor(R.color.white));
    view.setTextColor(activity.getResources().getColor(R.color.red));
    view.setTextColor(activity.getResources().getColor(R.color.green));

and shave color value in res/values/colors.xml

colors.xml

<?xml version="1.0" encoding="utf-8"?>
 <resources> 

     <color name="white">#fff</color>
     <color name="red">#DF1111</color>
     <color name="green">#63DF11</color>

  </resources> 

in Widget try this one

Widget.setBackgroundColor(Color.RED); 

and this

remoteView.setInt(R.id.message, "setBackgroundColor" android.graphics.Color.BLACK);

What about this

if (colornumb == 1)
        {
        //view.setTextColor(R.id.text1, Color.RED);
        view.setTextColor(Color.parseColor("red"));
        }

It did not update because i had a configure activity. It just copy/paste the color changer code in the configure activity and updated the views from there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM