Spinner in from java android application — Java Android
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.
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.“
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.
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.
In this lesson, you learnt Best Java code snippets using
//on item select event handling
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() in java.
How to use Spinner in Android !
What is Spinner
A button consists of text or an icon (or both text and an icon) that communicates what action occurs when the user touches it.
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.
Regards
5S JAVA AND KOTLIN LEARNING