Spinner in Android

Spinner In Android

In this tutorial we will learn to implement Spinner with a simple example in Android Studio. Spinner is a drop down list which allows the user to pick one item from it. In this example we will create a simple spinner which shows a drop down list of android OS and when we select one item then the image of the similar OS will be shown below.

Example:
  • Create a new project in Android Studio and name it as Spinner.
  • Design a spinner and image_view in a main layout, as follows.
File: activity_main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.hss_24.spinner.MainActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:padding="10dp"
        android:text="AndroidLovers"
        android:textSize="28sp"
        android:textStyle="bold" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Please select your Android OS"
        android:textSize="20sp" />

    <Spinner
        android:id="@+id/spinner"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_marginTop="10dp">

    </Spinner>

    <ImageView
        android:id="@+id/image_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp" />

</LinearLayout>
  • Create a integer array in strings.xml, as follows.
File: strings.xml:

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

    <integer-array name="image_list">
        <item>@drawable/cupcake</item>
        <item>@drawable/donut</item>
        <item>@drawable/eclair</item>
        <item>@drawable/froyo</item>
        <item>@drawable/gingerbread</item>
        <item>@drawable/honeycomb</item>
        <item>@drawable/icecream_sandwich</item>
        <item>@drawable/jellybean</item>
        <item>@drawable/kitkat</item>
        <item>@drawable/lollipop</item>
        <item>@drawable/marsh_mallow</item>
    </integer-array>

</resources>


  • Now its time to code, do the following in java file.
File: MainActivity.java:
import android.content.res.TypedArray;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.Spinner;

public class MainActivity extends AppCompatActivity {

    private ImageView imageView;
    private Spinner spinner;
    private TypedArray imgs;

    //    Spinner dropdown items
    String[] android_names = {"Cupcake", "Donut", "Eclair", "Froyo", "Ginger Bread", "Honeycomb",
            "IceCream Sandwich", "Jelly Bean", "Kitkat", "Lollipop", "MarshMallow"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        spinner = (Spinner) findViewById(R.id.spinner);
        imageView = (ImageView) findViewById(R.id.image_view);

        // get image list from array defined in strings.xml
        imgs = getResources().obtainTypedArray(R.array.image_list);

        // create and set adapter for spinner
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, android_names);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);


        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                // display images when spinner item is selected
                imageView.setImageResource(imgs.getResourceId(spinner.getSelectedItemPosition(), -1));
            }

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

            }
        });
    }
}

  • Run the Application.
Output:





Download project here
Thank You!!!
Please like and share...


Comments