Native Mobile App Development With Java

Android Spinner

15-Sep-2024

How to implement a Spinner in Android Studio project to let users select from a dropdown list

Create a simple example of using a Spinnerin Android. In this example, we'll create an Android app with a Spinner, and the user can set the select data using the Spinner. We'll also display a toast message with the selected Spinner.



Here is the tutorial for Kotlin




There Are Some Common Attributes for Spinner:


id
This ID serves as the control's unique identity.
layout_widthmatch_parent -> Text area contains fullscreen
wrap_content-> Text area contains only text size
layout_widthmatch_parent -> Text area contains fullscreen
wrap_content-> Text area contains only text size





Step 1:


File open res/layout/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"
tools:context=".MainActivity">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<Spinner
android:id="@+id/spinner"
android:layout_width="200dp"
android:layout_height="wrap_content" />
</LinearLayout>


</RelativeLayout>


Step 2:

File open com.microappvalley.newtestproject/MainActivity.java file −

Java:


package com.microappvalley.newtestproject;

import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
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 {



ArrayList<String> spinnerArray;
Spinner spinner;
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

spinner=findViewById(R.id.spinner);
spinnerArray=new ArrayList<>();
spinnerArray.add("Java");
spinnerArray.add("Kotlin");
spinnerArray.add("JetPack");
spinnerArray.add("Flutter");

ArrayAdapter<String> spinnerAdapter= new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, spinnerArray);

spinner.setAdapter(spinnerAdapter);

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
Toast.makeText(MainActivity.this, spinnerArray.get(position), Toast.LENGTH_SHORT).show();
}

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

}

});


}
}







Open Device Manager, run the emulator, and  then run the application. Next, check the working output and check the output  you declared in your code.


Output:







Comments