0

I want to make a quiz app that can calculate the sum of the number in radio button that user click. How to caluclate the sum and how can I display the result in another layout after user click the button to generate the result?

Question page Result page

Here is the code for question layout (activity_main.xml):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/instruction_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/instruction" />

    // Question 1

    <TextView
        android:id="@+id/question1_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/question1" />

    <RadioGroup
        android:id="@+id/question1_radiogroup"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <RadioButton
                android:id="@+id/question1_radioButton1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:onClick="onRadioButtonQuestion1Clicked"
                android:text="1" />

            <RadioButton
                android:id="@+id/question1_radioButton2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="2" />

            <RadioButton
                android:id="@+id/question1_radioButton3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:onClick="onRadioButtonQuestion1Clicked"
                android:text="3" />

            <RadioButton
                android:id="@+id/question1_radioButton4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:onClick="onRadioButtonQuestion1Clicked"
                android:text="4" />

            <RadioButton
                android:id="@+id/question1_radioButton5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:onClick="onRadioButtonQuestion1Clicked"
                android:text="5" />

            <RadioButton
                android:id="@+id/question1_radioButton6"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:onClick="onRadioButtonQuestion1Clicked"
                android:text="6" />

        <RadioButton
            android:id="@+id/question1_radioButton7"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="onRadioButtonQuestion1Clicked"
            android:text="7" />

    </LinearLayout>
</RadioGroup>

// Question 2

<TextView
    android:id="@+id/question2_text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/question2" />

<RadioGroup
    android:id="@+id/question2_radiogroup"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <RadioButton
            android:id="@+id/question2_radioButton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="onRadioButtonQuestion2Clicked"
            android:text="1" />

        <RadioButton
            android:id="@+id/question2_radioButton2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="onRadioButtonQuestion2Clicked"
            android:text="2" />

        <RadioButton
            android:id="@+id/question2_radioButton3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="onRadioButtonQuestion2Clicked"
            android:text="3" />

        <RadioButton
            android:id="@+id/question2_radioButton4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="onRadioButtonQuestion2Clicked"
            android:text="4" />

        <RadioButton
            android:id="@+id/question2_radioButton5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="onRadioButtonQuestion2Clicked"
            android:text="5" />

        <RadioButton
            android:id="@+id/question2_radioButton6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="onRadioButtonQuestion2Clicked"
            android:text="6" />

        <RadioButton
            android:id="@+id/question2_radioButton7"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="onRadioButtonQuestion2Clicked"
            android:text="7" />

    </LinearLayout>
</RadioGroup>

// Question 3

<TextView
    android:id="@+id/question3_text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/question3" />

<RadioGroup
    android:id="@+id/question3_radiogroup"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <RadioButton
            android:id="@+id/question3_radioButton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="onRadioButtonQuestion3Clicked"
            android:text="1" />

        <RadioButton
            android:id="@+id/question3_radioButton2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="onRadioButtonQuestion3Clicked"
            android:text="2" />

        <RadioButton
            android:id="@+id/question3_radioButton3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="onRadioButtonQuestion3Clicked"
            android:text="3" />

        <RadioButton
            android:id="@+id/question3_radioButton4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="onRadioButtonQuestion3Clicked"
            android:text="4" />

        <RadioButton
            android:id="@+id/question3_radioButton5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="onRadioButtonQuestion3Clicked"
            android:text="5" />

        <RadioButton
            android:id="@+id/question3_radioButton6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="onRadioButtonQuestion3Clicked"
            android:text="6" />

        <RadioButton
            android:id="@+id/question3_radioButton7"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="onRadioButtonQuestion3Clicked"
            android:text="7" />

    </LinearLayout>
</RadioGroup>

// Question 4

<TextView
    android:id="@+id/question4_text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/question4" />

<RadioGroup
    android:id="@+id/question4_radiogroup"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <RadioButton
            android:id="@+id/question4_radioButton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="onRadioButtonQuestion4Clicked"
            android:text="1" />

        <RadioButton
            android:id="@+id/question4_radioButton2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="onRadioButtonQuestion4Clicked"
            android:text="2" />

        <RadioButton
            android:id="@+id/question4_radioButton3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="onRadioButtonQuestion4Clicked"
            android:text="3" />

        <RadioButton
            android:id="@+id/question4_radioButton4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="onRadioButtonQuestion4Clicked"
            android:text="4" />

        <RadioButton
            android:id="@+id/question4_radioButton5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="onRadioButtonQuestion4Clicked"
            android:text="5" />

        <RadioButton
            android:id="@+id/question4_radioButton6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="onRadioButtonQuestion4Clicked"
            android:text="6" />

        <RadioButton
            android:id="@+id/question4_radioButton7"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="7" />

    </LinearLayout>
