May I know is it possible to invoke android softkeyboard or Softkeyboard sample provided in the Androidsdk sample by button click? As of now I could invoke the softkeyboard only on edit view control's focus.
As per the below suggestion I did the following modification, but still didnt worked:
here is my code.. i got error on using getContentView() so I used "v".
private OnClickListener mKeypadbtnListener = new OnClickListener() {
public void onClick(View v) {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); imm.showSoftInputFromInputMethod(v.getWindowToken(), 0);
}
You should try imm.showSoftInput(view, 0); instead of v.getwindowToken(). I think this will help you.
You can display the soft keyboard issuing the following:
InputMethodManager imm = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInputFromInputMethod(getContentView().getWindowToken(), 0);
Related
I am using Search Widget to implement search interface in my app in action bar. As soon as the activity starts the keyboard pops up even if I have not interacted with the search widget. So for that I added the attribute in the activity tag in manifest file windowSoftInputMode giving value either stateHidden or stateAlwaysHidden but this did not worked. So I tried to do it programmatically using the hideSoftInputFromWindow method but this also did not worked.
What does this causes to pop up the keyboard even after the attributes are set not to show and how to solve this issue? Please help!
Try to Use this functions to show OR hide the keyboard:
/**
* Hides the soft keyboard
*/
public void hideSoftKeyboard() {
if(getCurrentFocus()!=null) {
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),
0);
}
}
/**
* Shows the soft keyboard
*/
public void showSoftKeyboard(View view) {
InputMethodManager inputMethodManager = (InputMethodManager)
getSystemService(INPUT_METHOD_SERVICE);
view.requestFocus();
inputMethodManager.showSoftInput(view, 0);
}
Just set focus as false at first time when that screen loaded. By default when screen loaded, the focus goes to first edittext and Keyboard appears.set like this
view.setFocusable(false);
InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
and on clickListener of that view make that view as focused. by using
view.requestFocus();
You can try to set the focus on any other view of the activity like this:
view.requestFocus();
I spotted the problem and solved the issue. I was using android.widget.SearchView and changed it to android.support.v7.widget.SearchView, after which I didn't even need to use windowSoftInputMode in the manifest or the hideSoftInputFromWindow() method. Please note!
I have a edit text view and button right side to it. When I click the button edittext has to get focus and keyboard has to appear. I programmitically
tried following.
name_text = (EditText)findViewById(R.id.name_text);
name_edit = (ImageView)findViewById(R.id.name_edit_icon);
name_text.setFocusable(false);
name_edit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
name_text.setFocusable(true);
//name_text.setEnabled(true);
}
});
I tried all possiblities, none is worked for me. Please help me. Thanks.
Write this code inside the click event to TOGGLE the keyboard:
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
I am developing chat application. WhenI click Take picture button my keyboard appears and the new intent starts. When I take the picture and return to chat application, the keyboard is hidden. I want the keyboard to remain appeared.
Anyone else had this problem?
Thanks!
To show soft keyboard do this:
//Import this
import android.view.inputmethod.InputMethodManager;
//Create object
private InputMethodManager imm;
imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, InputMethodManager.HIDE_IMPLICIT_ONLY);
In my case, I need to open the keyboard within onActivityResult() after getting a picture from Gallery or taking a picture via camera.
To make it works, I have to put a delay like 500 ms. Otherwise, it will not open the keyboard.
Here is my code inside onActivityResult():
// Set original file name to description.
EditText etDescription = getView().findViewById(R.id.etAttachmentDescription);
etDescription.setText(fileName);
etDescription.selectAll();
etDescription.requestFocus();
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
// Open keyboard after 500ms
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
}
}, 500);
please use this method to hide your soft keyboard .call this method from onactivity result.
public static void hideSoftKeyboard(Activity context) {
InputMethodManager inputManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
if (inputManager != null)
inputManager.hideSoftInputFromWindow(context.getWindow().getDecorView().getApplicationWindowToken(), 0);
context.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
}
#Badrul's solution worked for me but I had to requestFocus() on the edit text view first, but it worked great.
mEditText.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, InputMethodManager.HIDE_IMPLICIT_ONLY);
showSoftInput() doesn't show the keyboard for me, but toggleSoftInput() does. I saw some other post that said to disable the hard keyboard when using the emulator, but I'm not using an emulator. I'm loading my APK on an actual device with no hard keyboard. Shouldn't both methods work? Why doesn't showSoftInput() work? I would like to explicitly associate the keyboard with a specific text field.
Doesn't work:
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
editText.setText("textchange"); //i see the text field update
imm.showSoftInput(editText, InputMethodManager.SHOW_FORCED);
Works:
InputMethodManager imm = (InputMethodManager) getDelegate().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
It seems that the keyboard is initially displayed but hidden by something else, because the following works (but is actually a dirty work-around):
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
editText.postDelayed(new Runnable()
{
#Override
public void run()
{
editText.requestFocus();
imm.showSoftInput(editText, 0);
}
}, 100);
And when looking at logcat I suspect the cause behind this message hides the keyboard initially shown:
Hide Clipboard dialog at Starting input: finished by someone else... !
Show Keyboard + focus and also if you want to Hide the keyboard
#Override
public void onResume () {
super.onResume();
inputSearch.setFocusableInTouchMode(true);
inputSearch.requestFocus();
// Show Keyboard
InputMethodManager imm = (InputMethodManager) getSherlockActivity().getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(inputSearch, InputMethodManager.SHOW_IMPLICIT);
}
P.S inputSearch = (EditText) getSherlockActivity().findViewById(R.id.inputSearch);
// Hide Keyboard
InputMethodManager imm = (InputMethodManager) getSherlockActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(inputSearch.getWindowToken(), 0);
ShowSoftInput works if the imm's target view is same with the editText.
You can check this by imm.isActive(editText) or editText.isInputMethodTarget().
ToggleSoftInput is just toggling the keyboard of the current target of imm.
Target view of imm is set after the focus has been changed by editText.requestFocus(). I think some post processes exist between these two tasks since one post runnable was not enough. I tested double post and it worked for me.
editText.requestFocus();
editText.post(new Runnable() {
#Override
public void run() {
editText.post(new Runnable() {
#Override
public void run() {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) {
imm.showSoftInput(editText, 0);
}
}
});
}
});
The precise answer to this question why does showSoftInput doesnt work and toggleSoftInput does?
Is that the View to which you are trying to display the Keyboard doesn't have the focus. So to solve this problem and to use the method showSoftInput you will have to call the following to methods on your view:
setFocusable(true);
setFocusableInTouchMode(true);
Calling the above methods will make sure that when you click on the View retains and captures the focus.
showSoftInput() does not work when device has a hard (slide-out) keyboard
Android show softkeyboard with showSoftInput is not working?
Try this:
public void showTheKeyboard(Context context, EditText editText){
InputMethodManager imm = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
}
If this doesn't work read the tutorial from here
public void hideKeyboard() {
myTextView.setFocusable(true);
myTextView.setFocusableInTouchMode(true);
imm.hideSoftInputFromWindow(myTextView.getWindowToken(), 0);
}
WORKS
public void hideKeyboard() {
imm.hideSoftInputFromWindow(myTextView.getWindowToken(), 0);
}
DOES NOT WORK
imm is dealt with earlier as I am using a Fragment so:
Declare imm in the Fragment
private InputMethodManager imm;
Then in the fragment add:
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
imm = (InputMethodManager)
getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
}
He says after 3 to 4 hours of research and failures !!
Thanks user_CC ! :-)
Phil
I know this post is pretty old, but for those who came for answers from this date and none of the above worked. The code below worked for me on an Activity when an Alert Dialog popped up.
InputMethodManager keyboard = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
to show keyboard:
keyboard.toggleSoftInput(editText.getPaintFlags(), 0);
to hide keyboard:
keyboard.hideSoftInputFromWindow(editText.getWindowToken(), 0);
In my view, I have a search EditText and I would like to trigger programmatically the behaviour of a click event on the field, i.e give focus to the text field AND display soft keyboard if necessary (if no hard keyboard available).
I tried field.requestFocus(). The field actually gets focus but soft keyboard is not displayed.
I tried field.performClick(). But that only calls the OnClickListener of the field.
Any idea ?
Good sir, try this:
edittext.setFocusableInTouchMode(true);
edittext.requestFocus();
I'm not sure, but this might be required on some phones (some of the older devices):
final InputMethodManager inputMethodManager = (InputMethodManager) context
.getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.showSoftInput(edittext, InputMethodManager.SHOW_IMPLICIT);
Here is the code that worked for me.
edittext.post(new Runnable() {
public void run() {
edittext.requestFocusFromTouch();
InputMethodManager lManager = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
lManager.showSoftInput(edittext, 0);
}
});
That's it! Enjoy ;)
The following code worked for me, after the other two answers didn't work for me:
#Override
public void onResume() {
super.onResume();
SingletonBus.INSTANCE.getBus().register(this);
//passwordInput.requestFocus(); <-- that doesn't work
passwordInput.postDelayed(new ShowKeyboard(), 300); //250 sometimes doesn't run if returning from LockScreen
}
Where ShowKeyboard is
private class ShowKeyboard implements Runnable {
#Override
public void run() {
passwordInput.setFocusableInTouchMode(true);
// passwordInput.requestFocusFromTouch();
passwordInput.requestFocus();
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
((InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE)).showSoftInput(passwordInput, 0);
}
}
After a successful input, I also make sure I hide the keyboard
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
((InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE))
.hideSoftInputFromWindow(getView().getWindowToken(), 0);
Technically, I just added 300 ms of delay before running the soft keyboard display request. Weird, right? Also changed requestFocus() to requestFocusFromTouch().
EDIT: Don't use requestFocusFromTouch() it gives a touch event to the launcher. Stick with requestFocus().
EDIT2: In Dialogs (DialogFragment), use the following
getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
instead of
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
In my case, I wanted to display the virtual keyboard with no reference to a specific textbox, so I used the first part of the accepted answer to focus :
edittext.setFocusableInTouchMode(true);
edittext.requestFocus();
Then I show the virtual keyboard :
InputMethodManager inputMethodManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
for kotlin, implement using extension function as follow
fun View.showSoftKeyboard() {
this.requestFocus()
val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.showSoftInput(this, InputMethodManager.SHOW_IMPLICIT)
}
field.post(new Runnable() {
#Override
public void run() {
field.requestFocus();
field.onKeyUp(KeyEvent.KEYCODE_DPAD_CENTER, new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DPAD_CENTER));
}
});
In my case just this solved all problems:
val inputMethodManager: InputMethodManager =
context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.showSoftInput(edittext, InputMethodManager.SHOW_IMPLICIT)
I have put it in RecyclerView Adapter, because I use data binding.
Also I haven't used edittext.setFocusableInTouchMode(true) because in my layout it is true by default.
And don't use this line: edittext.requestFocus(). When I removed it, it started to work :)
val mAlertDialog = mBuilder.show()
//Coloca o foo no txtDlgQuantidade e então chama o teclado
mDialogView.txtDlgQuantidade.isFocusable = true
mDialogView.txtDlgQuantidade.isFocusableInTouchMode = true
mDialogView.txtDlgQuantidade.requestFocus()
mAlertDialog.window?.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE or WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM)
mAlertDialog.window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE)