How to get selected spinner value in android, android spinner, spinner in android, android spinner get selected item value, android get selected value from spinner

How to get selected spinner value in android

A spinner in android is a widget that is used to hold and display items in a drop-down nature. Like in website development, a spinner serves the same role as a select option.

Unlike other widgets in android, where the value is requested from the user to input, for example, edit text, in a spinner, the values are predefined depending on what has been provided mostly from an array and therefore, application user will only need to select from the drop-down.

As we discussed in a previous article, we highlighted how to create a spinner in android and how to load data into a spinner using kotlin language in android

For this article, we shall majorly focus on how to get selected spinner value in android using java language. Note that the steps discussed here can also be used in kotlin language by converting java files to kotlin files using the steps described here.  

How to create a spinner in android

First, we highlight the steps followed to create a spinner in android.

  • In XML, define the spinner widget as follows and ensure you assign a unique id.

<Spinner

                            android:id="@+id/computermodels"

                            android:gravity="center_vertical|start"

                            android:paddingHorizontal="20sp"

                            android:layout_width="match_parent"

                            android:layout_height="50dp"/>

  • In the java file (.java), define the spinner at the top or global level as follows

private Spinner spinner;

  • Initialize the spinner defined above at the onCreate method as shown below

spinner=findViewById(R.id.carmodels);

  • Next, define the array list of values that will be displayed in the spinner as option values, in this case we are using car brands as follows.

                       String[] arraycarbrands = new String[] {

                                      "Toyota", "Nissan","VW","Audi","Mazda","Honda","Honda"

        };

  • Next, create an array adapter that will hold the layout for the spinner. Also, match the spinner created in the XML with the adapter as shown below

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,  android.R.layout.simple_spinner_item,  arraycarbrands);

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

        spinner.setAdapter(adapter);

When you run the application with the steps defined so far, the spinner will display the various car brands defined in the array above.

How to get selected spinner value in android

Having displayed the array data into the spinner, you will now need to learn the way in which you can access the value that the user selects from the drop-down list.

  • First, the id of the spinner defined in the XML file will be the one used while getting the value selected from the spinner.
  • In java file, we use the getSelectedItem() method and then convert the value to string as shown below

String selectedcarmodel = ((Spinner)findViewById(R.id.carmodels)).getSelectedItem().toString();

  • To check whether the value selected from the list is captured, we use a Toast to display the value selected

Toast.makeText(this," Selected car brand is "+selectedcarmodel ,Toast.LENGTH_LONG).show();

At this stage, the value selected from the spinner is available for submission or any other function.

How to add a listener to spinner

To add a listener to a spinner, we use the setOnItemSelectedListener() method and then follow with a public void method called on item selected().

At this step, you can check whether the item selected equals to a certain value.

Also, remember to add the onNothingSelected() method that runs when nothing is selected from the spinner list.

An example of setOnItemSelectedListener() and onItemSelected() methods is as follows,

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            @Override

            public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {

                if(spinner.getSelectedItem().toString().equals("Toyota")){

             // add activity when one item is selected

               }

             @Override

            public void onNothingSelected(AdapterView<?> adapterView) {

          // add action when nothing is selected

            }

 });

 

Conclusion

In the above discussion, we have highlighted the steps on how to create a spinner in android from defining it in the XML file to implementing the functions in the java file whereby we have defined an array that holds the items that are displayed in the spinner.

Having highlighted the steps above of how to get the selected spinner item value in android, it’s my hope that you can now implement this function with ease. Thanks for following.