Using Spinners in Android Application

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

In this tutorial we are going to create a Spinner and will add a click listener to that.

Step:1 layout

We have added a spinner in layout.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <Spinner
        android:id="@+id/mSpinner"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:padding="16dp"
        style="@android:style/Widget.Holo.Light.Spinner"
        />

    <TextView

        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="www.androstuff.com"
        android:layout_gravity="center"
        android:textColor="@color/colorPrimaryDark"
        android:textSize="22sp"
        android:layout_marginTop="200dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</LinearLayout>

Step 2: Creating Entries for Spinnner

Navigate to values folder in res folder of your android app and than right click on it and select to create a new value resource xml. Add the entries as your need.

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="Avengers">
        <item> Tony</item>
        <item> Thor</item>
        <item> Hulk</item>
        <item> Captain</item>
        <item> THonous</item>

    </string-array>
</resources>

Step 3: MainActivity.java

package com.example.kaushal.spinner;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //create a spinner
        final Spinner mSpinner=findViewById(R.id.mSpinner);
        //create an ArrayAdapter using the string array and a default spinner layout
        ArrayAdapter<CharSequence>adapter=ArrayAdapter.createFromResource(this,
                R.array.Avengers,android.R.layout.simple_spinner_item);
        //specify the layout to use when list of choices appears
        adapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
        //Apply the adapter to the spinner
        mSpinner.setAdapter(adapter);
        //add click listener
        mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
              String x=mSpinner.getSelectedItem().toString();
                Toast t=Toast.makeText(MainActivity.this,x,Toast.LENGTH_SHORT);
                t.show();

            }

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

            }
        });
    }
}

In mainActivity we have added the onItemselectedlistener do when ever quser will click on any option a toast will pop up. You can set the task according to your need.

Now Run the app and feel the Magic. 😉

If you have any doubt related to Post kindly Ask in comment Section.

Download Complete Application:

Click below to download complete project from Github Repository

https://github.com/kaushalsood97/Spinner

 

 

 

About kaushal97

Android Enthusiast

Leave a Reply

Your email address will not be published. Required fields are marked *