Question

I am working on building a user feedback form. The form consists of a number of RadioGroups. Inside each RadioGroup there is 5 RadioButtons.

Currently this is the way I check the values of each group. However it seems very long winded and awkward to do it this way, can someone suggest a more efficient method of doing so that might tidy up the code more?

Here is my XML File:

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

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

        <!-- Activity Title -->


        <TextView
            android:id="@+id/feedbackTitle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:text="@string/feedbackTitletxt"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <!-- Description box -->


        <TextView
            android:id="@+id/feedbackInfo"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:text="@string/feedbackInfotxt" />

        <!-- Name -->

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

            <TextView
                android:id="@+id/nameLabel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/nameLabeltxt" />


            <EditText
                android:id="@+id/nameEntryBox"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="10dp"
                android:ems="10"
                android:inputType="textPersonName" />

        </LinearLayout>

        <!-- County -->

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

            <TextView
                android:id="@+id/countyLabel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/countyLabeltxt" />


            <Spinner
                android:id="@+id/countySpinner"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginBottom="10dp"
                android:layout_weight="1" />

        </LinearLayout>

        <!-- Date of Birth -->

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


            <TextView
                android:id="@+id/dobLabel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="5dp"
                android:text="@string/dobLabeltxt" />


            <DatePicker
                android:id="@+id/datePicker"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="10dp" />

        </LinearLayout>

        <!-- Atmosphere -->


        <TextView
            android:id="@+id/atmosphereLabel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:text="@string/atmosphereLabeltxt" />


        <RadioGroup
            android:id="@+id/atmospheregroup"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginBottom="10dp"
            android:orientation="horizontal" >

            <RadioButton
                android:id="@+id/arad1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:layout_weight="1"
                android:text="@string/radVal1" />

            <RadioButton
                android:id="@+id/arad2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginLeft="15dp"
                android:layout_weight="1"
                android:text="@string/radVal2" />

            <RadioButton
                android:id="@+id/arad3"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="15dp"
                android:layout_weight="1"
                android:text="@string/radVal3" />

            <RadioButton
                android:id="@+id/arad4"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="15dp"
                android:layout_weight="1"
                android:text="@string/radVal4" />

            <RadioButton
                android:id="@+id/arad5"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_marginLeft="15dp"
                android:layout_weight="1"
                android:text="@string/radVal5" />
        </RadioGroup>

        <!-- Service -->


        <TextView
            android:id="@+id/serviceLabel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:text="@string/serviceLabeltxt" />



            <RadioGroup
                android:id="@+id/sevicegroup"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginBottom="10dp"
                android:orientation="horizontal" >


                <RadioButton
                    android:id="@+id/srad1"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="5dp"
                    android:layout_weight="1"
                    android:text="@string/radVal1" />

                <RadioButton
                    android:id="@+id/srad2"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="15dp"
                    android:layout_weight="1"
                    android:text="@string/radVal2" />

                <RadioButton
                    android:id="@+id/srad3"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="15dp"
                    android:layout_weight="1"
                    android:text="@string/radVal3" />

                <RadioButton
                    android:id="@+id/srad4"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="15dp"
                    android:layout_weight="1"
                    android:text="@string/radVal4" />

                <RadioButton
                    android:id="@+id/srad5"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="15dp"
                    android:layout_weight="1"
                    android:text="@string/radVal5" />
            </RadioGroup>

        <!-- Rating Bar -->


        <TextView
            android:id="@+id/overallLabel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:text="@string/overallLabeltxt" />


        <RatingBar
            android:id="@+id/ratingBar1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginBottom="10dp" />

        <!-- Submit Button -->

        <Button
            android:id="@+id/submitFormBtn"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/submitFormBtntxt" />
    </LinearLayout>

</ScrollView>

