Android Simple Spinner Example

Posted by

Hello all and welcome to easytechsolutions A place for all your queries ,So in today’s post we are going to implement A Simple Spinner in Android .

Spinner is act as a dropdown list of selections of any data , you just need to select that data and you are good to go as it is widely used in Online Forms , Online Banking etc.

Lets get started implementing this cool android widget:-

Steps you need to do are :

Step 1:- Open up your Android Studio and make a new project and select Blank Activity to start with

Step 2:- Open up activity_main.xml which is under res/layout file and paste below code in it :-

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    tools:context="com.spinnerexampleapp.MainActivity">

    <Spinner
        android:id="@+id/spinner"
        android:layout_width="200dp"
        android:layout_height="50dp"
        android:layout_centerInParent="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="80dp"
        android:background="@android:drawable/btn_dropdown"
        android:prompt="@string/spinner_title"
        />

</RelativeLayout>

In above step we added Spinner in our layout file now next step we are going to make array and store some values in it and make adapter instance and set to Spinner so that every time you click on Spinner you see number of data.

Step 3:- Open MainActivity.java file and paste below code in it :-

MainActivity.java

package com.spinnerexampleapp;

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 implements AdapterView.OnItemSelectedListener {

    String[] country = {"India","USA","England","Autralia","New Zealand","South Africa","Pakistan","China","Japan"};

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

        Spinner spin = (Spinner) findViewById(R.id.spinner);
        spin.setOnItemSelectedListener(this);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item,country);
        spin.setAdapter(adapter);
    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        
        //Toast a message when we select any country from Spinner dropdown:-
        Toast.makeText(this,country[position], Toast.LENGTH_SHORT).show();

    }

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

    }
}

We are done with coding now just save all and run and you will get to see a nice and cool Spinner implementation in your Android App.

Hope you grab useful information about Spinner in Android and how you can implement it !

Our Facebook Page URL :-https://www.facebook.com/easytechsolutions.info/

Leave a Reply

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