yahya zaki
yahya zaki

Reputation: 1

Getting a RadioButton's value

How can I get the values of my simple radio button quiz to a new XML layout page. I'm not sure what happened. When i click on the button, it force closes.

TextView hasil;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button btnSubmit = (Button) findViewById(R.id.submit);
    btnSubmit.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View view){
            int score = 0;
            if (((RadioButton)findViewById(R.id.radioButton1)).isChecked()) {score++;}
            if (((RadioButton)findViewById(R.id.radioButton8)).isChecked()) {score++;}
            if (((RadioButton)findViewById(R.id.radioButton11)).isChecked()) {score++;}
            if (((RadioButton) findViewById(R.id.radioButton16)).isChecked()){score++;}
            if (((RadioButton) findViewById(R.id.radioButton19)).isChecked()){score++;}
            if (((RadioButton) findViewById(R.id.radioButton24)).isChecked()){score++;}
            if (((RadioButton) findViewById(R.id.radioButton25)).isChecked()){score++;}
            if (((RadioButton) findViewById(R.id.radioButton29)).isChecked()){score++;}

            displayResult(score);
        }

    });
}

private void displayResult(int score) {
    String message = "You scored " + score;
    message += " out of 6";
    message += "\nWell done!";
    hasil.setText(message);
}

Upvotes: 0

Views: 61

Answers (2)

bnayagrawal
bnayagrawal

Reputation: 1154

You should add logcat message so that one can know what's caused your app to crash.

However if you want to know which radio button is checked, you can try the code below.

int score=0;
RadioGroup rg = (RadioGroup)findViewById(R.id.YOUR_RADIO_GROUP_ID);
switch (rg.getCheckedRadioButtonId()) {
                case R.id.radioButton1:
                    score++;
                    break;
                case R.id.radioButton8:
                    score++;
                    break;
                case R.id.radioButton11:
                    score++;
                    break;
                case R.id.radioButton16:
                    score++;
                    break;
                case R.id.radioButton19:
                    score++;
                    break;
                case R.id.radioButton24:
                    score++;
                    break;
                case R.id.radioButton25:
                    score++;
                    break;
                case R.id.radioButton29:
                    score++;
                    break;
                default:
                    break;
            }

I assume you have placed your radio buttons inside radio group.

Upvotes: 1

Lakshmikant Deshpande
Lakshmikant Deshpande

Reputation: 844

You don't seem to have initialized hasil TextView. Add the following line in OnCreate method, by replacing HASIL_ID with the appropriate ID.

hasil = (TextView) findViewById(R.id.HASIL_ID);

Upvotes: 1

Related Questions