</RadioGroup>

// Question 5

<TextView
    android:id="@+id/question5_text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/question5" />

<RadioGroup
    android:id="@+id/question5_radiogroup"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <RadioButton
            android:id="@+id/question5_radioButton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="onRadioButtonQuestion5Clicked"
            android:text="1" />

        <RadioButton
            android:id="@+id/question5_radioButton2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="onRadioButtonQuestion5Clicked"
            android:text="2" />

        <RadioButton
            android:id="@+id/question5_radioButton3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="onRadioButtonQuestion5Clicked"
            android:text="3" />

        <RadioButton
            android:id="@+id/question5_radioButton4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="onRadioButtonQuestion5Clicked"
            android:text="4" />

        <RadioButton
            android:id="@+id/question5_radioButton5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="onRadioButtonQuestion5Clicked"
            android:text="5" />

        <RadioButton
            android:id="@+id/question5_radioButton6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="onRadioButtonQuestion5Clicked"
            android:text="6" />

        <RadioButton
            android:id="@+id/question5_radioButton7"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="onRadioButtonQuestion5Clicked"
            android:text="7" />

    </LinearLayout>
</RadioGroup>

<Button
    android:id="@+id/jana_keputusan_button"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Jana Keputusan" />

</LinearLayout>

Here is the code for the question activity (MainActivity.java):

package com.android.resilien.resilienminver;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;

public class MainActivity extends AppCompatActivity {

public int score = 0;

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

    final Button generate_result_button = findViewById(R.id.jana_keputusan_button);
    generate_result_button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Intent generate_result_intent = new Intent(MainActivity.this, ResultActivity.class);
            startActivity(generate_result_intent);
        }
    });
}

// Question 1 Radio Button clicked
public void onRadioButtonQuestion1Clicked(View view) {
    // Is the button checked?
    boolean checked = ((RadioButton) view).isChecked();

    // Check which radio button was clicked
    switch(view.getId()) {
        case R.id.question1_radioButton1:
            if(checked) {score = score + 1;}
            break;
        case R.id.question1_radioButton2:
            if(checked) {score = score + 2;}
            break;
        case R.id.question1_radioButton3:
            if(checked) {score = score + 3;}
            break;
        case R.id.question1_radioButton4:
            if(checked) {score = score + 4;}
            break;
        case R.id.question1_radioButton5:
            if(checked) {score = score + 5;}
            break;
        case R.id.question1_radioButton6:
            if(checked) {score = score + 6;}
            break;
        case R.id.question1_radioButton7:
            if(checked) {score = score + 7;}
            break;
    }
}

// Question 2 Radio Button clicked
public void onRadioButtonQuestion2Clicked(View view) {
    // Is the button checked?
    boolean checked = ((RadioButton) view).isChecked();

    // Check which radio button was clicked
    switch(view.getId()) {
        case R.id.question2_radioButton1:
            if(checked) {score = score + 1;}
            break;
        case R.id.question2_radioButton2:
            if(checked) {score = score + 2;}
            break;
        case R.id.question2_radioButton3:
            if(checked) {score = score + 3;}
            break;
        case R.id.question2_radioButton4:
            if(checked) {score = score + 4;}
            break;
        case R.id.question2_radioButton5:
            if(checked) {score = score + 5;}
            break;
        case R.id.question2_radioButton6:
            if(checked) {score = score + 6;}
            break;
        case R.id.question2_radioButton7:
            if(checked) {score = score + 7;}
            break;
    }
}

// Question 3 Radio Button clicked
public void onRadioButtonQuestion3Clicked(View view) {
    // Is the button checked?
    boolean checked = ((RadioButton) view).isChecked();

    // Check which radio button was clicked
    switch(view.getId()) {
        case R.id.question3_radioButton1:
            if(checked) {score = score + 1;}
            break;
        case R.id.question3_radioButton2:
            if(checked) {score = score + 2;}
            break;
        case R.id.question3_radioButton3:
            if(checked) {score = score + 3;}
            break;
        case R.id.question3_radioButton4:
            if(checked) {score = score + 4;}
            break;
        case R.id.question3_radioButton5:
            if(checked) {score = score + 5;}
            break;
        case R.id.question3_radioButton6:
            if(checked) {score = score + 6;}
            break;
        case R.id.question3_radioButton7:
            if(checked) {score = score + 7;}
            break;
    }
}

