I have a radiogroup where each radiobutton changes text after I press the "submit" button. Each button corresponds to a score. But after I press "submit" the radio button that was previously selected is staying selected on the next screen when the radio button text changes. How do I unselect the radiogroup completely? I assume that I add the java code to my action listener part of my code?
public class MainActivity extends AppCompatActivity {
public int score = 0; //answer score
public int PreviousScore;
int i = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView Score = (TextView) findViewById(R.id.Score);
Button b = (Button)findViewById(R.id.button); //button
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
question1(); //call params for question
//button listener, when button clicked, produce output on textfield "Score"
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
PreviousScore = score;
Score.setText(String.valueOf(action()));
i++;
question1();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void question1(){
if(i>3){
findViewById(R.id.Score).setVisibility(View.VISIBLE);
TextView t = (TextView) findViewById(R.id.Question);
t.setText("Final Score");
Button AnswerA = (Button) findViewById(R.id.AnswerA);
AnswerA.setVisibility(View.INVISIBLE);
Button AnswerB = (Button) findViewById(R.id.AnswerB);
AnswerB.setVisibility(View.INVISIBLE);
Button AnswerC = (Button) findViewById(R.id.AnswerC);
AnswerC.setVisibility(View.INVISIBLE);
Button AnswerD = (Button) findViewById(R.id.AnswerD);
AnswerD.setVisibility(View.INVISIBLE);
Button b = (Button)findViewById(R.id.button);
b.setVisibility(View.INVISIBLE);
}
else {
findViewById(R.id.Score).setVisibility(View.GONE);
TextView t = (TextView) findViewById(R.id.Question);
TextView A = (TextView) findViewById(R.id.AnswerA);
TextView B = (TextView) findViewById(R.id.AnswerB);
TextView C = (TextView) findViewById(R.id.AnswerC);
TextView D = (TextView) findViewById(R.id.AnswerD);
t.setText(question[i]);
A.setText(answerA[i]);
B.setText(answerB[i]);
C.setText(answerC[i]);
D.setText(answerD[i]);
}
}
public void onRadioButtonClicked(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
if(i<2) {
switch (view.getId()) {
case R.id.AnswerA:
if (checked)
score = PreviousScore + 2;
break;
case R.id.AnswerB:
if (checked)
score = PreviousScore + 4;
break;
case R.id.AnswerC:
if (checked)
score = PreviousScore + 6;
break;
case R.id.AnswerD:
if (checked)
score = PreviousScore + 8;
break;
}
}
else if(i>=2)
switch(view.getId()) {
case R.id.AnswerA:
if (checked)
score = PreviousScore + 1;
break;
case R.id.AnswerB:
if (checked)
score = PreviousScore + 2;
break;
case R.id.AnswerC:
if (checked)
score = PreviousScore + 3;
break;
case R.id.AnswerD:
if (checked)
score = PreviousScore + 4;
break;
}
}
public String action(){
String action = null;
if (score <= 11)
action= "Low Risk";
else if(score < 15)
action = "Medium Risk";
else if(score > 15)
action = "High Risk";
return action;
}
}
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="@+id/Question"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
android:id="@+id/button"
android:layout_marginBottom="132dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/Score"
android:text="score goes here"
android:layout_above="@+id/radioGroup"
android:layout_centerHorizontal="true"
android:textSize="30dp" />
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/radioGroup"
android:layout_above="@+id/button"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New RadioButton"
android:id="@+id/AnswerA"
android:layout_below="@+id/Question"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="41dp"
android:checked="false"
android:onClick="onRadioButtonClicked"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New RadioButton"
android:id="@+id/AnswerB"
android:layout_below="@+id/AnswerA"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:checked="false"
android:onClick="onRadioButtonClicked"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New RadioButton"
android:id="@+id/AnswerC"
android:layout_below="@+id/AnswerB"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:checked="false"
android:onClick="onRadioButtonClicked"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New RadioButton"
android:id="@+id/AnswerD"
android:layout_below="@+id/AnswerC"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:checked="false"
android:onClick="onRadioButtonClicked"/>
</RadioGroup>