Android Spinner

sapan
Android provide variety of widgets on of them is spinner. Spinner is similar to that of the drop-down list in web it allows user to select a single item from list of items.



There are two type of spinner dialog and dropdown. There is a problem which I faced while using inline list box i.e inline list is not scroll-able. If items count goes beyond the screen size than you can't select the item

To add spinner to your layout you need to add <Spinner> element to your layout.

Note:
By default spinner mode is set to dropdown.
To set Spinner to dialog set android:spinnerMode="dialog"


<Spinner
    android:id="@+id/spinFrom"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />

To add elements to your spinner you need the list of items for this. If you are not dynamically populating your spinner you can use xml value to fill it. [ strings.xml located inside value folder of android project ]


<resources>
    <string name="app_name">DroidFeatures</string>

    <string-array name="currency">
        <item>USD</item>
        <item>AUD</item>
        <item>BGN</item>        
        <item>DKK</item>
        <item>GBP</item>
        <item>HKD</item>
      
    </string-array>

</resources>

Now to initialize the spinner the data. Find the view , initialize the items array , add OnItemSelectedListener.

To initialize the spinner specify the resource array , and default layout.


// Create an ArrayAdapter using the string array and a default spinner layout
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
                R.array.currency,android.R.layout.simple_spinner_item);
        // Specify the layout to use when the list of choices appears
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        // Apply the adapter to the spinner
        spinFrom.setAdapter(adapter);


Now to respond to spinner item select OnItemSelectedListener need to be associated with it.


//setting up the spinner listners
       spinFrom.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
           @Override
           public void onItemSelected(AdapterView<?> adapterView, View view, int pos, long id) {
               Toast.makeText(con, adapterView.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();

           }

           @Override
           public void onNothingSelected(AdapterView<?> adapterView) {

           }
       });

Code : 


public class ConverterActivity extends AppCompatActivity {

   
    Context con;
    Spinner spinFrom ;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        //set the context
        con = this;   

        //Find the spinner
        spinFrom   = (Spinner)findViewById(R.id.currFrom);

        // Create an ArrayAdapter using the string array and a default spinner layout
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
                R.array.currency,android.R.layout.simple_spinner_item);
        // Specify the layout to use when the list of choices appears
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        // Apply the adapter to the spinner
        spinFrom.setAdapter(adapter);
      


        //setting up the spinner listners
       spinFrom.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
           @Override
           public void onItemSelected(AdapterView<?> adapterView, View view, int pos, long id) {
               Toast.makeText(con, adapterView.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();

           }

           @Override
           public void onNothingSelected(AdapterView<?> adapterView) {

           }
       });

      
    }


    
}

Post a Comment

0Comments
Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !