0% found this document useful (0 votes)
228 views7 pages

How Can I Add Items To A Spinner in Android - Stack Overflow PDF

This document discusses several ways to add items to a spinner in Android. The top answers provide code samples to populate a spinner by: 1. Creating a string array and ArrayAdapter and setting the adapter to the spinner. 2. Adding items directly to an adapter and notifying it of changes. 3. Reading items from a JSON array and adding them to an adapter to assign to a spinner.

Uploaded by

Shoaib Quraishi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
228 views7 pages

How Can I Add Items To A Spinner in Android - Stack Overflow PDF

This document discusses several ways to add items to a spinner in Android. The top answers provide code samples to populate a spinner by: 1. Creating a string array and ArrayAdapter and setting the adapter to the spinner. 2. Adding items directly to an adapter and notifying it of changes. 3. Reading items from a JSON array and adding them to an adapter to assign to a spinner.

Uploaded by

Shoaib Quraishi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Stack Overflow

Questions

Tags

Users

sign up
Badges

Unanswered

log in

Ask

Read this post in our app!

How can I add items to a spinner in Android?


43

android

spinner

How can I add items to a spinner?

share

improve this question


Asked
Mar 9 '11 at 5:16

Jazz

Michael Petrotta
42.3k 10 101

145

6 Answers

61

Edited
Mar 9 '11 at 5:19

order by votes

XML file:
<Spinner android:id="@+id/Spinner01"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

Java file:
public class SpinnerExample extends Activity {
private String[] arraySpinner;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

this.arraySpinner = new String[] {


"1", "2", "3", "4", "5"
};
Spinner s = (Spinner) findViewById(R.id.Spinner01);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, arraySpinner);
s.setAdapter(adapter);

See spinner example.

share

improve this answer


frieza
5,769

8 40 58

user3454984
23 5

Answered
Mar 9 '11 at 5:24

Edited
Feb 13 '15 at 5:44

While this may theoretically answer the question, it would be preferable to include the essential parts
of the answer here, and provide the link for reference. Bill the Lizard Jan 26 '12 at 21:00

use this android.R.layout.simple_spinner_dropdown_item in place of this


android.R.layout.simple_spinner_item it looks lot better sai Jun 4 '14 at 17:33

does not compile... ??? Lou Morda Dec 9 '14 at 8:58

What is array_spinner? name of the array is arraySpinner. is it spelling mistake or it has to be like that
only> SurajS Feb 3 '15 at 9:22

add a comment

41

I know this is an old question, but I've found another option in case anyone needs it:
spinner definition in the layout file
<Spinner android:id="@+id/spinner"
android:layout_width="fill_parent"
android:drawSelectorOnTop="true"
android:prompt="@string/spin"
android:entries="@array/spinnerItems"
/>

Items definition in the file array.xml:


<resources>
<string-array name="spinnerItems">
<item>item1</item>
<item>item2</item>
<item>item3</item>
<item>item4</item>
</string-array>
</resources>

Link to the full source of the code

share

improve this answer


maxivis
742 9

19

Answered
Apr 15 '13 at 23:20

I tried all the methods above and a few other tutorials, but this i the only one that worked for me. Thank you so
much! snapplex Feb 7 '14 at 23:42

Glad to help you @snapplex ;) maxivis Feb 10 '14 at 23:36

This should be top answer. Clean. nobetw Apr 16 '15 at 0:41

add a comment

18

Try this Code...


final List<String> list=new ArrayList<String>();
list.add("Item 1");
list.add("Item 2");
list.add("Item 3");
list.add("Item 4");
list.add("Item 5");
final String[] str={"Report 1","Report 2","Report 3","Report 4","Report 5"};
final Spinner sp1= (Spinner) findViewById(R.id.spinner1);
final Spinner sp2= (Spinner) findViewById(R.id.spinner2);
ArrayAdapter<String> adp1=new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,list);
adp1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp1.setAdapter(adp1);
ArrayAdapter<String> adp2=new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item,str);
adp2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

