Keyboard not closing when dismiss the dialogFragment - android

I have a fragment which i use to show map. From this fragment I am opening another dialog fragment which have an editText. On clicking editText the keyboard opens but when I dismiss the dialogFragment without first closing the keyboard, the dialogFragment closes as it should but the keyboard remains open. and after again touching anywhere the keyboard closes. How do I close the keyboard on dismissing the dialogFragment.
I have already tried :
android:windowSoftInputMode="stateAlwaysHidden" in activity.
also tried :
InputMethodManager imm =
(InputMethodManager) messageEditTxt.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm.isActive())
imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
in onDismiss function.

Try this
public static void hideSoftKeyboard(Context context, View view) {
try {
InputMethodManager inputMethodManager =
(InputMethodManager) context.getSystemService(
Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(
view.getWindowToken(), 0);
} catch (Exception e) {
e.printStackTrace();
}
}
Usage
hideSoftKeyboard(getActivity(), getView())

I had the same problem and found that it is related to the windowSoftInput defined in the manifest. After removing android:windowSoftInputMode="stateHidden" from the activity, it worked.

Just place this in your global class and you can access anywhere
public static void hideKeyboard(Activity activity) {
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
//Find the currently focused view, so we can grab the correct window token from it.
View view = activity.getCurrentFocus();
//If no view currently has focus, create a new one, just so we can grab a window token from it
if (view == null) {
view = new View(activity);
}
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
for Activity-:
GlobalClass.hideSoftKeyborad(MainActivity.this);
for fragments-
GlobalClass.hideSoftKeyborad(getActivity);

Related

onClick hideKeyboard crashes that app when edittext is not selected

I'm trying to implement a feature where the virtual keyboard disappears from view when the user touches outside of it. It works, but when the edittext is not selected it causes the app to crash. I tried using a try/catch but that didn't help. From what I can tell, it's trying to close the keyboard when no keyboard is open. Any suggestions?
In the XML file:
android:onClick="hideKeyboard"
In the Java file:
public void hideKeyboard(View view) {
try {
InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
}
catch (Exception e) {
}
}
Replace you code
InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
with:
InputMethodManager imm=(InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
//Find the currently focused view, so we can grab the correct window token from it.
View view3=getCurrentFocus();
//If no view currently has focus, create a new one, just so we can grab a window token from it
if (view3==null){
view3=new View(this);
}
assert imm != null;
imm.hideSoftInputFromWindow(view3.getWindowToken(), 0);

Keyboard pops up after i dismiss DialogFragment

In my fragment, I call up a DialogFragment and then I call
getDialog().dismiss();
and have this in my onDismiss()
#Override
public void onDismiss(DialogInterface dialog)
{
InputMethodManager imm =
(InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm.isActive())
imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
super.onDismiss(dialog);
}
but for some reason after that, a keyboard seems to pop up when I go back to the fragment I've tried all sorts including trying to hide the keyboard in the callback on the fragment but nothing seems to be working.
In my fragment, I call up a DialogFragment and then I call
Try this:
public static void hideKeyboard(Activity activity) {
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
//Find the currently focused view, so we can grab the correct window token from it.
View view = activity.getCurrentFocus();
//If no view currently has focus, create a new one, just so we can grab a window token from it
if (view == null) {
view = new View(activity);
}
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
In your code
#Override
public void onDismiss(DialogInterface dialog)
{
super.onDismiss(dialog);
hideKeyboard(getActivity());
}
Use stateAlwaysHidden in Menifest file
<activity
android:screenOrientation="portrait"
android:name=".chat.activity.ChatActivity"
android:windowSoftInputMode="stateAlwaysHidden|adjustResize">
</activity>
Because you are using toggleSoftInput(int showFlags, int hideFlags) which toggles state of soft keyboard. from doc
This method toggles the input method window display.
You can use hideSoftInputFromWindow() which forces Android to hide the virtual keyboard using the InputMethodManager, calling hideSoftInputFromWindow, passing in the token of the window containing your focused view.
View view = this.getCurrentFocus();
if (view != null) {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
Try this block of code if you are in fragment
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWaYS_HIDDEN);
Or if you are in activity :
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWaYS_HIDDEN);
Please make sure to do it before setting the content view , or before inflating the view

Dismiss Keyboard on button click that close fragment

How do I close the keyboard on button click? I have a fragment which has an EditText and two buttons. One submits the EditText content, the other simply closes the fragment. Now when the fragment is gone, the keyboard stays. However, pressing the back button closes the keyboard or clicking on "done" also closes it. But what I need is the keyboard disappear when the fragment is closed.
I've tried solutions on similar questions here,here or here but none seems to work. Most of them throw a NullPointerException. All are for activities not fragments. The code for calling the keyboard works:
editText.requestFocus();
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
However I had to add getActivity() to make it work.
Any help will be appreciated.
Use this method
public void hideKeyboard() {
// Check if no view has focus:
View view = getActivity().getCurrentFocus();
if (view != null) {
InputMethodManager inputManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
}
for a fragment use the following function
public static void hideKeyboard(Activity activity) {
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
//Find the currently focused view, so we can grab the correct window token from it.
View view = activity.getCurrentFocus();
//If no view currently has focus, create a new one, just so we can grab a window token from it
if (view == null) {
view = new View(activity);
}
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
call it when the button is clicked
btn_cancel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
hideKeyboard(getActivity());
}
});
Try below method
public static void hideKeyboard(Context mContext) {
try {
View view = ((Activity) mContext).getWindow().getCurrentFocus();
if (view != null && view.getWindowToken() != null) {
IBinder binder = view.getWindowToken();
InputMethodManager imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(binder, 0);
}
} catch (NullPointerException e) {
e.printStackTrace();
}
}
In this method you have to pass context parameter. Hope it will help you out.
Evolving from previous answers and in Kotlin, using the calling view to obtain the window token.
button.setOnClickListener() { view ->
hideKeyboard(view)
}
private fun hideKeyboard(view: View) {
val inputMethodManager = view.context.getSystemService(Activity.INPUT_METHOD_SERVICE)
as InputMethodManager
inputMethodManager.hideSoftInputFromWindow(view.windowToken, 0)
}
After more thought, instead of having to add this call to every button, it may make more sense to clear the keyboard on loss of focus.
input.setOnFocusChangeListener { view, hasFocus ->
if(!hasFocus) {
hideKeyboard(view)
}
}
Try this simple code:
editText.onEditorAction(EditorInfo.IME_ACTION_DONE)

How to close keyboard programmatically which is not owned by the current window

I have this View Pager which has a set of pages. when the user is on the first page, the keyboard pops up. While i slide through the pages, the keyboard is not closed (that is how it is implemented). now When i am on the fourth or fifth page, i explicitly try to close the keyboard using the following piece of code but it does not work. Something tells me, that it is because the keyboard was opened on a separate page (by a different fragment).
InputMethodManager imm = (InputMethodManager)Context.GetSystemService(Activity.InputMethodService);
View v = ((Activity)context).CurrentFocus;
if (v == null)
return;
imm.HideSoftInputFromWindow(WindowToken, 0);
how is the windowtoken mapped here. I guess it is used to co-relate the view window which opened the keyboard. But don't all the pages in a pager display on the same window , essentially having the same token. If so, why doesn't it work
We can toggle input though. Here use this -
public static void toggle(Activity activity){
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
if (imm.isActive()){
// Hide keyboard
imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
} else {
// Show keyboard
imm.toggleSoftInput(0, InputMethodManager.HIDE_IMPLICIT_ONLY);
}
}
check this method.In my app,this is working fine in viewpager also.
public void hideKeyboard(Activity activity) {
// Check if no view has focus:
View view = activity.getCurrentFocus();
if (view != null) {
InputMethodManager inputManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
}
I hope its useful to you.
Implement by using the Interface method.
Create method in Interface class:
public interface ShowHideKeyboard(){
void showKeyBoard();
void hideKeyBoard();
}
Implement Interface class in your Activity:
public class YourActivity extends AppCompactActivity implements ShowHideKeyboard{
#Override
public void hideKeyBoard() {
View view = this.getActivity().getCurrentFocus();
if (view != null) {
view.clearFocus();
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}
#Override
public void showKeyboard() {
((InputMethodManager)
(getActivity())
.getSystemService(Context.INPUT_METHOD_SERVICE))
.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
}
}
Call showKeyBoard() and hideKeyBoard() method where you want to show or hide in fragment.
((ShowHideKeyBoard) getActivity()).showKeyBoard();
((ShowHideKeyBoard) getActivity()).hideKeyBoard();

Hiding soft keyboard without having focus

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

Categories

Resources