In my Activity I have an AutoCompleteTextView with a dropdown list. When the user selects an item, a new Activity is started. Since I have a lot of stuff in the next Activity, there's a delay of about 0.5-1s before it starts. I'm trying to hide the soft keyboard immediately after an item is selected:
actvActionSearch.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapter, View v,
int position, long id) {
imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(actvActionSearch.getWindowToken(), 0);
//do stuff to prepare and start next Activity
}
});
However, the soft keyboard gets hidden appr. at the same time the next Activity starts. Where does this delay come from? Hiding the keyboard is the 1st thing I perform
Note how you are getting InputMethodManager as a system service?
That means your call to hideSoftInputFromWindow is performed on a system Service, which means its always running in the background along side of your app, which means when you hide keyboard, its actually running in parallel as your app which is performing the activity create.
Related
I have an app that uses the keyboard, when the keyboard is up if the user presses back button the keyboard dissappears, however I would like to change one or two other things while it changes, so I need an event listener.
I tried
#Override
public void onBackPressed()
However this doesnt seem to catch the backbutton while the keyboard is up, if the user presses back twice, then this catches the second click only
Register this on your root view to detect keyboard appearance/disappearance :
root.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener(){
public void onGlobalLayout(){
int heightDiff = root.getRootView().getHeight()- root.getHeight();
/*If height difference is more then 150, consider keyboard as visible. -150 for disappearing */
}
});
Also, make sure to use adjustResize for keyboard.
I have an logic like this: when enter the edit activity, i will show popup first, then show soft keyboard in the onDismissListener callback, but when i call showKeyboard directly in the callback, the soft keyboard doesn't show. Only i call the view.postRunnable, it will show as expected. As well the activity softkeyboard option is set android:windowSoftInputMode="stateHidden|adjustResize"
private void showKeyboard() {
InputMethodManager imm = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(mEditText, InputMethodManager.SHOW_IMPLICIT);
}
Unless someone with deep knowledge of the inner gears of the UI framework come by to answer, everyone's answer will be a "best guess". So below is my best guess:
That is most likely related to the Window and WindowManager and how they interact with Views and keyboards.
The EditText being passed to the method have a token to your activity window, and here is my guess:
If the window is not in foreground, it can't show a keyboard. So when you post the method call, then that method gets executed after the Window from the Popup is gone, and the Window from the activity is in Foreground.
The onDismiss() callback is most likely called from non-UI thread, so you can't update your UI directly. That's why you need to post it to the UI thread with view.postRunnable().
With a bluetooth keyboard connected, the Note 2 forces the switch back to the Samsung keyboard. This is a problem, as the Samsung keyboard always pops up when I'm trying to type with my external, taking up screen space and defeating half the purpose. I could write an app that forces the switch back, probably, but I'd still have to hit Enter/OK everytime the keyboard connects. I disabled the Samsung keyboard altogether, but the OK button still shows up. Is there some sort of service that does this or something? Because if I can't block/disable whatever it is, it's going to mess with my workflow, seeing as I bought this K810 to switch between multiple devices.
Here's a code snippet I use in order to prevent the keyboard to popup at application start:
public void hideSoftKeyboard(View v) {
Activity activity = (Activity) v.getContext();
InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
}
I think that if you can detenct the event with:
http://developer.android.com/reference/android/content/res/Configuration.html#keyboard
There are some flags, which indicate what's the inserted keyboard.
Or just try this one, even though i didn't test it:
public boolean onEvaluateInputViewShown() {
return false;
}
Let me know then of the results.
Thanks
I navigate from Activity1 to Activity2
On Activity 2 I have a keyboard and this keyboard stays on the screen after selecting the back button and going to Activity 1.
This is how I fixed this issue
// This code is in Activity 2
#Override
public void onBackPressed() {
startActivity(intentForActivity1);
finish();
}
Is this a wrong solution to my problem?
Is it a bad idea to handle the back button manually?
Since you're capturing the back button press, most probably the soft keyboard does not receive the press and thus it does not hide.
Try hiding it yourself:
#Override
public void onBackPressed() {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
startActivity(intentForActivity1);
finish();
}
See Reto Meier's answer for more details on this method to hide the keyboard: Close/hide the Android Soft Keyboard
There's nothing inherently wrong with overriding the back button. Just make sure the behavior isn't confusing to the user.
Also, if you ever just want to hide the soft keyboard (say, you're switching between tabs or something like that), you can use InputMethodManager. Here's a thread where people discussed ways to do this.
I am trying to define a set of buttons that allow the user to enter data into an EditText box. I want all the default functionality of an EditText box except for the pop up of the soft keyboard. the only data to be allowed to enter into the EditText box should be the data from the buttons I have defined. I am trying to suppress the soft keyboard by catching the touch event and returning true. (per the conversation found on this thread)
private OnTouchListener txtTouchListener = new OnTouchListener()
{
#Override
public boolean onTouch(View v, MotionEvent event)
{
//Return true to suppress the passing of the event on to the OS
return true;
}
};
The problem is that this method then blocks the long click event from firing. To solve this I can return false and then handle the long click event. However, this then makes the short click bring up the soft keyboard. Also, upon long click not only is the soft keyboard suppressed, but so is the context menu. I am looking for a way to stop the keyboard from appearing on a short (or long) click but keep all other functionality (updating the cursor position on short click, on long click show the EditText context menu, etc.)
Any ideas on this is greatly appreciated!
This thread suggests:
EditText.setInputType(InputType.TYPE_NULL);
Or you set the inputType in the XML Attribute to none.
Not tried it myself though.