This question already has answers here:
Android: show soft keyboard automatically when focus is on an EditText
(33 answers)
Closed 8 years ago.
In my activity there is three editText view it will randomly gets hide at some time
After entering a single text in the editText the soft-keyboard will disappears automatically
Now I want to show the soft keyboard When editText get focused
How to do this?
Thanks in advance
Through InputMethodManager you can show and hide the soft keyboard. Use toggleSoftInput() method to show soft keyboard when EditText get focus and use hideSoftInputFromWindow() to hide the keyboard when EditText lost focus as below...
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean isFocused) {
if (isFocused) {
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
} else {
imm.hideSoftInputFromWindow(getWindow().getCurrentFocus().getWindowToken(), 0);
}
}
});
Try with these code..
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if (inputMethodManager != null) {
inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
}
Related
I am using a FrameLayout to show an EditText and a ListView (with checkboxes) alternately. When showing an EditText, I would like the soft keyboard to be shown. And when showing the ListView, I would like the soft keyboard to be hidden. Now usually a focus is needed in order to hide the soft keyboard. When my ListView gets shown, then getCurrentFocus() returns null. Is there a way to hide the soft keyboard, without having a focus?
I am showing the soft keyboard like that:
public static void requestFocusAndMoveCursorToTheEndAndShowKeyboard(final EditText editTextParam, final Activity activityParam) {
if (editTextParam == null) {
return;
}
if (editTextParam.requestFocus()) {
editTextParam.setSelection(editTextParam.getText().length()); // move Cursor to the end of the EditText
InputMethodManager imm = (InputMethodManager) activityParam.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
}
}
And I am trying to hide the soft keyboard like that:
public static void hideSoftInputKeyboardFromWindow(Activity activityParam) {
if (activityParam == null) {
return;
}
View view = activityParam.getCurrentFocus();
if (view != null) {
InputMethodManager inputMethodManager = (InputMethodManager) activityParam.getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
}
Make use of .clearFocus(); on edittext when you don't want a focus on it.
In your AndroidMenifest.xml add this:
<activity android:name="com.your.package.ActivityName"
android:windowSoftInputMode="stateHidden" />
Try this:
Write following method to youractivity or to your utility class
/**
* Hide soft keypad
*
*/
public static void hideKeyboard(Activity activity, View v) {
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
In your hideSoftInputKeyboardFromWindow method, try:
inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
instead of
inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
Edit: ok same answer as Dorami
How do you show them alternately? Using different fragments? Or do you simply inflate different layouts? Provide more details with your complete code
Thank you for your answers. Finally I got it solved using a View.OnFocusChangeListener for the EditText, like described here:
Hide soft keyboard on losing focus
I am working on an application where I am creating layout dynamically.
I'm stuck with the problem that I want to hide the keyboard when there is a RadioButton. I have 5 consecutive edit text boxes (Generated Dynamically). After that the RadioButton part starts. I Want to hide the keyboard when there are radio Buttons and show it again when edit text appears.
I tried this:
InputMethodManager imm = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
But no luck.
if you call this method in an 'onClick()' it will forcibly close the keyboard.
private void hideSoftKeyboard(){
if(getCurrentFocus()!=null && getCurrentFocus() instanceof EditText){
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(yourEditTextHere.getWindowToken(), 0);
}
}
This question already has answers here:
How to show soft-keyboard when edittext is focused
(48 answers)
Closed 9 years ago.
I have an EditText that I am passing focus to programmatically. But when I do, I want the keyboard to show up as well (and then go down when that EditText lose focus). Right now, the user has to click on the EditText to get the keyboard to show up -- even thought the EditText already has focus.
<activity android:name=".YourActivity"
android:windowSoftInputMode="stateVisible" />
Add this to manifest file...
This is how I show the ketyboard:
EditText yourEditText= (EditText) findViewById(R.id.yourEditText);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(yourEditText, InputMethodManager.SHOW_IMPLICIT);
set this for your activity in your manifest to pop keyboard automatically when your screen comes containing EditText box
<activity android:windowSoftInputMode="stateAlwaysVisible" ... />
To hide keyboard on losing focus set a OnFocusChangeListener for the EditText .
In the onCreate()
EditText editText = (EditText) findViewById(R.id.textbox);
OnFocusChangeListener ofcListener = new MyFocusChangeListener();
editText.setOnFocusChangeListener(ofcListener);
Add this class
private class MyFocusChangeListener implements OnFocusChangeListener {
public void onFocusChange(View v, boolean hasFocus){
if(v.getId() == R.id.textbox && !hasFocus) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
}
}
To show the keyboard, use the following code.
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(et, InputMethodManager.SHOW_IMPLICIT);
To hide the keyboard,, use the code below.
et is the reference to the EditText
InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(getActivity().INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(et.getWindowToken(), 0);
In order to do it based on focus listener you should go for:
final InputMethodManager imm =(InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
editText.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus){
imm.showSoftInput(et, InputMethodManager.SHOW_IMPLICIT);
}else{
imm.hideSoftInputFromWindow(et.getWindowToken(), 0);
}
imm.toggleSoftInput(0, 0);
}
});
Hope this helps.
Regards!
I have 2 edit text and a seekbar within an activity. I would like to prevent the soft keyboard from popping up when touching the seekbar even if an edit text has the focus. I also tried to force the seekbar focus but it did not work either.
I have tried to hide the soft keyboard on the three SeekBar methods (onProgressChanged, onStopTrackingTouch and onStartTrackingTouch), and also one by one but this does not work.
Is it possible and how to achieve that ?
Thx
private void hideSoftKeyboard(){
// HIDE SOFTKEYBOARD
InputMethodManager imm = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
}
/**
* SEEKBAR Start tracking
*/
public void onStartTrackingTouch(SeekBar seekBar) {
// HIDE SOFTKEYBOARD
hideSoftKeyboard();
}
Oups sorry, I found the solution after many searches.. Here is it :
private void hideSoftKeyboard() {
// HIDE SOFTKEYBOARD
//InputMethodManager imm = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
// imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(mSeekBar.getWindowToken(), 0);
}
This question already has answers here:
How to close/hide the Android soft keyboard programmatically?
(126 answers)
Closed 9 years ago.
I tried to show keyboard after I inflate LinearLayout and call setContentView like:
InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.showSoftInput(etContent, InputMethodManager.SHOW_FORCED);
getContent.requestFocus();
It didn't work. I also tried this:
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
But it also didn't work. How can I force the keyboard to show/hide? What did I do wrong?
This should work
public class KeyBoard {
public static void toggle(Activity activity){
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
if (imm.isActive()){
imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0); // hide
} else {
imm.toggleSoftInput(0, InputMethodManager.HIDE_IMPLICIT_ONLY); // show
}
}//end method
}//end class
this link is clear about hiding the soft keyboard.
to show it you can use a hack - create an EditText anywhere in your layout, layout_width and layout_height=0dip, and in onCreate do
yourEditText.requestFocus();