Android Spiner

Spiner Example
Spiner is used to display multiple option and select only one. Android spinner is associated with AdapterView. So you need to use one of the adapter classes with spinner and one arraylist to add item to display in the spiner.
Spiner show currently selected item.

Example

Create activity_main.xml

file: First create activity_main.xml file

 

<?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"
    android:background="#efe9e9"
    tools:context=".MainActivity">

<LinearLayout
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_marginTop="50dp"
    android:layout_height="wrap_content">


    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:padding="10dp"
        android:textSize="18sp"
        android:textStyle="bold"
        android:layout_height="wrap_content"
        android:text="Spiner Example" />

    <Spinner
        android:id="@+id/spinner"
        android:layout_marginTop="20dp"
        android:padding="10dp"

        android:layout_width="match_parent"
        android:layout_height="50dp" />
</LinearLayout>
</RelativeLayout>

Create MainActivity.java to Handle Spiner

package com.programmingtip.spiner;

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;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
private Spinner spinnerCourse;
ArrayList courseNameArr=new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //UI
        spinnerCourse=findViewById(R.id.spinner);
        //add item in array list
        courseNameArr.add("Android");
        courseNameArr.add("Java");
        courseNameArr.add("PHP");
        courseNameArr.add("HTML");

        ArrayAdapter arrayAdapter = new ArrayAdapter(this,android.R.layout.simple_spinner_item,courseNameArr);
        arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        //Setting the ArrayAdapter data on the Spinner
        spinnerCourse.setAdapter(arrayAdapter);


        //on selected item event spiner

        spinnerCourse.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                Toast.makeText(getApplicationContext(),"Selected Course: "+courseNameArr.get(i),Toast.LENGTH_LONG).show();
            }

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

            }
        });
    }
}

Screen:

 

Leave a Reply

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