String selectedClass = parent.getItemAtPosition(position).toString()
So add the following code inside classSpinner's onItemSelected() method.ĬtOnItemSelectedListener(new void onItemSelected(AdapterView parent, View view, int position, long id) options to divSpinner from the string-array resource. Inside the onItemSelected() method of classSpinner, you have to get the selected item from the dropdown list and based on that value, you have to assign entries i.e. Spinner classSpinner, void onCreate(Bundle savedInstanceState) Changing Value of second Spinner based on first Spinner value MainActivity.java public class MainActivity extends AppCompatActivity We will start by creating instances of both the spinners and then, assign the setOnItemSelectedListener() on both the spinners. To handle the GUI events, we need to implement the code inside the MainActivity.java file. We will do this through Java code as the user will select from the first dropdown at run time i.e. Doing so will assign the values present in the items_class array to the classSpinner.Īs per the user's selection of the option from the first spinner, our second Spinner will appear along with options based on the first spinner's selection. To add these entries to the spinner all we have to do is add a property in the main activity layout XML file. In our dataset, string-array named items_class will be assigned to the classSpinner to display the class items in the dropdown list. We have also defined our dataset for the Spinners.
We have defined the layout XML for the User interface.
You have already studied about what Spinners are and how they work.