Spinner in Android

In this tutorial we will learn how to create a Spinner in Android Studio. Spinner displays dropdown content in mobile screens. Generally Spinner provide a quick way to select one value from set. By default it shows the current selected value.

FlutterTPoint
FlutterTPoint

Spinner In Android Studio With Example

Spinner in android is used to to show the data in dropdown view. The list of strings or the icons assigned to the spinner adapter. The default Spinner adapter shows the list of the data in a dropdown way. When we touch the spinner the dropdown list occurs and shows the list of data. User can select one of them. The selected value set to the display on the screen and the dropdown get disappears.

Enable DataBinding

Please enable the Databinding. Databinding is the way of reducing the length of code. You do not need to don findViewById if you enabled databinding in android. You can simply enable databinding from here.

Create Spinner in Layout File

   <Spinner
            android:id="@+id/spinner"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:layout_gravity="center"/>

The above spinner widget creates Spinner in the layout file. You can use this.

activity_test.xml

Below is the Spinner layout activity.

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".Activity.TestActivity"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="30dp"
            android:text="Spinner Demo"
            android:textSize="20dp"
            android:textColor="@color/black"/>

        <Spinner
            android:id="@+id/spinner"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:layout_gravity="center"/>
    </LinearLayout>
</layout>

TestActivity.java

package com.example.androidtestingproject.Activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Toast;

import com.example.androidtestingproject.R;
import com.example.androidtestingproject.databinding.ActivityTestBinding;

public class TestActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
    private ActivityTestBinding binding;
    private String[] languages = {"c", "c++", "Java", "Kotlin", "Python"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_test);

        initView();
    }

    private void initView() {
        binding.spinner.setOnItemSelectedListener(this);
        ArrayAdapter arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, languages);
        arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        binding.spinner.setAdapter(arrayAdapter);
    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        Toast.makeText(getApplicationContext(), languages[position], Toast.LENGTH_SHORT).show();
    }

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

    }

}

In the above java class we created an array of languages which is String type. The ArrayAdapter is the default adapter which is used to create the adapter. It takes three parameters, this context, simple_spinner_item and the array.

        ArrayAdapter arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, languages);
        arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        binding.spinner.setAdapter(arrayAdapter);

Spinner Listener Implementation In Android

Implement The AdapterView.OnItemSelectedLister listener to your main class and override the methods.

To deduct that on which item you clicked, The onItemSelect method displays that. You can override this by implementing the class by AdapterView.OnItemSelectedListener.

 @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        Toast.makeText(getApplicationContext(), languages[position], Toast.LENGTH_SHORT).show();
    }

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

    }

Android Spinner Result

Spinner In android
Android Spinner

Thank you for visiting this tutorial on FlutterTPoint, Hope you got Spinner learning from here. For any query you can comment in the comment section below. Please visit more useful Android Tutorials.

close

Don’t miss new tips!

We don’t spam! Read our [link]privacy policy[/link] for more info.

Leave a Comment