Search code examples
javaandroidformsspinnerandroid-spinner

Android Spinner - Focus forward after confirming (not changing) selection


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?


Solution

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