Color Changer
package com.example.buttoncolor;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b1= findViewById(R.id.btnRed);
Button b2= findViewById(R.id.btnOrange);
Button b3= findViewById(R.id.btnGreen);
Button b4= findViewById(R.id.btnBlue);
Button b5= findViewById(R.id.btnPink);
Button b6= findViewById(R.id.btnYellow);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
setMyScreenColor(Color.RED);
}
});
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
setMyScreenColor(0xFFeb5c2d);
}
});
b3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
setMyScreenColor(Color.GREEN);
}
});
b4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
setMyScreenColor(Color.BLUE);
}
});
b5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//0xFF is written before writing the color code :
setMyScreenColor(0xFFe81099);
}
});
b6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
setMyScreenColor(Color.YELLOW);
}
});
}
public void setMyScreenColor(int color){
View view =this.getWindow().getDecorView();
view .setBackgroundColor(color);
}
}
Image: