Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Skipping disabled EditText's when pressing Next IME button on soft keyboard

I have a LinearLayout with several EditText's, all of them created programmatically (not with an XML layout), and in particular without IDs.

When I'm typing in one of the EditText's, and the next one (respective to focus) is disabled, and I press the Next IME button on the keyboard, the focus advances to the disabled EditText, but I can't type anything in it.

What I was expecting was focus to advance to the next enabled EditText. I also tried, in addition to making the EditText disabled via edittext.setEnabled(false), to disable its focusability via edittext.setFocusable(false) and edittext.setFocusableInTouchMode(false), and to set a TYPE_NULL input type, but to no avail.

Any hints?

Thanks ;)

like image 772
Venator85 Avatar asked Aug 23 '13 16:08

Venator85


People also ask

How to Show soft keyboard in Android?

By default, the soft keyboard may not appear on the emulator. If you want to test with the soft keyboard, be sure to open up the Android Virtual Device Manager ( Tools => Android => AVD Manager ) and uncheck "Enable Keyboard Input" for your emulator.

How to hide soft keyboard?

You can force Android to hide the virtual keyboard using the InputMethodManager, calling hideSoftInputFromWindow , passing in the token of the window containing your focused view. This will force the keyboard to be hidden in all situations. In some cases, you will want to pass in InputMethodManager.

What is Android soft keyboard?

The Android system shows an on-screen keyboard — known as a soft input method — when a text field in your UI receives focus. The keyboard takes about half the screen; in other words, you only have half of the screen to display any information.


2 Answers

Solved by examining how the next focusable is found by the keyboard from this blog post and by subclassing EditText:

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.EditText;

public class MyEditText extends EditText {

    public MyEditText(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public MyEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyEditText(Context context) {
        super(context);
    }

    @Override
    public View focusSearch(int direction) {
        View v = super.focusSearch(direction);
        if (v != null) {
            if (v.isEnabled()) {
                return v;
            } else {
                // keep searching
                return v.focusSearch(direction);
            }
        }
        return v;
    }

}

More details:

ViewGroup implementation of focusSearch() uses a FocusFinder, which invokes addFocusables(). The ViewGroup's implementation tests for visibility, while the View implementation tests for focusability. Neither test for the enabled state, which is why I added this test to MyEditText above.

like image 106
Venator85 Avatar answered Nov 16 '22 02:11

Venator85


I solved it setting the focusable property to false, not only the enabled property:

editText.setEnabled(false);
editText.setFocusable(false);
like image 37
IgniteCoders Avatar answered Nov 16 '22 01:11

IgniteCoders