Show soft input keyboard - android

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

Related

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.

Android: Hide soft input keyboard

I need to hide the soft keyboard in response to clicking a button.
I saw some posts about this, and I tried it with:
InputMethodManager im = (InputMethodManager) getSystemService(
Context.INPUT_METHOD_SERVICE);
im.hideSoftInputFromWindow(myEditText1.getWindowToken(), 0);
That worked well. But now I have two EditText views. How can I now hide the soft keyboard, no matter wich EditText is selected? I tried it also with
InputMethodManager im = (InputMethodManager) getSystemService(
Context.INPUT_METHOD_SERVICE);
im.hideSoftInputFromWindow(myEditText1.getWindowToken(), 0);
im.hideSoftInputFromWindow(myEditText2.getWindowToken(), 0);
, but that didn't worked...
Thanks for your help!
EDIT:
Found solution. Posted below.
Simply you dont need to point specific view. Im using this and works :)
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
A solution is to not get the window token from the EditText, but from the buton wich hides the keyboard itselfs:
InputMethodManager im = (InputMethodManager) getSystemService(
Context.INPUT_METHOD_SERVICE);
im.hideSoftInputFromWindow(hideKeyboardButton.getWindowToken(), 0);
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
You can "play" with the parameter to achieve whatever you want.
Hope this helped!

softkeyboard not working when barcode scanner is connected to tablet

InputMethodManager imm = (InputMethodManager)getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
Using this code also soft keyboard is not appearing when barcode scanner is connected please help with this
I found solution and it worked for Nexus 4.4.2
if(getResources().getConfiguration().hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_NO){
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showInputMethodPicker();
}
You have to disable hardware keyboard on the popup message.
Android can only have one keyboard connected at a time.
The scanner counts as a keyboard.

Why wont the softkeyboard show here?

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?

How to hide keyboard for samsung galaxy note programmatically in android

I am using data and time picker for two edit text, I want to hide keyboard for two edit text.I am doing like this
mDatePickerEdt = (EditText)findViewById(R.id.createwedding_datepicker_edt);
mTimePickerEdt = (EditText)findViewById(R.id.createwedding_timepicker_edt);
mDatePickerEdt.setInputType(InputType.TYPE_NULL);
mTimePickerEdt.setInputType(InputType.TYPE_NULL);
it is working for smart phones.I getting problem in samsung galaxy note.If any one have idea.Please help, Thanks in advance.
you can use following code to hide keyboard.
getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
try this
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
or try input InputMethodManager.HIDE_NOT_ALWAYS instead of 0
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
you can use below code to suppress the keyboard until the user touched the edittext view.
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

Categories

Resources