// Question 4 Radio Button clicked
public void onRadioButtonQuestion4Clicked(View view) {
    // Is the button checked?
    boolean checked = ((RadioButton) view).isChecked();

    // Check which radio button was clicked
    switch(view.getId()) {
        case R.id.question4_radioButton1:
            if(checked) {score = score + 1;}
            break;
        case R.id.question4_radioButton2:
            if(checked) {score = score + 2;}
            break;
        case R.id.question4_radioButton3:
            if(checked) {score = score + 3;}
            break;
        case R.id.question4_radioButton4:
            if(checked) {score = score + 4;}
            break;
        case R.id.question4_radioButton5:
            if(checked) {score = score + 5;}
            break;
        case R.id.question4_radioButton6:
            if(checked) {score = score + 6;}
            break;
        case R.id.question4_radioButton7:
            if(checked) {score = score + 7;}
            break;
    }
}

// Question 5 Radio Button clicked
public void onRadioButtonQuestion5Clicked(View view) {
    // Is the button checked?
    boolean checked = ((RadioButton) view).isChecked();

    // Check which radio button was clicked
    switch(view.getId()) {
        case R.id.question5_radioButton1:
            if(checked) {score = score + 1;}
            break;
        case R.id.question5_radioButton2:
            if(checked) {score = score + 2;}
            break;
        case R.id.question5_radioButton3:
            if(checked) {score = score + 3;}
            break;
        case R.id.question5_radioButton4:
            if(checked) {score = score + 4;}
            break;
        case R.id.question5_radioButton5:
            if(checked) {score = score + 5;}
            break;
        case R.id.question5_radioButton6:
            if(checked) {score = score + 6;}
            break;
        case R.id.question5_radioButton7:
            if(checked) {score = score + 7;}
            break;
    }
}

}

  • Do you mean how do you send the result to another layout after the user presses the radio button? – Calco May 25 '18 at 08:01
  • for displaying result in next `activity` layout. You can use `Intent` refer this https://stackoverflow.com/questions/14292398/how-to-pass-data-from-2nd-activity-to-1st-activity-when-pressed-back-android – Kopi Bryant May 25 '18 at 08:03

3 Answers3

1

First i would strongly recommend to sum up the click method for all RadioButtons to one single method, e.g. onRadioAction:

The layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/instruction_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/instruction" />

    // Question 1

    <TextView
        android:id="@+id/question1_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/question1" />

    <RadioGroup
        android:id="@+id/question1_radiogroup"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <RadioButton
                android:id="@+id/question1_radioButton1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:onClick="radioButtonAction"
                android:text="1" />

            <RadioButton
                android:id="@+id/question1_radioButton2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="2" />

            <RadioButton
                android:id="@+id/question1_radioButton3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:onClick="radioButtonAction"
                android:text="3" />

            <RadioButton
                android:id="@+id/question1_radioButton4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:onClick="radioButtonAction"
                android:text="4" />

            <RadioButton
                android:id="@+id/question1_radioButton5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:onClick="radioButtonAction"
                android:text="5" />

            <RadioButton
                android:id="@+id/question1_radioButton6"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:onClick="radioButtonAction"
                android:text="6" />

        <RadioButton
            android:id="@+id/question1_radioButton7"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="radioButtonAction"
            android:text="7" />

    </LinearLayout>
</RadioGroup>

// Question 2

<TextView
    android:id="@+id/question2_text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/question2" />

<RadioGroup
    android:id="@+id/question2_radiogroup"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <RadioButton
            android:id="@+id/question2_radioButton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="radioButtonAction"
            android:text="1" />

        <RadioButton
            android:id="@+id/question2_radioButton2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="radioButtonAction"
            android:text="2" />

        <RadioButton
            android:id="@+id/question2_radioButton3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="radioButtonAction"
            android:text="3" />

        <RadioButton
            android:id="@+id/question2_radioButton4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="radioButtonAction"
            android:text="4" />

        <RadioButton
            android:id="@+id/question2_radioButton5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="radioButtonAction"
            android:text="5" />

        <RadioButton
            android:id="@+id/question2_radioButton6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="radioButtonAction"
            android:text="6" />

        <RadioButton
            android:id="@+id/question2_radioButton7"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="radioButtonAction"
            android:text="7" />

    </LinearLayout>
