Vijaya Varma Lanke
Vijaya Varma Lanke

Reputation: 621

how to make a selected value in spinner as a default one for next session in android

I am working on an android project and I am using a spinner which is populated manually. For example if, in the spinner I have the following items:

select

Category 1

Category 2

Category 3

Initially the spinner value is select now i selected category 2 How would I programmatically make Category 2 as the default value for next session(opening the app next time). Thank you.

Upvotes: 0

Views: 173

Answers (3)

Luca Ziegler
Luca Ziegler

Reputation: 4134

In the first session you save the selected index in a SharedPreferences:

PreferenceManager.getDefaultSharedPreferences(this).edit().putInt("srIndex",spinner.getSelectedItemPosition());

And in the second session you read the saved index and set it to the spinner:

spinner.setSelection(PreferenceManager.getDefaultSharedPreferences(this).getInt("srIndex",0));

Upvotes: 1

Shashank
Shashank

Reputation: 1127

You can use SharedPreferences to save the index of selected category and every time activity is loaded retrieve the index from SharedPreferences and set selection

Spinner.setSelection(index);

If you not aware of SharedPreferences you can refer to StackOverflow

Upvotes: 0

jjz
jjz

Reputation: 2057

If you know the position of "Category 2" you can do the item below. You can get the index from the adapter.

spinner.setSelection(indexOfCategory2);

Upvotes: 0

Related Questions