Dismissing a DialogFragment when the soft keyboard is hidden - android

I have a simple DialogFragment that contains an EditText. When the DialogFragment is created the soft keyboard is shown immediately and the EditText gains immediate focus by using:
mEditText.requestFocus();
getDialog().getWindow().setSoftInputMode(
LayoutParams.SOFT_INPUT_STATE_VISIBLE);
In fact, what I have is essentially like the example given in this blog:
http://android-developers.blogspot.co.uk/2012/05/using-dialogfragments.html
When the back button is pressed, I wish for the DialogFragment to be dismissed. What actually happens is that the first back button press causes the soft keyboard to be hidden. A further back press is required to dismiss the DialogFragment.
I was quite surprised that there doesn't seem to be a simple API solution for this (such as setting a flag) as I'd have thought it'd be a common requirement.
Having searched on SO the best option seems to be to detect when the soft keyboard has been hidden, and then call dismiss() on that event. Such possible solutions for detecting the soft keyboard is hidden are:
EditText with soft keyboard and "Back" button
How to check visibility of software keyboard in Android?
Before I go ahead and use one of the above solutions, is there any other means I should consider dismiss of the entire DialogFragment and soft keyboard with one hit of the back button?

Why not using a cancel button instead of exploiting the back one?

Related

How to disable back button behavior?

I need to create a pin entry. I want the numeric keypad to be visible always. The problem is, I cannot override hardware button behavior which dismisses the keyboard. OnBackButtonPressed isn't entered when the button is pressed for dismissing the keyboard. What can I do? ()
I think that the best option for you is use an specific keyboard.
Maybe you can use this Xamarin component https://components.xamarin.com/view/TestComponent

How Android handle Soft Keyboard Hide/Show in Dialog

Does the host activity of the dialog got restarted when the system show / hide the Soft Keyboard because of the focus change in dialog?
My application is Fragment-Driven. I have a lot of fragments going around. One of my fragment trigger an action to open a dialog. Inside that dialog, there is input field and when I tap on that input field, all the states of the UI on the host fragment (actually all the fragments, so must be affecting on Main Activity) got reset. In my activity, I have already put this.
android:configChanges="keyboardHidden|orientation|screenSize"
So, by right, even though the soft keyboard is being shown on Activity, it should not got restarted.
I don't think I need put any code because it is purely on the logic of how Android controls on soft keyboard shows / hide.
Edit: My question is NOT how to hide soft keyboard. My question is how to handle the LifeCycle restart when the soft keyboard appears.
The actual problem is, in my underlying activity, I am having ListView. Because of that, when the soft-keyboard appear, the system check on the view from the host activity and try to PAN those ListView.
Because of that, the getView ( .. ) of the Adapter of the ListView got called and the UI states insides the items in ListView got refreshed.
The activity or fragment does not got restarted when the soft-keyboard appear if you put
android:configChanges="keyboardHidden|orientation|screenSize"
in your Activity or Application.
For the time being, I solved this by putting
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING);
in my host activity. It will not resize / pan the UI of the Activity when the Soft Keyboard appear. But, it is ok for me. All of my UI insides Activity or Fragments are not requesting any input from User. All the inputs are requesting with separate Dialog UI.
The above code does NOT affect the resize / pan on the Dialog. But, if you wants to make sure, put this in your Dialog subclass.
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
I am NOT sure this is desirable way or not. But, it suits my situation and because this is my question, I feel I have an obligation to answer it when I find a solution.
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);
}

Set android focus on touch?

I have this issue: in my app, when user taps on EditText bar, keyboard pops up. After that, it is impossible to get rid of keyboard. When back button is pressed, whole application just turn off.
How can I make sure, that when user taps on some other object (not EditText), keyboard will be removed? Or at least, how to make it possible to hide keyboard by tapping back button?
Thanks.
in xml for EditText this will make keyboard dismiss when press enter on keyboard
android:imeOptions="actionDone"
You can hide the keyboard simple by overriding onBackPressed in your Activity and using the following code:
InputMethodManager inputMethodManager = (InputMethodManager)getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(anyView.getWindowToken(), 0);
Note that anyView can be any view that is attached to your current window.
You can see it working in my app called Magic Annotator. See method hideSoftKeyboard()

Custom keyboard in Android not handling back key correctly

I've created a soft keyboard inside a v4 support Fragment but I think I'm going about it incorrectly. The keyboard shows up OK, but I can't hide it (the back button closes the programme). Otherwise it works fine. I would like the hardware Back button to be able to close just the keyboard.
myKeyboard = new Keyboard(getActivity(), R.xml.my_keyboard);
myKeyboardView = (MyKeyboardView) getActivity().findViewById(R.id.my_keyboard_view);
myKeyboardView.setKeyboard(myKeyboard);
// this is a custom class to receive soft keyboard events
// just passes on the events to the Activity as normal
keyboardView.setOnKeyboardActionListener(new MyKeyboardListener(getActivity()));
To get the keyboard to show up, I use an OnClickListener on the target EditText with an animation and setting the Visibility to VISIBLE.
Should I be using this approach? I looked for a way to get the keyboard on the backstack but it's not obvious.

Dismiss Soft Keyboard after Hardware Search is performed

I have an Activity which performs searching within my app. I have this Activity set as the handler for the hardware search button. This all works great.
If a user navigates to my Activity, enters a search query and then clicks on the "Search" button then I dismiss the soft keyboard via:
InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(etQuery.getWindowToken(), 0);
etQuery is my EditText field.
However, if a user taps the hardware search button, then the search input overlap is shown, and the user is allowed to enter text, which they do, upon clicking "Go" my Activity gets the input and performs the search successfully. However, the soft keyboard is still visible.
Even though I do call the above keyboard dismissal code, it doesnt actually work. I assume because the window [token] that initiated the soft keyboard is NOT in fact the etQuery EditText - it was initiated by the Hardware Search facility. Thus asking it to close based on the EditTexts token has no effect.
At this point, I don't really care who opened the soft keyboard, I just want it to close.
How can I force the soft keyboard to close regardless of who opened it?
I solved this by adding the following attribute to the activity in the manifest:
android:windowSoftInputMode="stateHidden"

Categories

Resources