Hai, I want to edit the spinner value in runtime in android application
-
So what is the problem you are facing in that – ingsaurabh Dec 29 '10 at 05:47
-
@schwiz please understand my question.i want edittable spinner. – user555910 Dec 29 '10 at 06:08
-
2sounds more like you want code written for you than a question. – Nathan Schwermann Dec 29 '10 at 18:20
5 Answers
If you meant fill Spinner values runtime: there's nothing special in that Do smth like:
1) Instantiate Spinner
thru xml resource
2) Get reference to Spinner object thru Activity.findViewById()
3) Define Adapter
to your spinner :
ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(context, android.R.layout.simple_spinner_item)
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
4) Fill adapter with desired values thru adapter.add()
If you meant make Spinner values editable as EditText
it's a bit complicated.
i) You need to define your own styles instead of built-in styles simple_spinner_item
and simple_spinner_dropdown_item
, like:
<?xml version="1.0" encoding="utf-8"?>
<!--
~ my_simple_spinner_item.xml
-->
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mySimpleSpinnerItem"
style="?android:attr/mySpinnerItemStyle"
android:singleLine="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
ii) Then just apply those styles to your Spinner
Never checked but should work, at least I used to redefine styles for my Spinner in this way.

- 16,638
- 18
- 73
- 146
Call setSelection()
to change the selected item in the Spinner
. Otherwise, there is no such thing as an "editable" Spinner
in Android.

- 986,068
- 189
- 2,389
- 2,491
I hope it will helpful to you.
Try this Code..
public class MainActivity extends Activity {
Spinner sp;
EditText et;
List<String> li;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
li=new ArrayList<String>();
li.add("Item 1");
li.add("Item 2");
li.add("Item 3");
sp=(Spinner) findViewById(R.id.spinner1);
Button b=(Button) findViewById(R.id.button1);
et=(EditText)findViewById(R.id.editText1);
add();
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
li.add(et.getText().toString());
et.setText(null);
add();
}
});
}
private void add() {
ArrayAdapter<String> adp=new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line,li);
adp.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
sp.setAdapter(adp);
}
}
Short & simple answer:
final Spinner team = (Spinner) findViewById(R.id.team_name);
team.setSelection(5); // To set 6th value in the list.

- 8,835
- 2
- 47
- 46
In my question here: Android editable spinner plus virtual keyboard - is it possible? I placed working code for editable spinner. The only problem is with the virtual keyboard presence.