Simple Dropdown (Spinner)

Now let’s see about Dropdown (Known as Spinner) in android.

In this project, we are going to show a list of android versions in a Spinner.

for that create a new project named Simple Dropdown(you can change the name as your wish :p ).

Now enter the following data in your activity_main.xml file.

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    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.a4akhilsudha.simpledropdown.MainActivity">

    <Spinner
        android:id="@+id/spinner"
        android:layout_width="0dp"
        android:layout_height="50dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintVertical_bias="0.112" />

</android.support.constraint.ConstraintLayout>

Now create a string array in your string.xml file. In here we are creating a list of android versions.

<resources>
    <string name="app_name">Simple Dropdown</string>
    <string-array name="android_versions">
        <item>Apple pie</item>
        <item>Banana bread</item>
        <item>Cupcake</item>
        <item>Donut</item>
        <item>Eclair</item>
        <item>Froyo</item>
        <item>Gingerbread</item>
        <item>Honeycomb</item>
        <item>Ice cream sandwich</item>
        <item>Jellybean</item>
        <item>Kitkat</item>
        <item>Lollipop</item>
        <item>Marshmallow</item>
        <item>Nougat</item>
        <item>Oreo</item>
    </string-array>
</resources>

Now open your MainActivity.java file and enter the following data.

package com.a4akhilsudha.simpledropdown;

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

        /*Find the id of spinner*/
        final Spinner spinner = (Spinner) findViewById(R.id.spinner);

        /*set an adapter with strings array*/
        spinner.setAdapter(new ArrayAdapter<>(this,R.layout.support_simple_spinner_dropdown_item,getResources().getStringArray(R.array.android_versions)));

        /*set click listener*/
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                Toast.makeText(MainActivity.this, "You have Selected "+ spinner.getSelectedItem().toString(), Toast.LENGTH_SHORT).show();
            }

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

                /*Do something if nothing selected*/


            }
        });
    }
}

Now run (Shift + f10) the program on a real device or in a  virtual device.

Congrats …. 🙂 You just created an android app which displays a drop down menu having some android versions.

Output :

Leave a Reply

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