24

I'm using 4 EditText fields and 2 spinners in an activity. The order of these components are 2 EditText, then 2 spinners and then 2 EditText fields.

The problem occurs when I transfer focus (with the help of soft keyboard next button) from EditText to spinner, spinner does not get the focus and the focus is transferred to the next EditText field that was placed after the spinners.

I have used requestfocus() on spinner, but it did not work.

How do I make sure the spinner gets focus?

2
  • 1
    Please grant the answer flag.
    – JJD
    Apr 12, 2013 at 10:04
  • 1
    Minus 1 for not granting the selected answer.
    – Jorge Gil
    Jan 8, 2015 at 4:23

4 Answers 4

44

I managed to find a solution other then repositioning my Spinner. In the EditText before the spinner, add this listener:

editTextBefore.setOnEditorActionListener(new OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView textView, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_NEXT) {
            hideKeyboard();
            textView.clearFocus();
            spinner.requestFocus();
            spinner.performClick();
        }
        return true;
    }
});

You also need to add these line to able spinner to get focus:

spinner.setFocusable(true); // can be done in XML preferrable

My hideKeyboard function was just a visual detail that I wanted to add so the keyboard get hidden:

private void hideKeyboard() {
    InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    inputManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(),
            InputMethodManager.HIDE_NOT_ALWAYS);
}

Hope I have helped in this tricky question.
The flag InputMethodManager.HIDE_NOT_ALWAYS can be found in the documentation.

3
  • 1
    You don't have to set focusableInTouchMode? Jul 30, 2014 at 13:47
  • It would be helpful to set focusableInTouchMode. Also, it seemed not to work if you try this in XML, try programmatically.
    – Jorge Gil
    Jan 8, 2015 at 4:20
  • 1
    In addition you have to set the attribute android:imeOptions="actionNext" for EditText in xml. Dec 17, 2017 at 17:25
20

Thanks, I have solved by doing the following:

  1. I set the Spinner object on top (within the onCreate method) just to make sure that my code gets executed first
  2. I used the following:

    Spinner s1 = (Spinner) findViewById(R.id.spinner1); 
    s1.setFocusable(true); 
    s1.setFocusableInTouchMode(true);
    s1.requestFocus();
    
1
  • 3
    You clearly got the solution from @shanet so you should have just selected their answer. Feb 29, 2016 at 14:23
7

This is a shot in the dark, but try setting the focusable property (in XML or in code; whatever way you are doing it) to true on the spinner.

http://developer.android.com/reference/android/view/View.html#attr_android:focusable

EDIT: Also, see this question: Can't manage to requestFocus a Spinner

2
  • thanx i have solved by doing the following: 1) I set the Spinner object on top (Within the onCreate method) just to make sure that my code gets executed first. 2) I used the following: Spinner s1 = (Spinner) findViewById(R.id.spinner1); s1.setFocusable(true); s1.setFocusableInTouchMode(true);
    – Shameen
    Jun 23, 2011 at 6:45
  • 1
    @Shameen You could've set those flags in XML. Jul 30, 2014 at 14:03
-1

I just had the same problem. I solved it using the nextFocusDown/Up/Left/Right properties.

<EditText
    android:id="@+id/tPhone"
    ...
    android:focusableInTouchMode="true"
    android:focusable="true"
    android:nextFocusDown="@+id/sCountry"/>

<Spinner
    android:id="@+id/sCountry"
    ....
    android:focusableInTouchMode="true"
    android:focusable="true"
    android:nextFocusUp = "@+id/tPhone"
    android:nextFocusDown="@+id/tStreet"/>

 <EditText
    android:id="@+id/tStreet"
    ...
    android:visibility="gone"
    android:focusableInTouchMode="true"
    android:focusable="true" 
    android:nextFocusUp = "@+id/sCountry"/>

But why this is even neccessary... beats me.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.