How to set a default text to a Spinner
Use this code
declaration
String selected, spinner_item;
spinner code
sp.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) { selected = sp.getSelectedItem().toString(); if (!selected.equals("Country")) spinner_item = selected; System.out.println(selected); setid(); } private void setid() { sp.setSelection(sp_position); t.setText(spinner_item); } @Override public void onNothingSelected(AdapterView<?> arg0) { } });
You only need 3 lines of code for this:
(1) Declare a global variable that is used to save and load the spinner item ID. By global, I mean declaring it right after "public class MainActivity extends Activity {".
int mySpinner_selectedId;
(2) Then, add the following code AFTER the user has made his/her choice. One good location would be a button that appears together with your spinner.
mySpinner_selectedId = mySpinner.getSelectedItemPosition();
(3) Finally, add the following code right after "mySpinner.setAdapter(adapter);" to load the last selected item.
mySpinner.setSelection(mySpinner_selectedId);
You're most welcomed.