Change Button Color When Clicked Android Studio

Viewed 283

How can I change the color of the button when it's clicked, I'm making a quiz app and when the button is clicked it will change its color

2 Answers

You can use a MaterialButtonToggleGroup to select from a group of choices:

        <com.google.android.material.button.MaterialButtonToggleGroup
            android:id="@+id/toggleButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:singleSelection="true"
            app:selectionRequired="true">
            <Button
                android:id="@+id/button1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Button 1"
                style="?attr/materialButtonOutlinedStyle"
                />
            <Button
                android:id="@+id/button2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Button 2"
                style="?attr/materialButtonOutlinedStyle"
                />
            <Button
                android:id="@+id/button3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Button 3"
                style="?attr/materialButtonOutlinedStyle"
                />
        </com.google.android.material.button.MaterialButtonToggleGroup>

enter image description here enter image description here

Just set View.OnClickListener to your Button and setBackgroundColor to it.

Button button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        v.setBackgroundColor(Color.parseColor("#ff0000"));
    }
});

If you want to reset color when the other Button is clicked, you can use a common OnClickListener among the Buttons. Below is an example:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private Button button1;
    private Button button2;
    private Button button3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button1 = findViewById(R.id.button1);
        button2 = findViewById(R.id.button2);
        button3 = findViewById(R.id.button3);
        button1.setOnClickListener(this);
        button2.setOnClickListener(this);
        button3.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button1:
                button1.setBackgroundColor(Color.parseColor("#ff0000"));
                button2.setBackgroundColor(Color.parseColor("#0000ff"));
                button3.setBackgroundColor(Color.parseColor("#0000ff"));
                break;
            case R.id.button2:
                button1.setBackgroundColor(Color.parseColor("#0000ff"));
                button2.setBackgroundColor(Color.parseColor("#ff0000"));
                button3.setBackgroundColor(Color.parseColor("#0000ff"));
                break;
            case R.id.button3:
                button1.setBackgroundColor(Color.parseColor("#0000ff"));
                button2.setBackgroundColor(Color.parseColor("#0000ff"));
                button3.setBackgroundColor(Color.parseColor("#ff0000"));
                break;
        }
    }
}
Related