A Spinner in Android is a dropdown menu that allows users to select one item from a list. Here, we'll create a simple Android app using Java that includes a Spinner and displays the selected item.
Step-by-Step Guide
1. Set Up Your Project
Open Android Studio and create a new project. Name it SpinnerExample and select Empty Activity.
2. Update the Layout File
Open activity_main.xml
and add the Spinner element:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Spinner
android:id="@+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"/>
<TextView
android:id="@+id/selected_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/spinner"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:textSize="18sp"/>
</RelativeLayout>
3. Update the MainActivity
Open MainActivity.java
and update it to initialize the Spinner:
package com.example.spinnerexample;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private Spinner spinner;
private TextView selectedItem;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner = findViewById(R.id.spinner);
selectedItem = findViewById(R.id.selected_item);
// Array of items for the spinner
String[] items = {"Apple", "Banana", "Cherry", "Date", "Elderberry"};
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, items);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner.setAdapter(adapter);
// Set the spinner item selection listener
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// Display the selected item in the TextView
String item = parent.getItemAtPosition(position).toString();
selectedItem.setText("Selected: " + item);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// Another interface callback
}
});
}
}
Explanation
1. Layout File (activity_main.xml
)
In the layout file, we added a Spinner
and a TextView
. The Spinner allows users to select an item, and the TextView displays the selected item.
2. MainActivity
In the MainActivity, we perform the following steps:
- Initialize the Spinner and TextView using
findViewById()
. - Create an array of items to display in the Spinner.
- Create an
ArrayAdapter
to hold the items and set it to the Spinner. - Set an
OnItemSelectedListener
on the Spinner to handle item selections and display the selected item in the TextView.
Conclusion
By following these steps, you can easily create a Spinner in an Android app using Java. This example covers the basics of setting up a Spinner and handling item selections.