Dynamically change background color (Android)

I have the following code:

public class MainActivity extends Activity {

private BufferedReader br;
private Socket s;
private View v,v1;
private RelativeLayout rl;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    try {
        s = new Socket("192.168.1.36",50000);
        br = new BufferedReader(new InputStreamReader(s.getInputStream()));
    }catch(Exception e){e.printStackTrace();}
    color();
}


private void color(){

    rl = (RelativeLayout) this.findViewById(R.id.rellay);
    while(true){
        try{

            String received = br.readLine();
            if(received != null){
            //  System.out.println(received);
            String[] color = received.split(",");
                            setColor(color);

            }
        }catch(Exception e){e.printStackTrace();}
    }
}
private void setColor(String[] color){
    rl = (RelativeLayout) this.findViewById(R.id.rellay);
    int red = Integer.parseInt(color[0]); 
    int green = Integer.parseInt(color[1]);
    int blue = Integer.parseInt(color[2]);
    int a = Integer.parseInt(color[3]);
    rl.setBackgroundColor(Color.argb(a, red, green, blue));

What I want to do is get 4 values ​​separated by commas (this works), and I want the values ​​in the range 0-255 to be RGB color.

I want to change the background of Android activity. I can change the color once, from the onCreate method, but when I try to change it later on, I get a white screen by default. Values ​​never exceed 255.

How can I do that? Thank!!

+3
source share
1 answer

Make sure the alpha value! = 0. The alpha value represents the transparency of the color, so 0 is completely transparent. try starting with fixed values, for example:

rl.setBackgroundColor(Color.argb(255, 0, 0, 0));

, . .

, . , .

0

All Articles