1/10/2021 Android Spinner Example - javatpoint
Android Spinner Example
Android Spinner is like the combox box of AWT or Swing. It can be used to display
the multiple options to the user in which only one item can be selected by the user.
Android spinner is like the drop down menu with multiple values from which the end
user can select only one value.
Android spinner is associated with AdapterView. So you need to use one of the adapter
classes with spinner.
Android Spinner class is the subclass of AsbSpinner class.
Android Spinner Example
In this example, we are going to display the country list. You need to use ArrayAdapter class to store the country list.
Let's see the simple example of spinner in android.
activity_main.xml
Drag the Spinner from the pallete, now the activity_main.xml file will like this:
File: activity_main.xml
[Link] 1/6
1/10/2021 Android Spinner Example - javatpoint
<?xml version="1.0" encoding="utf-8"?>
<[Link] xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="[Link]">
<Spinner
android:id="@+id/spinner"
android:layout_width="149dp"
android:layout_height="40dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.502"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.498" />
</[Link]>
Activity class
Let's write the code to display item on the spinner and perform event handling.
File: [Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
[Link] 2/6
1/10/2021 Android Spinner Example - javatpoint
public class MainActivity extends AppCompatActivity implements
[Link] {
String[] country = { "India", "USA", "China", "Japan", "Other"};
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
//Getting the instance of Spinner and applying OnItemSelectedListener on it
Spinner spin = (Spinner) findViewById([Link]);
[Link](this);
//Creating the ArrayAdapter instance having the country list
ArrayAdapter aa = new ArrayAdapter(this,[Link].simple_spinner_item,country);
[Link]([Link].simple_spinner_dropdown_item);
//Setting the ArrayAdapter data on the Spinner
[Link](aa);
//Performing action onItemSelected and onNothing selected
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long id) {
[Link](getApplicationContext(),country[position] , Toast.LENGTH_LONG).show();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
Output:
[Link] 3/6
1/10/2021 Android Spinner Example - javatpoint
← prev next →
Youtube For Videos Join Our Youtube Channel: Join Now
Help Others, Please Share
Learn Latest Tutorials
Apache Solr MongoDB Gimp Tutorial Verilog
Tutorial tutorial Tutorial
Gimp
Solr MongoDB Verilog
[Link] 4/6
1/10/2021 Android Spinner Example - javatpoint
Teradata PhoneGap Gmail Tutorial [Link] Tutorial
Tutorial Tutorial
Gmail [Link]
Teradata PhoneGap
PLC tutorial Adobe Postman
Illustrator Tutorial
PLC Tutorial
Postman
Illustrator
Preparation
Aptitude Logical Verbal Ability Interview
Reasoning Questions
Aptitude Verbal A.
Reasoning Interview
Company
Interview
Questions
Company
Trending Technologies
Artificial AWS Tutorial Selenium Cloud tutorial
Intelligence tutorial
Tutorial AWS Cloud
Selenium
AI
Hadoop ReactJS Data Science Angular 7
tutorial Tutorial Tutorial Tutorial
Hadoop ReactJS D. Science Angular 7
Blockchain Git Tutorial Machine DevOps
Tutorial Learning Tutorial Tutorial
Git
Blockchain ML DevOps
[Link] / MCA
DBMS tutorial Data DAA tutorial Operating
Structures System tutorial
DBMS tutorial DAA
OS
DS
[Link] 5/6
1/10/2021 Android Spinner Example - javatpoint
Computer Compiler Computer Discrete
Network tutorial Design tutorial Organization and Mathematics
Architecture Tutorial
C. Network Compiler D.
COA D. Math.
Ethical Computer Software html tutorial
Hacking Tutorial Graphics Tutorial Engineering
Tutorial Web Tech.
E. Hacking C. Graphics
Software E.
Cyber Automata C Language C++ tutorial
Security tutorial Tutorial tutorial
C++
Cyber Sec. Automata C
Java tutorial .Net Python tutorial List of
Framework Programs
Java tutorial Python
Programs
.Net
Control Data Mining
Systems tutorial Tutorial
Control S. Data Mining
[Link] 6/6