Android AutoCompleteTextView - handle dynamic IME option change by "re-selecting" view

332 Views Asked by At

I'm starting with one programatically created AutoCompleteTextView view on my layout. After user typed valid data into the field, I'm creating a new AutoCompleteTextView object right under the first one by the same way, with the same parameters. I'm storing all of the field references in an ArrayList to keep up with the last one, this way I'm making sure that there will be new fields only under the one at the bottom (basically the last element in the reference list) - so it goes on at every field.

I would like to add this feature: when a new field is created, I change the last field's IME options to EditorInfo.IME_ACTION_NEXT and nextFocusForward attribute to the freshly created field - also programmatically. What I want to achieve: right when user presses Enter on the keyboard, last field's focus jumps to the new field. I'm using this code to set new IME options:

ArrayList<AutoCompleteTextView> fields = new ArrayList<>();

//Creating freshlyCreatedField, IT'S STILL NOT IN THE fields LIST!

AutoCompleteTextView currentlySelectedField = fields.get(fields.size() - 1); // last element

currentlySelectedField.setImeOptions(EditorInfo.IME_ACTION_NEXT);
currentlySelectedField.setNextFocusForwardId(freshlyCreatedField.getId());
currentlySelectedField.setSingleLine(true); //ACTV needs it to get IME to work

//Adding freshlyCreatedField to the fields list,
//so next time it will be the currentlySelectedField

But after running this code nothing happens to the currently selected AutoCompleteTextView object. Clearing and requesting focus did not work, and making it work by the reversed way (creating +1 "invisible" (technically GONE) field everytime) would be much more painful.

One more thing: after selecting any other field and re-selecting the one with configuration changes the Enter button works as it should first time! If I could do the same programmatically, it would solve my problem... so, any ideas how to do it? (Of course, I welcome better solutions also... ;) )

1

There are 1 best solutions below

0
On

After some days, I've started to play with this problem again. Yes, it is a solution, but no, it's not so pretty as I would it to be like...

Anyhow, I created this code:

ArrayList<AutoCompleteTextView> fields = new ArrayList<>();

//Creating freshlyCreatedField, IT'S STILL NOT IN THE fields LIST!

//Last element
AutoCompleteTextView currentlySelectedField = fields.get(fields.size() - 1);

currentlySelectedField.setOnEditorActionListener(
        new TextView.OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        boolean handled = false;
        if (actionId == EditorInfo.IME_ACTION_NEXT 
                || actionId == EditorInfo.IME_ACTION_DONE 
                || event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
            freshlyCreatedField.requestFocus();
            handled = true;
        }

        return handled;
    }
});

//Adding freshlyCreatedField to the fields list,
//so next time it will be the currentlySelectedField

I put an EditorActionListener to the field actually under edit where the keyboard listens for one of the IME_ACTION_NEXT (if it's already set) and IME_ACTION_DONE action IDs (it's the default), or for the Enter key (for the case if someone would like to use physical keyboard). Then it jumps to the field below.

It works great, there aren't terrible problems with this approach. However I'm a little bit sad, because all of these possibilities stay invisible. The Done action button switches to Next only by clicking to somewhere else as I wrote it down in my question. So I think my "solution" can work, but it's not the best answer, so feel free to write your thoughts down if you have another idea... :)