</RadioGroup>

// Question 3

<TextView
    android:id="@+id/question3_text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/question3" />

<RadioGroup
    android:id="@+id/question3_radiogroup"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <RadioButton
            android:id="@+id/question3_radioButton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="radioButtonAction"
            android:text="1" />

        <RadioButton
            android:id="@+id/question3_radioButton2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="radioButtonAction"
            android:text="2" />

        <RadioButton
            android:id="@+id/question3_radioButton3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="radioButtonAction"
            android:text="3" />

        <RadioButton
            android:id="@+id/question3_radioButton4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="radioButtonAction"
            android:text="4" />

        <RadioButton
            android:id="@+id/question3_radioButton5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="radioButtonAction"
            android:text="5" />

        <RadioButton
            android:id="@+id/question3_radioButton6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="radioButtonAction"
            android:text="6" />

        <RadioButton
            android:id="@+id/question3_radioButton7"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="radioButtonAction"
            android:text="7" />

    </LinearLayout>
</RadioGroup>

// Question 4

<TextView
    android:id="@+id/question4_text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/question4" />

<RadioGroup
    android:id="@+id/question4_radiogroup"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <RadioButton
            android:id="@+id/question4_radioButton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="radioButtonAction"
            android:text="1" />

        <RadioButton
            android:id="@+id/question4_radioButton2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="radioButtonAction"
            android:text="2" />

        <RadioButton
            android:id="@+id/question4_radioButton3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="radioButtonAction"
            android:text="3" />

        <RadioButton
            android:id="@+id/question4_radioButton4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="radioButtonAction"
            android:text="4" />

        <RadioButton
            android:id="@+id/question4_radioButton5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="radioButtonAction"
            android:text="5" />

        <RadioButton
            android:id="@+id/question4_radioButton6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="radioButtonAction"
            android:text="6" />

        <RadioButton
            android:id="@+id/question4_radioButton7"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="7" />

    </LinearLayout>
</RadioGroup>

// Question 5

<TextView
    android:id="@+id/question5_text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/question5" />

<RadioGroup
    android:id="@+id/question5_radiogroup"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <RadioButton
            android:id="@+id/question5_radioButton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="radioButtonAction"
            android:text="1" />

        <RadioButton
            android:id="@+id/question5_radioButton2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="radioButtonAction"
            android:text="2" />

        <RadioButton
            android:id="@+id/question5_radioButton3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="radioButtonAction"
            android:text="3" />

        <RadioButton
            android:id="@+id/question5_radioButton4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="radioButtonAction"
            android:text="4" />

        <RadioButton
            android:id="@+id/question5_radioButton5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="radioButtonAction"
            android:text="5" />

        <RadioButton
            android:id="@+id/question5_radioButton6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="radioButtonAction"
            android:text="6" />

        <RadioButton
            android:id="@+id/question5_radioButton7"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="radioButtonAction"
            android:text="7" />

    </LinearLayout>
</RadioGroup>

<Button
    android:id="@+id/jana_keputusan_button"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Jana Keputusan" />

</LinearLayout>

The activity:

package com.android.resilien.resilienminver;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;

public class MainActivity extends AppCompatActivity {

public int score = 0;

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

    final Button generate_result_button = findViewById(R.id.jana_keputusan_button);
    generate_result_button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Intent generate_result_intent = new Intent(MainActivity.this, ResultActivity.class);
            startActivity(generate_result_intent);
        }
    });
}



public void radioButtonAction(View view) {
    // Is the button checked?
    boolean checked = ((RadioButton) view).isChecked();

    // Check which radio button was clicked
    switch(view.getId()) {
        case R.id.question5_radioButton1:
            if(checked) {score = score + 1;}
            break;
        case R.id.question5_radioButton2:
            if(checked) {score = score + 2;}
            break;
        case R.id.question5_radioButton3:
            if(checked) {score = score + 3;}
            break;
        case R.id.question5_radioButton4:
            if(checked) {score = score + 4;}
            break;
        case R.id.question5_radioButton5:
            if(checked) {score = score + 5;}
            break;
        case R.id.question5_radioButton6:
            if(checked) {score = score + 6;}
            break;
        case R.id.question5_radioButton7:
            if(checked) {score = score + 7;}
            break;
    }
}

To extend your app i would also recommend, that you generate the layout for questions dynamically in your source code, not as hardcoded layout xml.

