Spinner in from java android application — Java Android

5Ssoftwaresafety
5S JAVA AND KOTLIN LEARNING
4 min readOct 12, 2020

This is how you can use an Android spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() inside Main Activity in Java Android Studio:

This example demonstrates the category of stadiums, you need to select a category from the category.

Spinner Example — Select Football Team

Android Spinner in JAVA ANDROID

Android Spinner is a view similar to the dropdown list which is used to select one option from the list of options. It provides an easy way to select one item from the list of items and it shows a dropdown list of all values when we click on it.

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.

Create Empty Project

How to implement on Android Spinner Dropdown List in android programmatically.

Step 2 — “Add the following code to res/layout/activity_main.xml.“

Android — Spinner
Android — Spinner

I worked in simple relative layout method for only give basic example.

  • In the below code, we have declared Spinner as ”@+id/spinner”.
  • You can add a spinner to your layout with the Spinner object. You should usually do so in your XML layout with a <Spinner> element. For example:

RELATIVE LAYOUT FILE CODE IN XML:

<?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="com.
yourname.yourpackagename.MainActivity"
android:padding="16dp"
android:id="@+id/relLayout"
android:orientation="vertical"
>
<Spinner
android:id="@+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="@id/textView1"
android:layout_marginStart="10dp"
/>

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:textStyle="bold"
android:text="Choose a football club ⚽:"
/>

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/spinner"
android:layout_marginTop="20dp"
android:text="Football Team 🏟️: "
android:textStyle="bold"
/>

<TextView
android:id="@+id/stadiumname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/textView2"
android:layout_below="@id/spinner"
android:layout_marginTop="20dp"
android:layout_marginStart="5dp"
/>
</RelativeLayout>

Step 3: MainActivity.java” java file − Add the following code to src/MainActivity.java

JAVA CODE IN MAIN ACTIVITY:

package com.yourname.yourpackagename;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 {

Spinner spinner;
TextView capital_textView;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//init the spinner
spinner = (Spinner) findViewById(R.id.spinner);

capital_textView = (TextView) findViewById(R.id.stadiumname);

findViewById(R.id.imageview);


//value to be shown in the spinner
String [] countries = {"Manchester United FC", "Liverpool FC", "Chelsea FC", "AC Milan & Inter Milan", "AFC Ajax"};
//array adapter used to bind values in the spinner
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, countries);

spinner.setAdapter(adapter);


//on item select event handling
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
switch(position){
case 0:
capital_textView.setText("OLD TRAFFORD");
//capital_textView.setImageDrawable(R.drawable.yourimagename);
break;
case 1:
capital_textView.setText("ANFIELD");
break;
case 2:
capital_textView.setText("STAMFORD BRIDGE");
break;
case 3:
capital_textView.setText("SAN SIRO");
break;
case 4:
capital_textView.setText("JOHAN CRUYFF ARENA");
break;
}
}

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

}
});

}

}

Review Application

Spinner allows you to select an item from a drop down menu

For example. When you are using Gmail application you would get drop down menu as shown below, you need to select an item from a drop down menu.

spinner gmail example

Step 4: Results and How to Work SPINNER Codes!

This tutorial gave you a hands on experience in using Android Spinner as a drop down menu.

Android Spinner — drop down list

In this lesson, you learnt Best Java code snippets using

//on item select event handling
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() in java.

how to write code spinner in java android
how to write code spinner in java android

How to use Spinner in Android !

What is Spinner

Simple Spinner (range of selection)

A button consists of text or an icon (or both text and an icon) that communicates what action occurs when the user touches it.

Alarm Button Example

Depending on whether you want a button with text, an icon, or both, you can create the button in your layout in three ways:

If you have any question write comments I will reply.

Thanks for reading my blog.

Have a nice day.

If you need Google AdMob Alternatives you can check out Start Ad Platfrom.

https://portal.start.io/#/signup?referredby=69e578e9-ec0c-24b3-a74e-f58373fac2ef&preferredsite=pub&source=directURL

Regards

5S JAVA AND KOTLIN LEARNING

--

--

5S JAVA AND KOTLIN LEARNING
5S JAVA AND KOTLIN LEARNING

Published in 5S JAVA AND KOTLIN LEARNING

Android Examples: Android Date and Time: DatePicker, TimePicker, Analog & Digital Clock, TextClock, Chronometer. • Android Widgets: TextView, EditVew, Button, RadioButton, CheckBox, RatingBox, ProgressBox, SeekBar, ToogleButton, Spinner. 5S JAVA AND KOTLIN LEARNING

5Ssoftwaresafety
5Ssoftwaresafety

Written by 5Ssoftwaresafety

Software developer, safety engineer, 5s safety creator