You can also add spinner item value through String array xml file..
<resources>
<string name="app_name">Spinner_ex5</string>
<string name="hello_world">Hello world!</string>
<string name="menu_settings">Settings</string>
<string name="title_activity_main">MainActivity</string>
<string-array name="str2">
<item>Data 1</item>
<item>Data 2</item>
<item>Data 3</item>
<item>Data 4</item>
<item>Data 5</item>
</string-array>
</resources>

In mainActivity.java :

final Spinner sp3= (Spinner) findViewById(R.id.spinner3);


ArrayAdapter<CharSequence> adp3=ArrayAdapter.createFromResource(this,
R.array.str2, android.R.layout.simple_list_item_1);
adp3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp3.setAdapter(adp3);
sp3.setOnItemSelectedListener(new OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long id) {
// TODO Auto-generated method stub
String ss=sp3.getSelectedItem().toString();
Toast.makeText(getBaseContext(),ss , Toast.LENGTH_SHORT).show

();

}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub

share

improve this answer


Balaji
1,153

8 10

Name is Nilay
1,603 13

52

Answered
Jul 31 '12 at 0:40

Edited
Jul 31 '12 at 12:56

plus one for setting dropDownViewResource, thx! Lou Morda Nov 25 '14 at 19:39

add a comment

For adding item in Spinner, you can do one thing, try to create an adapter and then
add/remove items into the adapter, then you can easily bind that adapter to spinner by
using setAdapter() method.
Here is an example:
spinner.setAdapter(adapter);
adapter.add(item1);
adapter.add(item2);
adapter.add(item3);
adapter.add(item4);
adapter.add(item5);
adapter.notifyDataSetChanged();
spinner.setAdapter(adapter);

share

improve this answer


Paresh Mayani
67.6k 50

180 235

Answered
Mar 9 '11 at 6:01

thanx for ur reply i got the solution also now i am facing one problem on it. Jazz Mar 9 '11 at 8:03

I want to fetch the selected item of spinner i.e its position as further i want to pass it to other method see what
i did... dataspin.setOnItemSelectedListener(new myitemlistener()); Jazz Mar 9 '11 at 8:06
dataspin is my Spinner object Jazz Mar 9 '11 at 8:07
@Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { // TODO Autogenerated method stub String p=parent.getItemAtPosition(position).toString(); int pos=(int)
parent.getItemIdAtPosition(position); System.out.println("position of spinner..."+pos); setSearchView(pos); }
Jazz Mar 9 '11 at 8:08
if i do this than spinner item is not displaying Jazz Mar 9 '11 at 8:09

add a comment

This code basically reads a JSON array object and convert each row into an option in
the spinner that is passed as a parameter:
public ArrayAdapter<String> getArrayAdapterFromArrayListForSpinner(ArrayList<JSONObject>
aArrayList, String aField)
{
ArrayAdapter<String> aArrayAdapter = new ArrayAdapter<String>(context, android.R.layout.si
mple_spinner_item);
aArrayAdapter.setDropDownViewResource(R.layout.multiline_spinner_dropdown_item); //and
roid.R.layout.simple_spinner_dropdown_item
try {
for (int i = 0; i < aArrayList.size(); i++)
{
aArrayAdapter.add(aArrayList.get(i).getString(aField));
}
} catch (JSONException e) {
e.printStackTrace();
ShowMessage("Error while reading the JSON list");
}
return aArrayAdapter;
}

share

improve this answer


EASI
2,342

4 29 76

add a spinner to xml layout then add this code to java file :

Answered
Apr 15 '13 at 23:24

Spinner spinner;
spinner = (Spinner) findViewById(R.id.spinner1) ;
java.util.ArrayList<String> strings = new java.util.ArrayList<>();
strings.add("Mobile") ;
strings.add("Home");
strings.add("Work");
SpinnerAdaptor snprAdapter = new SpinnerAdaptor(AddMember.this,R.layout.support_simple_s
pinner_dropdown_item , strings);
spinner.setAdapter(snprAdapter);

share

improve this answer


Hossein
106 1

Answered
Apr 14 '14 at 20:34

Your Answer

log in
or
Name

Email

By posting your answer, you agree to the privacy policy and terms of service.

meta chat tour help blog privacy policy legal contact us full site
Download the Stack Exchange Android app
2016 Stack Exchange, Inc

Post Your Answer

You might also like