Updating ImeOptions of the current focused EditText - android

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?

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

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

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.

Related

How do I clear the focus on an editext? Other StackOverflow posts haven't helped much

I've looked at other StackOverflow posts, but many of them don't work, or the comments say that they don't work. I'm wondering how I can clear the focus on an edittext. I was hoping that when I close the keyboard, it would also lose focus, but this didn't happen.
Because of this, whenever someone exits the app and goes back in (with the app running in the background) the regular keyboard shows up, even though it's not supposed to. I have a whole system in place for managing the keyboard.
Code for showing/closing keyboard
private void closeEmojiKeyboard()
{
ivEmoji.setVisibility(View.VISIBLE);
ivKeyboard.setVisibility(View.GONE);
showKeyboard(etMessage);
llEmojiKeyboard.setVisibility(View.GONE);
}
private void showKeyboard(EditText view)
{
view.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
}
Code for hiding keyboard when someone opens the emoji keyboard
ivEmoji.setVisibility(View.GONE);
ivKeyboard.setVisibility(View.VISIBLE);
hideKeyboard(etMessage);
llEmojiKeyboard.setVisibility(View.VISIBLE);
And the code that hides the emoji keyboard when clicking the editext
llEmojiKeyboard.setVisibility(View.GONE);
showKeyboard(etMessage);
ivEmoji.setVisibility(View.VISIBLE);
ivKeyboard.setVisibility(View.GONE);
rvMessages.scrollToPosition(messagesList.size()-1);
Can anybody share some code with me that could help clear focus on my edittext? Thanks.
you can
ex 1.
editText.clearFocus()
ex 2.
EditText et = (EditText)view.findViewById(R.id.my_name);
et.setInputType(EditorInfo.TYPE_NULL); // setCursorVisible(false);

Soft Input Opens over a custom keyboard on Text Selection -- how to prevent

Many questions refer to a keyboard service rearing it's head at the wrong time, or how to substitute a view specific keyboard. I have no problem doing this. This problem is different in that the keyboard service pops up on top of a custom keyboard that was working fine until a long press to make a selection. At that point the default keyboard appears. I want to stop this.
As further clarification, it is not the long press that opens the system keyboard. It is action of making a selection. For example: A long press at the end of input does not select anything, but does pop up the "cut copy select all share..." dialog. When you click on "Select All" then the system keyboard opens.
I think the misleading suggestion of a link to a solution to this problem should be removed.
I use the following to install a special keyboard under an EditText:
MA_expression.setOnClickListener { view ->
mKeyboardView.visibility = View.VISIBLE
mKeyboardView.isEnabled = true
if (view != null) {
val imm = getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
imm.hideSoftInputFromWindow(view.windowToken, 0)
}
}
This works as expected:
Now the goal is to use "45" as the argument to a function, so the range of text that is to become the argument is selected (simple here, but it could just as well be embedded in a more complicated expression):
Now the problem is evident -- the standard keyboard service has popped up. It can be dismissed with the done button, the selection remains, my keyboard remains, the FUNa keyboard is selected and the function to apply is picked.
The result is correct, it is only the intervening system keyboard that must be told it is not wanted.
How is that done?
dismiss the android key board on the focus listener
view.setOnFocusChangeListener
view.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean hasFocus) {
if (hasFocus) {
val imm = getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
imm.hideSoftInputFromWindow(view.windowToken, 0)
}
}
});

How to enable keyboard on touch after disabling it with setTextIsSelectable

I am using a custom in-app keyboard, so I need to disable the system keyboard. I can do that with
editText.setShowSoftInputOnFocus(false);
for Android API 21+. But to do the same thing down to API 11, I am doing
editText.setTextIsSelectable(true);
Sometimes I want to show the system keyboard again after disabling it with setTextIsSelectable. But I can't figure out how. Doing the following does show the system keyboard, but if the user hides the keyboard and then clicks the EditText again, the keyboard still won't show.
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText, 0);
I guess I could do editText.setOnFocusChangeListener and then manually show or hide the system keyboard, but I would prefer to undo whatever setTextIsSelectable did. The following also does not work:
editText.setFocusable(true);
editText.setFocusableInTouchMode(true);
editText.setClickable(true);
editText.setLongClickable(true);
How do I do it?
Related question
Short answer
Doing the following will reverse the effects of setTextIsSelectable(true) and allow the keyboard to show again when the EditText receives focus.
editText.setTextIsSelectable(false);
editText.setFocusable(true);
editText.setFocusableInTouchMode(true);
editText.setClickable(true);
editText.setLongClickable(true);
editText.setMovementMethod(ArrowKeyMovementMethod.getInstance());
editText.setText(editText.getText(), TextView.BufferType.SPANNABLE);
Explanation
The thing that prevents the keyboard from showing is isTextSelectable() being true. You can see that here (thanks to #adneal).
The source code for setTextIsSelectable is
public void setTextIsSelectable(boolean selectable) {
if (!selectable && mEditor == null) return; // false is default value with no edit data
createEditorIfNeeded();
if (mEditor.mTextIsSelectable == selectable) return;
mEditor.mTextIsSelectable = selectable;
setFocusableInTouchMode(selectable);
setFocusable(selectable);
setClickable(selectable);
setLongClickable(selectable);
// mInputType should already be EditorInfo.TYPE_NULL and mInput should be null
setMovementMethod(selectable ? ArrowKeyMovementMethod.getInstance() : null);
setText(mText, selectable ? BufferType.SPANNABLE : BufferType.NORMAL);
// Called by setText above, but safer in case of future code changes
mEditor.prepareCursorControllers();
}
Thus, the code in the short answer section above first sets mTextIsSelectable to false with setTextIsSelectable(false) and then undoes all of the other side effects one-by-one.

Show soft input keyboard

I'm trying to show the soft input keyboard for a view on the touch event.
This line works:
inputManager.toggleSoftInputFromWindow(getWindowToken(),0,0);
But this line doesn't work:
inputManager.showSoftInput(this,0);
Why is it so? What if I want to connect the soft input to the view?
Thanks.
I think you are testing on emulator. not on real device?
It will not open the keyboard on AVD but it will open on real device, which does not have Hard key board.
To test it on AVD you need to disable the keyboard.
To disable keyboard use
Click on AVD manager > open you targeted AVD > Edit > Hardware > New > Keyboard Support > OK > Make it "NO"
try this:
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
try this in onclick event.
InputMethodManager imm =
(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,
InputMethodManager.HIDE_IMPLICIT_ONLY);
showSoftInput() won't work unless your View has focus. Moreover, calling requestFocus() does not give your View focus unless you first call setFocusableInTouchMode() and/or setFocusable() to true.
You need to request focus first and show the soft input as follows:
mEditTextStudy.requestFocus();
mEditTextStudy.post(
new Runnable() {
#Override
public void run() {
InputMethodManager imm =
(InputMethodManager)
getActivity()
.getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) {
imm.showSoftInput(mEditTextStudy, SHOW_FORCED);
}
}
});

EditText with textPassword inputType, but without Softkeyboard

i wanted to create an EditText with an android:inputType="textPassword. However i also do not want to use the SoftKeyboard for input.
So i tried setting InputMethod to null, but this would also disable the textPassword features of replacing password rendering to "*[lastchar]".
Is there any other way to disable the softKeyboard from showing up?
Uhm just figured it out myself:
Simply use:
pinInput.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
InputMethodManager mgr = (InputMethodManager) Pin.this.getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
});
This works well for me. I feared it might show the SoftKeyboard and then hide it again instantly, or flicker the screen, but does nothing like it.

Categories

Resources