Populate Second Spinner depending on first Spinner in Android (Java)

Adding dependent spinner is indeed very easy. It can be done with configuration and minimum customization.

For example you have following requirements:

Our first spinner populates two value :

  • Traumatic Event

  • Traumatic Behavior

If Traumatic Event is selected the second spinner will select the following values:

  • Sexual Abuse

  • Other Adolescent trauma

  • Bullying

  • Cyber Bullying

  • Peer Violence

  • Vandalism


If Traumatic behavior is elected the second spinner will select following values

  • Suddenly became hyperactive.

  • Suddenly became very reserved.

  • Trouble forming relationships with teachers.

  • Poor self-regulation

  • Emotional Numbness

  • Sudden deterioration in academic performance

  • Loss of interest in school, friends, hobbies

  • Negative Thinking




My Solution:


string.xml file

Added following lines in within <resources></resources> tags of res/values/strings.xml file.

<string-array name="IssueTypesArray">

<item>Traumatic Event</item>

<item>Traumatic Behavior</item>

</string-array>

<string-array name="Spinner2">


</string-array>

<string-array name="TraumaticEvent">

<item>Sexual Abuse</item>

<item>Other Adolescent trauma</item>

<item>Bullying</item>

<item>Cyber Bullying</item>

<item>Peer Violence</item>

<item>Accident</item>

<item>Vandalism</item>

</string-array>

<string-array name="TraumaticBehavior">

<item>Suddenly became hyperactive</item>

<item>Suddenly became very reserved.</item>

<item>Trouble forming relationships with teachers.</item>

<item>Poor self-regulation</item>

<item>Emotional numbness</item>

<item>Sudden deterioration in academic performance</item>

<item>Loss of interest in school, friends, hobbies</item>

<item>Negative Thinking</item>

</string-array>



Layout File:

Added following lines in res/layout/activity_request.xml file

<TextView

android:id="@+id/question4"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:fontFamily="sans-serif-black"

android:text="Select Trauma Type"

android:textSize="12dp"

android:background="#f3f3f3"

android:textStyle="bold" />

<Spinner

android:id="@+id/IssueTypesArray"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_marginTop="10dp"

android:layout_below="@+id/question4"

android:layout_alignParentStart="true"/>

<TextView

android:id="@+id/question_5"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:fontFamily="sans-serif-black"

android:text="Select Sub Type"

android:textSize="12dp"

android:background="#f3f3f3"

android:textStyle="bold" />

<Spinner

android:id="@+id/spinner2"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_marginTop="10dp"

android:layout_below="@+id/question_5"

android:layout_alignParentStart="true"/>



Request Class(Java)


// first spinner

IssueTypesArray=(Spinner)findViewById(R.id.IssueTypesArray);

// populate values

ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.IssueTypesArray, android.R.layout.simple_spinner_item);

adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

IssueTypesArray.setAdapter(adapter);


// second spinner

IssueSubType = (Spinner)findViewById(R.id.spinner2);

final ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(this, R.array.TraumaticEvent, android.R.layout.simple_spinner_item);

adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

final ArrayAdapter<CharSequence> adapter3 = ArrayAdapter.createFromResource(this, R.array.TraumaticBehavior, android.R.layout.simple_spinner_item);

adapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);


//set the adapter depending on the value populated in the first spinner

IssueTypesArray.setOnItemSelectedListener(new OnItemSelectedListener() {

public void onItemSelected(AdapterView<?> parent, View view, int position, long id)

{

String selectedItem = parent.getItemAtPosition(position).toString();//this is your selected item

if(selectedItem.contentEquals("Traumatic Event"))

{

IssueSubType.setAdapter(adapter2);


}

if(selectedItem.contentEquals("Traumatic Behavior"))

{

IssueSubType.setAdapter(adapter3);

}

}

public void onNothingSelected(AdapterView<?> parent)

{


}

});











4 views
WHAT'S UP?

CURRENT PROJECT

 

Developing a platform to record any incidents related to the physical and mental well being of students in school and a workflow to address these with the right competent authority of the school. This would help the school to have an electronic record of the entire case management and would go a long way in improving the safety and security of the students.

 

LAST EVENT
1

Meeting With Mentors

 

Had a one to one meeting with mentors in incubation center.

21

ICDS School visit

 

Visited Integrated Child Development Services school in Kaikhali.

CONTACT

WEBEL-BCC&I Tech Incubation Centre

Webel Bhavan, Ground Floor,
Block EP & GP, Sector V, Salt Lake City, Kolkata 700091

Email: connect@ratqsciencentechs.com

  • YouTube
  • w-facebook
  • Twitter Clean

© 2019 by Ratq Science & Tech