Finally to display the result e.g. add a Button to the bottom and set the following code to display a dialog with the achieved score. As alternative you can also start a new Activity and show it there via Intent and Bundle.

    private onClickedButtonResult(View v) {     
    AlertDialog alertDialog = new AlertDialog.Builder(this)
        //set icon 
         .setIcon(android.R.drawable.ic_dialog_alert)
        //set title
        .setTitle("Your score")
        //set message
        .setMessage("You achieved a score of " + String.valueOf(score) + "!")
        //set positive button
        .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
           //Call finish to close your app   
                finish();
            }
        })
        .show();
}

Alternative:
Show score in another Activity
Create an Intent for the other Activity and save the value of score in the intent:

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

    final Button generate_result_button = findViewById(R.id.jana_keputusan_button);
    generate_result_button.setOnClickListener(new View.OnClickListener() {
       public void onClick(View v) {
         Intent generate_result_intent = new Intent(MainActivity.this, ResultActivity.class);
         Bundle extras = generate_result_intent.getExtras();
         extras.putExtra("SCORE", score);  
         startActivity(generate_result_intent);
       }
    });

}

And in your other Activity you can read the passed value of score and show it to the user:

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

    Intent mIntent = getIntent();
    if (i.hasExtra("SCORE")) {
       int score = mIntent.getIntExtra("SCORE", 0);
       // Do something to display the score
    }
}

Finally, be aware that the user can increase his own score by clicking multiple times on a RadioButton. That should be addressed in your application logic, because the scoring would be invalid.

alex
  • 5,516
  • 2
  • 36
  • 60
  • `putExtra` is a Method of `Intent` documented in the attached link. Can you please post your code, wehre you use `putExtra`. https://developer.android.com/reference/android/content/Intent#putextra_16 – alex May 28 '18 at 07:32
0

To get text from RadioButton I offer you to use:

int indexOfButton = radioGroup.indexOfChild(findViewById(radioGroup.getCheckedRadioButtonId()));
String scoreS = (indexOfButton > -1)? ((RadioButton) radioGroup.getChildAt(indexOfButton)).getText().toString() : "";
score += Integer.parseInt(scoreS);

This methods is getting checked RadioButton id and by it finds index in that group. By the index you can get RadioButton text.

To show the score in other activity you can send data throw Intent extras. Like this:

Intent intent = new Intent(MainActivity.this, ResultActivity.class);
intent.putExtra("result", score);
startActivity(intent);

On ResultActivity.class you can get score from extras like this:

Integer result = 0;
Intent i = getIntent();
if (i.hasExtra("result")) {
    result = i.getIntExtra("result");
}

Then you can display it in TextView or in other possible ways.

If you don't understand something, feel free to ask.
I hope it helps you...

Dumbo
  • 1,630
  • 18
  • 33
  • how to display the result as TextView? final TextView result_TextView = (TextView) findViewById(R.id.score_text); int score = 0; Intent result_intent = getIntent(); Bundle bundle = result_intent.getExtras(); if (result_intent.hasExtra("result")) { score = result_intent.getIntExtra("score", 0); result_TextView.setText("Skor: "+ score); } – Ruzain Irfan May 26 '18 at 04:17
  • It looks like everything is pretty good. Except don't define `TextView` as `final`. It says that object is final and you would not be able to change his value later. It calls *blanck final* because you creating it empty. And I think you don't need to create `Bundle` object if you not using it. – Dumbo May 26 '18 at 05:11
  • **final** TextView result_TextView = (TextView) findViewById(R.id.score_text); is totally fine. `final` works here as immutable reference. That means, that a reference to the `TextView` score_text is immutable, but the `TextView` itself can be modified. Its rather a _bad_ practice to _not_ use `final` here, because this reference shouldn't be changed later. This brings advantages like null-safety, reducing side-effects etc. – alex May 28 '18 at 07:37
0

You can go to another activity and display result by INTENT. You can pass your score to another activity by using putExtra.

MainActivity:

okAction.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {   
        Intent intent = new Intent(MainActivity.this, AnotherActivity.class);   
        intent.putExtra("Score", score);
        startActivity(intent);
    }
});

Another Activity:

Intent intent = getIntent();
Bundle bundle = intent.getExtras();
if(bundle != null){
   int score = bundle.getInt("Score");
   restltText.setText("Your score is: "+ score);
}
Sheikh Hasib
  • 7,423
  • 2
  • 25
  • 37