7

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 ;)

Venator85
  • 10,245
  • 7
  • 42
  • 57
  • Possible duplicate of [Move to another EditText when Soft Keyboard Next is clicked on Android](https://stackoverflow.com/questions/17989733/move-to-another-edittext-when-soft-keyboard-next-is-clicked-on-android) – Miloš Černilovský Jun 27 '17 at 10:07

3 Answers3

12

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.

Venator85
  • 10,245
  • 7
  • 42
  • 57
5

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

editText.setEnabled(false);
editText.setFocusable(false);
IgniteCoders
  • 4,834
  • 3
  • 44
  • 62
  • 1
    It's quite interesting, that it didn't work for me when I set the focusable property in layout XML, I had to do that in code to make it work. (ImageButton inside my custom compound view) – Almighty May 11 '19 at 17:44
0

See

EditText editText = (EditText) findViewById(R.id.search);
editText.setOnEditorActionListener(new OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        boolean handled = false;
        if (actionId == EditorInfo.IME_ACTION_SEND) {
            sendMessage();
            handled = true;
        }
        return handled;
    }
});

This was grabbed from http://developer.android.com/training/keyboard-input/style.html#Action .

If you can figure out how to focus on the next TextView, you can add an OnEditorActionListener to every TextView and have it pass the focus to the next one if it is disabled.

cade
  • 563
  • 4
  • 15
  • Finding the next EditText is the point here. Since the EditText's in my activity are dynamic (their number and state is decided at runtime), I'd prefer not to keep a list of EditText's. How does the keyboard find the next EditText? Maybe looking into its source code may prove helpful... – Venator85 Aug 24 '13 at 08:32