Why wont the softkeyboard show here? - android

I'm in a DialogFragment in the onCreateDialog I'm using the InputMethodManager to show the keyboard when the dialog opens. But, its not working for some reason. Anyone know why?
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder b = new Builder(getActivity());
b.setMessage("Enter a 5 digit zipcode");
final EditText et = new EditText(getActivity());
et.setInputType(InputType.TYPE_CLASS_NUMBER);
et.requestFocus();
et.setHint("Zipcode");
InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(INPUT_METHOD_SERVICE);
imm.showSoftInput(et, InputMethodManager.SHOW_FORCED);
b.setView(et);
return b.create();
}

Have you tried running your code on an Android device? If you are using an android emulator, chances are that your Keyboard Support inside the Hardware properties for your emulator is turned off.
To enable it, go to AVD Manager-> Edit AVD-> Hardware-> Add Keyboard Support / Edit Keyboard Support and enable its value

You need perhaps to request focus on EditText. Also I found getWindow() more reliable if avail on Dialog.
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
et.requestFocus();
getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);
Update my answer. Your dialog is perhaps already running. So the request is made at the current window then you create the dialog. The dialog has its own window perhaps and so it does not show?

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

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?
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.

Disable soft-keyboard from EditText but still allow copy/paste?

Hi I'm making custom dialer so I create my own input pad.
The problem is how do I disable the EditText but still allow cut/copy/paste? The stock dialer can do this.
I have tried android:focusable="false" but it disables cut/copy (can still paste though).
I also tried to disable the inputType programatically which disables all three commands:
myEditText.setInputType(InputType.TYPE_NULL); //Can't cut/copy/paste
Disabling it from manifest also doesn't work:
android:configChanges="orientation|keyboardHidden" //Keyboard still popped up
Any solution? Thanks
After hours and hours of research, I finally found a solution that works for all API versions. Hope this saves someone's time.
If you are developing for API >= 11, the solution is simple, either:
1) Add the two properties below in the xml file of EditText
android:inputType="none"
android:textIsSelectable="true"
or
2) Programatically do the below
myEditText.setInputType(InputType.TYPE_NULL);
myEditText.setTextIsSelectable(true);
And you're done.
If you want to cater for API < 11 as well, I found that there is no way to disable to keyboard from popping out if you wanted to select the text for copy paste purpose. Setting focusable to false will disable the keyboard but it doesn't help because it disables your ability to select text too. Any other solutions I found in stackoverflow all either doesn't work or disables text selection at the same time too.
One ugly way to solve this is as such..
First, add this property in the xml file of EditText
android:editable="false"
Yes this is deprecated, but necessary for making the EditText not editable in API version < 11.
Next, we will need to hide the keyboard as soon as it shows up, so that we can continue selecting text without the keyboard blocking the way.
Use this code below to detect keyboard showing up (solution obtained from https://stackoverflow.com/a/9108219/1241783), and hide it immediately.
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.HONEYCOMB)
{
final View activityRootView = findViewById(R.id.activityRoot);
activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
Rect r = new Rect();
//r will be populated with the coordinates of your view that area still visible.
activityRootView.getWindowVisibleDisplayFrame(r);
int heightDiff = activityRootView.getRootView().getHeight() - (r.bottom - r.top);
if (heightDiff > 100) { // if more than 100 pixels, its probably a keyboard...
//Hide the keyboard instantly!
if (getCurrentFocus() != null)
{
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
}
}
}
});
}
It works for my case. Though you can see the keyboard showing up in a split second (which is the ugly part) but I can't think of any other way to get this to work at the time of writing. If you have a better solution, please leave a comment!
Let me know too if this saves someone's time :)
To disable the soft keyboard showing, keeping the copy/paste and cursor functionality, just add this line in your activity:
getWindow().setFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM,
WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
Since the current top answer uses a deprecated method and didn't have the paste method for me, here's another way that doesn't use old methods. But, it does try to use a hidden method via reflection with a fallback. =)
I've subclassed EditText into a new widget called KeyboardlessEditText that still retains all the cool editing features without the keyboard showing. Just drop the file in and go.
The full code is a little long for this post, but as long as GitHub doesn't go down, then this will work: https://github.com/danialgoodwin/android-widget-keyboardless-edittext/blob/master/KeyboardlessEditText2.java
To disable system keyboard automatic pop up for EditText or TextView do the following:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
editTextView.setShowSoftInputOnFocus(false);
} else {
editTextView.setTextIsSelectable(true);
//N.B. Accepting the case when non editable text will be selectable
}
I had the same problem but later I also wanted allow typing after double tap.. after hours and hours of searching I found working solution (at least for me). Use this in your onCreate method:
editText.setCursorVisible(false);
editText.setTextIsSelectable(true);
editText.setShowSoftInputOnFocus(false);
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN); // This just hide keyboard when activity starts
These lines should definitely do the trick.. and if you want to revert that use this:
editText.setCursorVisible(true);
editText.setShowSoftInputOnFocus(true);
To show keyboard again use:
private void showSoftKeyboard(View view) {
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
view.requestFocus();
inputMethodManager.showSoftInput(view, 0);
}
To allow copy/paste next time just use these three lines:
editText.setCursorVisible(false);
editText.setTextIsSelectable(true);
editText.setShowSoftInputOnFocus(false);
For further keyboard hide use:
private void hideSoftKeyboard() {
if(getCurrentFocus() != null) {
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
}
}
This code is working on API >= 21
try this
EditText et = ... // your EditText
et.setKeyListener(null) //makes the EditText non-editable so, it acts like a TextView.
No need to subclass. The main difference between this and making your EditText non-focusable, is that the EditText still has its own cursor - you can select text, etc. All it does is suppress the IME from popping up its own soft keyboard.
Had a similar need due to my custom inline "fake" input which was still visible as the os soft keypad was appearing after focus moved to an edit text.
Solution was to make the edit text hide soft input until the previous custom input widget had finished its edit lifecycle.
Used #Bruce's answer for inspiration, also saw a few related posts which I'll attach at end.
Solution I found worked was:
fun setInputType(inputType: Int) {
getEditText().setRawInputType(inputType)
if (inputType == InputType.TYPE_NULL) {
getEditText().setTextIsSelectable(true)
getEditText().isCursorVisible = true
}
}
had to use setRawInputType() instead as multiline text input was not respected when setting from InputType.TYPE_NULL back to InputType.TYPE_TEXT_FLAG_MULTI_LINE.
Seems there are users reporting issues relating to calling setInputType(InputType.TYPE_NULL). see:
https://issuetracker.google.com/issues/36907992
other useful related posts:
How to make EditText not editable through XML in Android?
EditText non editable

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