here is my Activity:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.feedback_scroll);

    // References to XML - Reference required to every component on the
    // form.
    name = (EditText) findViewById(R.id.nameEntryBox);
    county = (Spinner) findViewById(R.id.countySpinner);
    // Set array adapter
    county.setAdapter(fillCountySpinner());
    date = (DatePicker) findViewById(R.id.datePicker);
    atmosGroup = (RadioGroup) findViewById(R.id.atmospheregroup);
    atmo1 = (RadioButton) findViewById(R.id.arad1);
    atmo2 = (RadioButton) findViewById(R.id.arad2);
    atmo3 = (RadioButton) findViewById(R.id.arad3);
    atmo4 = (RadioButton) findViewById(R.id.arad4);
    atmo5 = (RadioButton) findViewById(R.id.arad5);
    servGroup = (RadioGroup) findViewById(R.id.sevicegroup);
    ser1 = (RadioButton) findViewById(R.id.srad1);
    ser2 = (RadioButton) findViewById(R.id.srad2);
    ser3 = (RadioButton) findViewById(R.id.srad3);
    ser4 = (RadioButton) findViewById(R.id.srad4);
    ser5 = (RadioButton) findViewById(R.id.srad5);
    rating = (RatingBar) findViewById(R.id.ratingBar1);
    submitBtn = (Button) findViewById(R.id.submitFormBtn);

    // Add a listener to the submit button - Anonymous inner class
    submitBtn.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // Create a new Intent.
            Intent i = new Intent(v.getContext(), FeedbackResults.class);

            // Add extra parameters using putExtra() method. extras are
            // essentially a Bundle of additional information we want to
            // pass to a new activity.
            // It is similar to a map in the sense that is uses key value
            // pairs.

            // Name - Get name from EditText box and convert to string.
            i.putExtra("name", name.getText().toString());
            // County - getSelectedItem() will return data of currently
            // selected item. Convert this to a String.
            i.putExtra("county", county.getSelectedItem().toString());

            // DOB - dd/mm/yyyy format.
            i.putExtra("day", date.getDayOfMonth());
            i.putExtra("month", date.getMonth());
            i.putExtra("year", date.getYear());

            // Atmosphere - assign the checked buttons id to id variable and
            // pass to method which will assign "atmos" a value based on
            // radio button selected.
            id = atmosGroup.getCheckedRadioButtonId();
            getAtmosphere(id);
            i.putExtra("atmosphere", atmos);

            // Service - assign the checked buttons id to id variable and
            // pass to method which will assign "serv" a value based on
            // radio button selected.
            id = servGroup.getCheckedRadioButtonId();
            getService(id);
            i.putExtra("service", serv);

            // Rating - As the rating is a Float and we cannot display a
            // Float in a TextView we must convert to a string.
            i.putExtra("rating", Float.toString(rating.getRating()));

            // Start new Activity that will display a review of the
            // customers feedback. Pass the intent to the method. This
            // intent contains all of the data in it's extras array.
            startActivity(i);
            finish();

            // Toast message - Create a Toast Object to inform user that
            // feedback was submitted.
            Toast.makeText(v.getContext(), "Thank you for your feedback!",
                    Toast.LENGTH_LONG).show();

        }
    });
}

In this call I have this method written out for each different group.

// Called when the user presses the submit button. The id of the radioButton
// that was checked is passed to the method.
public void getAtmosphere(int id) {

    // Switch statement will check if the ID matches any of the cases.
    switch (id) {
    case R.id.arad1:
        atmos = "1";
        break;
    case R.id.arad2:
        atmos = "2";
        break;
    case R.id.arad3:
        atmos = "3";
        break;
    case R.id.arad4:
        atmos = "4";
        break;
    case R.id.arad5:
        atmos = "5";
        break;
    }
}

The above is working fine, and no errors however I just want to know how I can improve it! Many thanks.

Was it helpful?

Solution

However it seems very long winded and awkward to do it this way

RadioGroups are just long winded... You could write less code with:

TextView checked = (TextView) findViewById(amosgroup.getCheckedRadioButtonId());
atmos = checked.getText().toString();

(getCheckedRadioButtonId() is long winded by itself!) But this assumes that the text for each RadioButton is "1", "2", etc and findViewById() is slower than a switch statement...


Or you could use an OnCheckedChangedListener so you don't have to call getCheckedRadioButtonId(). But this is called every time the user clicks a new RadioButton, if you only need the "final answer", it is a few milliseconds slower.

OTHER TIPS

Well since your ids and the resultant Strings you make are logical ("1" for arad1,etc), you can use this answer to get the id as a String. Of course, if your Buttons are already using the number as the text, it's easier to use that instead.

So something like this might work:

String s =  getResources().getResourceEntryName(id);
atmos = s.substring (s.length()-1);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top