Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Updating ImeOptions of the current focused EditText

I have an EditText with the ImeOptions set to EditorInfo.IME_ACTION_NEXT. So the "Next" button is displayed on the keyboard when the field is focused. I want the button to change for "Done" WHILE the user is typing (for some reasons). So I have a TextWatcher and I try to change the ImeOptions to EditorInfo.IME_ACTION_DONE on "afterTextChanged" but the key on the keyboard doesn't change. I tried to hide the keyboard, change the ImeOptions and show the keyboard again but it doesn't work (this solution works for iOS).

Does someone know how to do that?

like image 365
Eselfar Avatar asked May 20 '16 16:05

Eselfar


3 Answers

I tried this and it works, the problem is that you can sometime see when the keyboard appears and disappears.

@Override
public void afterTextChanged(Editable s) {

    if (/*condition*/) {

        // Change the IME of the current focused EditText
        editText.setImeOptions(EditorInfo.IME_ACTION_DONE);


        // Hide the keyboard
        hideKeyboard(activity);

        // Restart the input on the current focused EditText
        InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.restartInput(editText);

        // Show the keyboard
        showKeyboard(activity);
    }
}
like image 126
Eselfar Avatar answered Nov 20 '22 08:11

Eselfar


You can try something like that:

if (your condition) {


    youreditText.setImeOptions(EditorInfo.IME_ACTION_DONE);
    hideKeyboard(activity);
    InputMethodManager input= (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
    input.restartInput(youreditText);

    showKeyboard(youractivity);
}
like image 35
Arslan Ali Avatar answered Nov 20 '22 09:11

Arslan Ali


From the answers above:

// Restart the input on the current focused EditText
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.restartInput(editText);

Only restarting the input does the trick. Otherwise, presumeably keyboard flickering is to be expected. Input restart also doesn't clear the field's contents i.e. doesn't lead to data loss, which is the desired behavior.

like image 1
Ivan Caravanio Avatar answered Nov 20 '22 08:11

Ivan Caravanio