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.
Step 2 − Add the following code to res/layout/activity_main.xml. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.
This example demonstrates how to change the Text color of Menu item in Android using Kotlin.