1

I want to develop an Android app that 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 entering), 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.

Does anyone have a clue how I could implement that?

frid000
  • 41
  • 8
  • 1
    Maybe `setOnItemClickListener` would be helpful? Cause that should get invoked no matter which item you tap. Or try adding the same code in `onNothingSelected` (I'm assuiming that may get called when you don't select anything or select the same item again. – Vucko Nov 02 '21 at 13:27
  • 1
    "You should not call OnItemClickListener for a spinner. A Spinner does not support item click events." (See https://stackoverflow.com/questions/51683253/spinner-setonitemclicklistener-not-working/51683280). Unfortunately, `onNothingSelected` isn't called when the same item stays selected... – frid000 Nov 02 '21 at 13:30

2 Answers2

0

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.
akhil nair
  • 1,371
  • 1
  • 11
  • 19
  • That could be a solution. But it would complicate the input-action. The app should allow quickly repeated inputs with the same spinner-selection – frid000 Nov 02 '21 at 15:47
  • @frid000 Unfortunately `onItemSelected` in the spinner not called when selection doesn't change is a drawback and limitation from Android. – akhil nair Nov 03 '21 at 08:14
0

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) {
        }
    });
frid000
  • 41
  • 8