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);
}
}
Related
Thanks in advance for the help.
I would like a keyboard to appear at the end or during the execution of the following code.
// edit text
EditText editText = new EditText(activity);
editText.setBackgroundColor(Color.WHITE);
editText.setGravity(Gravity.TOP);
editText.setText("Type here...");
RelativeLayout relativeLayoutEditText = new RelativeLayout(activity);
RelativeLayout.LayoutParams paramRelativeLayoutEditText = new RelativeLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, 43 * display.getHeight() / 80 );
paramRelativeLayoutEditText.addRule(RelativeLayout.ALIGN_PARENT_TOP);
relativeLayoutEditText.addView(editText,paramRelativeLayoutEditText);
// add all created views to rootView
rootView.addView(relativeLayoutEditText, paramEditText);
// open keyboard
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
but the keyboard only appears after touching the editText field (ie with my finger). Is there a way that I can make the board automatically appear without using a physical touch?
As an aside I know that how I am specifying the width and height isn't exactly the right way to do things.
Thanks to #Shubham's link I was able to figure it out. The solution was not the answer given in the link, however. It was the second answer.
editText.requestFocus();
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
Edit:
once using the above solution the keyboard will remain on the screen until a user presses either the back button or the home button (sometimes it takes a few times). To remove the keyboard use this
imm.toggleSoftInputFromWindow(rootView.getWindowToken(), 0,0);
in my case rootView is the rootView of the current activity. I have not tested this to see if this will work on child views.
Add
editText.requestFocus();
Try this:
EditText editText = (EditText ) findViewById(R.id.myTextViewId);
editText.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
ensure that the edittext is focusable in touch mode. You can do it two way.
In xml:
android:focusableInTouchMode="true"
in Java:
editText.setFocusableInTouchMode(true);
Try This
EditText textView = (EditText ) findViewById(R.id.myTextViewId);
textView.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(textView, InputMethodManager.SHOW_IMPLICIT);
http://developer.android.com/reference/android/view/View.html#requestFocus()
Possible answer link. Many of the above are also sourced from this link.
Try This :
EditText textView = (EditText )findViewById(R.id.myTextViewId);
textView.requestFocus();
InputMethodManager imm =
(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(textView, InputMethodManager.SHOW_IMPLICIT);
this will show the keypad
InputMethodManager imm = (InputMethodManager)
OrderMainActivity.this.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(txtCustomerId, InputMethodManager.SHOW_IMPLICIT);
enter your activity name instead of this if you calling keypad from the listeners eg. button click
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
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);
}
In my application i am using tab activity and activity group. In one of the activity i have edit texts. When the user click on the edit text, soft keyboard appears. But when the user click on the back button the soft key board dismisses and the previous activity on the activity stack appears.
Earlier on other application when i press back button when there is soft keyboard on the screen, only soft keyboard dismiss and it wouldn't go back to previous activity unless i press the back button again. I want this to happen in my current application as well. Plz Help.
Use below in your On create Method. it is working fine.
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
can try this-
**
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
**
InputMethodManager imm = (InputMethodManager)
this.getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm.isAcceptingText()) {
System.out.println("Software Keyboard was shown");
} else {
System.out.println("Software Keyboard is hidden");
}
This does the trick. I checked if the soft key board is opened it stays at the current activity, otherwise it goes to previous activity.
code
InputMethodManager imm = (InputMethodManager)getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
check below link also :-
http://developer.android.com/guide/topics/resources/runtime-changes.html#HandlingTheChange
http://developer.android.com/reference/android/view/inputmethod/InputMethodManager.html
I think this will help you
protected void hideSoftKeyboard(EditText input) {
input.setInputType(0);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(input.getWindowToken(), 0);
}
My android Activity has an EditText. Whenever the screen loads the soft keyboard automatically pops up. This obscures the visibility of my screen. I want to display the soft keyboard only on the click event of that EditText.
How can I do this?
In my code I got something like this:
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(password.getWindowToken(), 0);
imm.hideSoftInputFromWindow(username.getWindowToken(), 0);
where password and username are TextEdit views.
And to show:
imm.showSoftInputFromWindow(username.getWindowToken(), 0);
Try with this..
EditText yourEditText= (EditText) findViewById(R.id.yourEditText);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(yourEditText, InputMethodManager.SHOW_IMPLICIT);
To Close u can use
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(yourEditText.getWindowToken(), 0);
This is how to show and/or how to close it.
So when you start the activity, use first the close code.