-1

Hai, I want to edit the spinner value in runtime in android application

user555910
  • 2,627
  • 6
  • 22
  • 28

5 Answers5

4

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.

Barmaley
  • 16,638
  • 18
  • 73
  • 146
2

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

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
1

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);
    }
}
Patrick
  • 63
  • 6
Balaji
  • 2,024
  • 17
  • 13
1

Short & simple answer:

final Spinner team = (Spinner) findViewById(R.id.team_name);
team.setSelection(5); // To set 6th value in the list.
Sumoanand
  • 8,835
  • 2
  • 47
  • 46
1

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.

Community
  • 1
  • 1
ggurov
  • 1,496
  • 2
  • 15
  • 21