Congratulations - C# Corner Q4, 2022 MVPs Announced
Why Join
Become a member
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
TECHNOLOGIES
ANSWERS
LEARN
NEWS
BLOGS
VIDEOS
INTERVIEW PREP
BOOKS
EVENTS
Training
Live
JOBS
MORE
CAREER
MEMBERS
How to Set Spinner in Android
Abhijeet Singh
Apr 01, 2020
15.8k
0
1
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
Print
Other Artcile
Here you will see how can we set spinner in Android to select one value from a set.
spinnertoday.rar
Spinner
Spinners provide a quick way to select one value from a set. In the default state, a spinner shows its currently selected value.
Procedure
Start the Eclipse IDE.
Create a new project.
Create a MainActivity.java file.
Create an activity_main.xml for layout design.
Declare a spinner in the XML layout, for example:
<
Spinner
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:id
=
"@+id/ss"
/>
Declare a string array with some elements and pass these elements to the ArrayAdapter.
For instance
String[] str={
"select"
,
"yellow"
,
"red"
,
"Green"
,
"purple"
,
"blue"
};
ArrayAdapter<String> adap=
new
ArrayAdapter<String>
(
this
, android.R.layout.simple_spinner_item,str);
The code is given below.
MainActivity.java
package
com.example.spinnertodayy;
import
android.app.Activity;
import
android.graphics.Color;
import
android.os.Bundle;
import
android.view.View;
import
android.widget.AdapterView;
import
android.widget.AdapterView.OnItemSelectedListener;
import
android.widget.ArrayAdapter;
import
android.widget.RelativeLayout;
import
android.widget.Spinner;
import
android.widget.TextView;
import
android.widget.Toast;
public
class
MainActivity
extends
Activity {
Spinner s1;
RelativeLayout ll;
String[] str={
"select"
,
"yellow"
,
"red"
,
"Green"
,
"purple"
,
"blue"
};
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
s1=(Spinner) findViewById(R.id.ss);
ll=(RelativeLayout) findViewById(R.id.rr);
ArrayAdapter<String> adap=
new
ArrayAdapter<String>
(
this
, android.R.layout.simple_spinner_item,str);
s1.setAdapter(adap);
s1.setOnItemSelectedListener(
new
OnItemSelectedListener() {
public
void
onItemSelected(AdapterView<?> arg0, View arg1,
int
arg2,
long
arg3) {
// TODO Auto-generated method stub
String ss1=arg0.getItemAtPosition(arg2).toString();
String ss=((TextView)arg1).getText().toString();
Toast.makeText(getApplicationContext(), ss,
10000
).show();
}
public
void
onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
}
activity_main.xml
<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"
android:id=
"@+id/rr"
android:paddingBottom=
"@dimen/activity_vertical_margin"
android:paddingLeft=
"@dimen/activity_horizontal_margin"
android:paddingRight=
"@dimen/activity_horizontal_margin"
android:paddingTop=
"@dimen/activity_vertical_margin"
tools:context=
".MainActivity"
>
<Spinner
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:id=
"@+id/ss"
/>
</RelativeLayout>
Output
Android
Android spinner
set spinner in Android
Recommended Ebook
Printing in C# Made Easy
Download Now!
Similar Articles
FEATURED ARTICLES
View All
C# Asynchronous Programming
Challenge yourself
C# Skill
Get Certified
Microsoft Azure