LinearLayout abc = findViewById(R.id.view);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener((new View.OnClickListener() {
@Override
public void onClick(View v) {
int max = 1;
int min = 225;
Random rand = new Random();
int a = rand.nextInt((max - min) + 1) + min;
int b = rand.nextInt((max - min) + 1) + min;
int c = rand.nextInt((max - min) + 1) + min;
int d = rand.nextInt((max - min) + 1) + min;
abc.setBackgroundColor(Color.argb(a, b, c, d));
}
}));
I tried with Switch too but my app is closed or return to previous Activity.
Change Background Color of a Layout
If your requirement is changing Background color of a layout Dynamically means, Try any of the below
If you are outside an activity
If you are on activity
If you want to set color from Android Graphics color
If you want to set color from using Hexa Code
OR If you want to set color randomly
and this is the usage:
abc.setBackgroundColor(Int.randomColor());