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.
Related
I want to show softkeyboard after tapping on EditText in my custom Softkeyboard.
I have made one custom keyboard in which I've added one key.
This key will navigate you to a Conversion View which contains an EditText.
So after tapping on that EditText I want to open my own custom softkeyboard.
Please reply me as soon as possible.
You have two options: create a soft keyboard using the InputMethodService--that is, create a true soft keyboard that will be listed under the keyboard listing in the device's settings that the user can turn on and use at any time. That can be a lot of work.
You're other option is to mock the soft keyboard by creating a custom view, of which you handle input and toggle the visibility of explicitly in your app; blocking the native soft keyboard from showing (which you can do per activity via your manifest or in code).
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);
}
I have an app that contains at some point a FragmentActivity. This FragmentActivity contains some Fragments (FragmentA, FragmentB, etc). On each of these fragments, there are one or more EditText.
I'm trying to handle the keyboard properly. I want to make the Keyboard appear or disappear whenever I want.
For Example, When the FragmentA is created, I want to open the keyboard on an EditText. Then, three options possible for the user :
He clicks on a button that goes to the next fragment
He clicks on the Action Send of the Keyboard(that does nothing (on purpose) but close the keyboard normally) and then he can click on the button from the view
He clicks on the back button of the phone. In this case, it closes the keyboard and then he can click on the button form the view.
My problem is, whenever the user closes the keyboard by clicking on the back button of the phone, Android thinks that he doesn't want to see the keyboard ever in the activity. So when the FragmentB is created, I can't programmatically show the keyboard (Using InputManager btw) on a EditText from this Fragment.
Then, a second problem is when i click on the edittext to get the focus, in Android 4.x, the keyboard shows again, no problems, but with Android 2.x, it's impossible to have the keyboard shown again even if the focus is on the edittext ! It's killing me.
It appears that once the user has explicitly close the keyboard with the back button in one activity (even a fragment activity), you cannot show it again.
Does anyone have a solution ? Maybe playing with the flags in InputManager ? I didn't get all of them and what they do.
Try this:
Show keyboard:
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
EditText view = getCurrentFocus();
inputMethodManager.showSoftInput(view, InputMethodManager.SHOW_FORCED);
Hide keyboard:
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
Hope it helps ;)
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?
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()