Skip to main content

Android Spinner

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) {

           }
       });

      
    }


    
}

Comments

Popular posts from this blog

Xamarin : Simple Calculator Tutorial Visual Studio C#

Simple Calculator in Android (Xamarin) using VisualStudio C#In Brief: This article will help you on building a simple calculator in android Xamarin with simple steps using C# Language in Visual Studio. Detail:
Building this simple calculator will let you learn how to work with the Android Buttons, Text View and simple functions. The UI is not so beautiful but is a great way to learn the basic functionalities.

Create Custom View in android MATRIX EFFECT CANVAS tutorial

Create Custom View in android MATRIX EFFECT CANVAS tutorial
Hi all as I stared my development with android I always wanted to create a Custom view or rather I want a canvas where I can draw freely. To learn about canvas I decided to create matrix rain effect which I can add to my android layout. Here is the complete tutorial which I have post. Hopping it will be use full to someone.

You can find the link to android project at the bottom of the post

Android Transparent status bar

Recently I wanted to get a transparent status bar so that my app background blends smoothly with the status bar . Below method worked