I want to develop an Andorid app which should run on devices with an external keyboard. The user should be able to go through a form using the enter key. Now, I have an issue with a Spinner and a Button.
The only way (I found) to transfer the focus from the Spinner to the Button is to use an setOnItemSelectedListener
:
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
root.findViewById(R.id.button).requestFocus();
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {}
});
This works when the user selects another item from the Spinner than the current value. However, it doesn't work when the user opens the dialog (with enter), then doesn't select another value (but presses enter to confirm the current choice). I guess the OnItemSelected
-Event isn't triggered when the value doesn't change.
Anyone has a clue how I could implement that?
CodePudding user response:
As the onItemSelected
in the spinner is not called if you don't change the selection then you would have to do something like this
- Force the user to select something from the spinner by adding a default value to the spinner like "Please select". Thus you could also show some error message if he doesn't select and eventually your
onItemSelected
listener would get invoked.
CodePudding user response:
I ended up by using the solution from this answer: (Which extends the Spinner-class)
How can I get an event in Android Spinner when the current selected item is selected again?
Then, it is possible to shift the focus to the next item with
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
root.findViewById(R.id.Button).requestFocus();
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});