Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spinner on value Change

I have two spinner in my system. Now I have to change the selected value of the 2nd spinner depending on the first spinner value. As soon as the user will change the 1st spinner value, the 2nd spinner value will set automatically depending upon the 1st spinner's selected value. How to implement this?

like image 770
dev_android Avatar asked Mar 07 '11 12:03

dev_android


People also ask

What is a spinner in development?

Spinners provide a quick way to select one value from a set. In the default state, a spinner shows its currently selected value. Touching the spinner displays a dropdown menu with all other available values, from which the user can select a new one. You can add a spinner to your layout with the Spinner object.

How do I set the spinner value in Kotlin?

This example demonstrates how to get Spinner value in Kotlin. Step 1 − Create a new project in Android Studio, go to File ⇉ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

What is spinner explain with suitable example?

Android Spinner is a view similar to the dropdown list which is used to select one option from the list of options. It provides an easy way to select one item from the list of items and it shows a dropdown list of all values when we click on it.


2 Answers

From the Hello Spinner tutorial:

Now create a nested class that implements AdapterView.OnItemSelectedListener. This will provide a callback method that will notify your application when an item has been selected from the Spinner. Here's what this class should look like:

public class MyOnItemSelectedListener implements OnItemSelectedListener {

    public void onItemSelected(AdapterView<?> parent,
        View view, int pos, long id) {
      Toast.makeText(parent.getContext(), "The planet is " +
          parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
    }

    public void onNothingSelected(AdapterView parent) {
      // Do nothing.
    }
}

The AdapterView.OnItemSelectedListener requires the onItemSelected() and onNothingSelected() callback methods. The former is called when an item from the AdapterView is selected, in which case, a short Toast message displays the selected text; and the latter is called when a selection disappears from the AdapterView, which doesn't happen in this case, so it's ignored. Now the MyOnItemSelectedListener needs to be applied to the Spinner. Go back to the onCreate() method and add the following line to the end: spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());

In other words, you need to create an OnItemSelectedListener that modifies the value of the second spinner, and attach it to the first spinner.

like image 76
I82Much Avatar answered Oct 16 '22 11:10

I82Much


You have to put the condition on onItemSelected of very first spinner. By this example you can get value of 2nd spinner depending on 1st spinner:

        public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,long arg3) {

        if(arg0.equals(spin0)){
        spin1.setClickable(true);
                    if(spin0.getSelectedItem().equals("India"))
                    {
                        ArrayAdapter <String> s1 = new ArrayAdapter <String> (this,android.R.layout.simple_spinner_item,states_india);
                        s1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                        spin1.setAdapter(s1);
                    }
                    else  if(spin0.getSelectedItem().equals("Pakistan"))
                    {
                        ArrayAdapter <String> s2 = new ArrayAdapter <String> (this,android.R.layout.simple_spinner_item,states_pak);
                        s2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                        spin1.setAdapter(s2);
                    }
                    else  if(spin0.getSelectedItem().equals("China"))
                    {
                        ArrayAdapter <String> s3 = new ArrayAdapter <String> (this,android.R.layout.simple_spinner_item,states_china);
                        s3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                        spin1.setAdapter(s3);
                    }

        }

    }    
like image 32
Parth Sharma Avatar answered Oct 16 '22 09:10

Parth Sharma