Skip to content Skip to sidebar Skip to footer

Listener On A Spinner

This code adds strings to a Spinner from a list. How do I implement a listener so that I can get the string that is clicked or tapped from the Spinner? spinner = (Spinner) findVi

Solution 1:

You can use this code

 spinner.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
    }
    public void onNothingSelected(AdapterView<?> parent) 
    {

    }           
});

Solution 2:

Anyone looking For kotlin you can do like this:

spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener{
        override fun onNothingSelected(p0: AdapterView<*>?) {
        }

        override fun onItemSelected(p0: AdapterView<*>?, p1: View?, p2: Int, p3: Long){
       }
    }

Post a Comment for "Listener On A Spinner"