Static Android Spinner?
-
14-11-2019 - |
Question
I'm writing an android app that has a spinner in it for difficulty settings (easy, medium, hard). What is happening is that when I go to the options layout I have the default (easy) is displayed. I can change it, and the effect I want happens. However, when I go back to the main layout (menu) and back again to the options layout it shows the default setting once again.
How do I get it to show my selection of medium or hard when I switch between the menu and options layouts? I've tried making a static method and creating my spinner there, but I just get errors. Please help! I will provide some code (either my own or an close approximation) if you think it will help you answer my question better. Thank you all in advance.
Solution
You need to use the method Spinner.setSelection()
to set which value the Spinner shows when it starts.
So you'd set a variable, eg difficulty
when the difficulty is selected, which would be an int value between 0-2 (0 = easy, 1 = medium, 2 = hard)
Then when you draw the spinner just do this:
nameOfYourSpinner.setSelection(difficulty);
OTHER TIPS
Firstly have some static variable in actiivty that keeps track of the spinner selection that have been made deafult 0...
@Override
protected void onResume() {
super.onResume();
nameOfYourSpinner.setSelection("static variable made to track position");
}