Ashley Keating
Ashley Keating

Reputation: 119

Creating a Test/Questionnaire using Radio-Buttons - Android

I am making an android application that contains a test for depression. The test consists of 9 questions and each question has 4 possible answers. The answers are in the form of radio buttons. I need to assign numeric values to the radio buttons as follows:

Answer 1 = 0 Answer 2 = 1 Answer 3 = 2 Answer 4 = 3

At the end of the test the user will receive a result, 1-10 being likely not depressed and 10-27 being likely depressed.

Each question is asked on a separate page and the result page will contain the result which is the total of each answer added.

Can someone please make a suggestion as to how I should tackle this?

res/values/strings.xml code:

<?xml version="1.0" encoding="utf-8"?>
<resources>

<integer name="int1">0</integer>
<integer name="int2">1</integer>
<integer name="int3">2</integer>
<integer name="int4">3</integer>

</resources>

"Test1" xml code:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white" 
tools:context=".Test1">

<!-- title text -->
<TextView
    android:id="@+id/txtTitle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="37dp"
    android:text="@string/D1title"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<!-- subtitle text -->
<TextView
    android:id="@+id/txtSubTitle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/txtTitle"
    android:layout_centerHorizontal="true"
    android:text="@string/Q1Subtitle"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<!-- question 1 text -->
<TextView
    android:id="@+id/txtQ1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/txtSubTitle"
    android:layout_marginTop="38dp"
    android:text="@string/Q1"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<!-- radio group to allow for only one radio button selection -->
<RadioGroup
    android:id="@+id/radioAnswers1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/txtQ1" >

     <!-- 1st answer radio button-->
    <RadioButton
    android:id="@+id/radioA1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="48dp"
    android:checked="false"
    android:text="@string/zero"
    android:tag="@integer/int1" />

<!-- 2nd answer radio button-->
<RadioButton
    android:id="@+id/radioA2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/one"
    android:tag="@integer/int2" />

<!-- 3rd answer radio button-->
<RadioButton
    android:id="@+id/radioA3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/two"
    android:tag="@integer/int3" />

<!-- 4th answer radio button-->
  <RadioButton
    android:id="@+id/radioA4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/three"
    android:tag="@integer/int4" />

</RadioGroup>

 <!-- next button -->
<Button
    android:id="@+id/btnNext"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_toRightOf="@+id/txtTitle"
    android:text="@string/next" />

</RelativeLayout>

"Test1" java code:

package com.lifematters;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Toast;

public class Test1 extends Activity {

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

    //define Navigation Image Buttons
    final Button nextBtn = (Button) findViewById(R.id.btnNext);

    final RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioAnswers1);
    final RadioButton checkedButton = ((RadioButton)findViewById(radioGroup.getCheckedRadioButtonId()));

    static int result;




    radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener()
    {
        public void onCheckedChanged (RadioGroup group, int checkedId){

        final RadioButton radioButton1 = (RadioButton) findViewById(R.id.radioA1);
        final RadioButton radioButton2 = (RadioButton) findViewById(R.id.radioA2);
        final RadioButton radioButton3 = (RadioButton) findViewById(R.id.radioA3);

        if (radioButton1.isChecked()) {
            displayToast("No, not at all");

        } else if (radioButton2.isChecked()) {
            displayToast("On some days");

        }else if (radioButton3.isChecked()) {
            displayToast("On more than half the days");

        }else {
            displayToast("Nearly everyday");
        }
    }

});


  //Set up listener for Test
   nextBtn.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
            //listener call this function

           checkedButton.getTag(); 

           Intent intent = new Intent(getBaseContext(), Test2.class);
           intent.putExtra("EXTRA_RESULT", result);
           startActivity(intent);

           openTest2();
        }
    });


  }

    //Open test page
    public void openTest2() {
    //create new textview
    Intent i = new Intent(getApplicationContext(), Test2.class);
    startActivity(i);
  }


  public void displayToast(String text){
  Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
}

}

"Results1" XML code:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white" >

<!-- Creating Results Title -->
<TextView
    android:id="@+id/txtTitle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="19dp"
    android:text="@string/resultsTitle"
    android:textAppearance="?android:attr/textAppearanceLarge" />

 <!-- Creating Results Subtitle -->
 <TextView
    android:id="@+id/txtSubtitle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/txtTitle"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="24dp"
    android:text="@string/subtitleR1" />


<!-- Creating Score Label  -->
 <TextView
    android:id="@+id/txtScore"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/txtResults"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="29dp"
    android:text="@string/score1"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<!-- Creating Results Text -->
<TextView
    android:id="@+id/txtResults"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/btnNext"
    android:layout_alignParentLeft="true"
    android:text="@string/results1"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<!-- Creating Next Button -->
<Button
    android:id="@+id/btnNext"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_marginRight="14dp"
    android:text="@string/next" />

</RelativeLayout>

"Results1" java code:

package com.lifematters;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.app.Activity;
import android.content.Intent;

public class Results1 extends Activity {

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

    final Button nextBtn = (Button) findViewById(R.id.btnNext);
            final TextView score = (TextView) findViewById(R.id.txtScore);

            int result;

    result = getText(Test1.result1);


//Set up listener for Next
nextBtn.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
            //listener call this function
            open101();
        }
    });


 }

//Open test page
public void open101() {
//create new textview
Intent i = new Intent(getApplicationContext(), Depression101.class);
 startActivity(i);
 }
}

Upvotes: 1

Views: 3794

Answers (1)

jacobhyphenated
jacobhyphenated

Reputation: 2145

Rather than checking every single radio button to see which one is checked, you can get the only checked radio button from the checked ID.

RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioAnswers1);
RadioButton checkedButton = ((RadioButton)findViewById(radioGroup.getCheckedRadioButtonId() ))

Then you don't need that checked change listener, you can get the value on nextBtn click. Of course, you may still want the checked change listener to see if one was selected or display the toast... but that is besides the point.

Part 2, how to convert that radio button to a numeric value. Really, there are many ways to do this. I think my preferred way would be an enum, but you could do any number of methods for string to int mapping, radio button id to int mapping, etc. You could even use tags. Looks like you have some commented code in there around tags. I would just set the tags in the xml, then use checkedButton.getTag() to get the value when you are ready to move on to the next page. Simple enough.

I would definitely recommend passing the results on to the next Activity through intent extras. Each new Test page can add the result from that page to the previous page and pass the new value on, then when you get to the result page, the total result score is already calculated. It scales nicely if the user starts clicking the back button to change answers.

Upvotes: 1

Related Questions