Spinner & RadioGroup – Android Example

Hey Guys, we welcome you for our newest tutorial showing details about how you will be able to use Spinner & RadioGroup  in the Android Programming.

In this tutorial, user have to pass his/her name, choice of subject from the Radio Buttons and based on the selection of the subject, list of books of user selected subject is provided as a choice to the user. On the basis of user choices, as an objective of this program we have displayed User selected items as final selection.

Lets start with the program,

First of all we have to build and GUI for User Inputs. We have arranged EditText (for taking Name of the User), RadioGroup (for the Subject Selection (3 subjects – multiple selection not allowed)) and below that one Spinners (contain list of books for each subject (3 spinners for each subject book list)).  Name, display text and Id for all the widgets are also specified to take them as an reference in MainActivity.java (discussed after this). The below code snipped displays our activity_main.xml layout file.

activity_main.xml


As you can see, we have kept Spinner’s Visibility to ‘GONE’. That’s because list of book selection only appear only when User will select one of the subject.

187_GUI

You can see in the above screen capture, how the user Layout look like. So for now we have created the GUI part, now we move on to the main java coding part.

As an objective of this practical, we have to take User Name, choice of subject from the User, then after, on the basis of the subject choice we have to display list (sinner) of books of particular selected subject to the user from which user will select appropriate book title. As an final process, we have to display this all selection.

In our main java file, first objects of all the view widgets are declared and created with the help of findViewById() method. To acquire, the user selected subject from the radioGroup, we have declared an setOnCheckedChangeListener() where, user selection of subject is fetched in getSelectedSubject(). The method return subject choice of user with the help of getCheckedRadioButtonId()indexOfChild() and getChildAt() methods. On the basis of user choice list of (spinners of) books are get visible to the user by setting its visibility again to ‘VISIBLE’.

Finally, to get all these selections we have implemented getText() that will fetch the User Specified name and the choice of the user selected books from the spinner by getSelectedItem(). These all information is finally diplayed to the user with the help of Toast() method. The code snippet is as displayed below.

MainActivity.java

Specified string names, array of strings  in String.xml file are as displayed in below code snippet.

string.xml

<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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="@string/app_main_label"
        android:id="@+id/textView"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="@dimen/in_between_layout_space"
        android:id="@+id/linearLayout">

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/btn_label_Add_Student"
            android:id="@+id/btnAddStudent" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/btn_label_Show_Details"
            android:id="@+id/btnShowDetails" />

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

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:id="@+id/tvStdList"
        android:layout_below="@+id/linearLayout"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="@dimen/in_between_layout_space"
        android:gravity="center_horizontal" />
</RelativeLayout>

Output of this practical will be look a like as,

187_Final_Output

Hope you have understood this practical contain use of Spinner and RadioGroup very well. If question persist, you can always comment back to us. We will make sure to resolve queries.

“Bless for You, Peace for the World” – Team Tech Tutor

Leave a Reply

Your email address will not be published. Required fields are marked *