Skip to content

Instantly share code, notes, and snippets.

@MikeySmash
Last active May 4, 2019 11:43
Show Gist options
  • Save MikeySmash/c3cc7a4146696831dc57921d7d254495 to your computer and use it in GitHub Desktop.
Save MikeySmash/c3cc7a4146696831dc57921d7d254495 to your computer and use it in GitHub Desktop.
RadioButton changes background color
package com.example.android.quizapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onSelect(View view) {
RadioButton bgRadioButton1 = (RadioButton) findViewById(R.id.q1_answer1);
boolean isRadioButton1Selected = bgRadioButton1.isChecked();
RadioButton bgRadioButton2 = (RadioButton) findViewById(R.id.q1_answer2);
boolean isRadioButton2Selected = bgRadioButton2.isChecked();
RadioButton bgRadioButton3 = (RadioButton) findViewById(R.id.q1_answer3);
boolean isRadioButton3Selected = bgRadioButton3.isChecked();
RadioButton bgRadioButton4 = (RadioButton) findViewById(R.id.q1_answer4);
boolean isRadioButton4Selected = bgRadioButton4.isChecked();
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch (view.getId()) {
case R.id.q1_answer1:
if (checked)
bgRadioButton1.setBackgroundColor(getResources().getColor(R.color.radio_bg));
bgRadioButton2.setBackgroundColor(getResources().getColor(R.color.main_bg));
bgRadioButton3.setBackgroundColor(getResources().getColor(R.color.main_bg));
bgRadioButton4.setBackgroundColor(getResources().getColor(R.color.main_bg));
break;
case R.id.q1_answer2:
if (checked)
bgRadioButton1.setBackgroundColor(getResources().getColor(R.color.main_bg));
bgRadioButton2.setBackgroundColor(getResources().getColor(R.color.radio_bg));
bgRadioButton3.setBackgroundColor(getResources().getColor(R.color.main_bg));
bgRadioButton4.setBackgroundColor(getResources().getColor(R.color.main_bg));
break;
case R.id.q1_answer3:
if (checked)
bgRadioButton1.setBackgroundColor(getResources().getColor(R.color.main_bg));
bgRadioButton2.setBackgroundColor(getResources().getColor(R.color.main_bg));
bgRadioButton3.setBackgroundColor(getResources().getColor(R.color.radio_bg));
bgRadioButton4.setBackgroundColor(getResources().getColor(R.color.main_bg));
break;
case R.id.q1_answer4:
if (checked)
bgRadioButton1.setBackgroundColor(getResources().getColor(R.color.main_bg));
bgRadioButton2.setBackgroundColor(getResources().getColor(R.color.main_bg));
bgRadioButton3.setBackgroundColor(getResources().getColor(R.color.main_bg));
bgRadioButton4.setBackgroundColor(getResources().getColor(R.color.radio_bg));
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment