I'm new to android, so basically i made a really simple Exam, consisting of 3 questions, each has 2 answers. But everytime I check one, then "realize" I made a mistake, and want to change my choice, the first choice does not disappear. So I'm left with 2 checked answers.
Below is my main activity Java code:
public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener, View.OnClickListener {
RadioGroup rg1, rg2, rg3;
ImageView img;
Button submit;
boolean q1=false, q2=false, q3=false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rg1 = (RadioGroup)findViewById(R.id.rg1);
rg2 = (RadioGroup)findViewById(R.id.rg2);
rg3 = (RadioGroup)findViewById(R.id.rg3);
rg1.setOnCheckedChangeListener(this);
rg2.setOnCheckedChangeListener(this);
rg3.setOnCheckedChangeListener(this);
img = (ImageView)findViewById(R.id.img_id);
submit = (Button)findViewById(R.id.submit);
submit.setOnClickListener(this);
}
@Override
public void onCheckedChanged(RadioGroup rg, int id) {
if(rg == rg1){
q1= id == R.id.q1a1;
}
else if(rg==rg2) {
q2 = id == R.id.q2a1;
}
else if(rg==rg3) {
q3 = id == R.id.q3a1;
}
}
@Override
public void onClick(View v) {
if(v==submit)
{
if(q1&&q2&&q3)
img.setImageResource(R.drawable.img_1);
else
img.setImageResource(R.drawable.img_2);
}
}
}