I know that to hide soft keyboard I need to use code like this:
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
My problem is that I starting the ACTION_SEND intent, and use Twitter app to handle it. I pass a message to tweet it. But if the user does not confirm the message, but clicks ActionBar back button, the Twitter activity is finished, and my app lication comes back to the front. But the soft Keyboard, called by Twitter does not hide. I have no idea how to get Twitter's WindowToken. Could anybody help me?
Another way is to do the same in AndroidManifest.xml file. You can annotate your activity with the following line:
android:windowSoftInputMode="stateAlwaysHidden"
which means your activity will always hide keyboard when receives focus.
I have found an unswer.
I had to add this code:
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
in the onResume() method
Related
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().
I've read several answers to similar question but none of them are satisfying. I want to open and close the keyboard whenever I want.
This is what I've found:
Set the following to your activity inside AndroidManifest.xml
android:windowSoftInputMode="stateHidden"
Or in onCreate of your activity's code:
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
is this the correct way to do it?
I don't understand why there isn't a simple way to open and close it.
InputMethodManager iMM = (InputMethodManager)this.getSystemService(Service.INPUT_METHOD_SERVICE);
Show it via: iMM.showSoftInput(ed, 0);
Hide it via: iMM.hideSoftInputFromWindow(ed.getWindowToken(), 0);
can you make it so when you open up the app the on screen keyboard wont open up untill you click on one of the editText's?
In order to hide the virtual keyboard, you can use the InputMethodManager like this (you can put it in the onCreate()method of your landing activity) :
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
Add the following code in onCreate method of your activity:
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
It did the trick for me :)
in AndroidManifest.xml set android:configChanges="keyboardHidden"
it is up to your requirement. For suppose if you want to hide the keyboard every time the user opens your activity, then you can add
android:windowSoftInputMode="stateAlwaysHidden" in your android manifest for your activity. If you want it dynamically then you can make change whenever event to close the keyboard occurs using
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
Use this as reference whenever you want
I click a button and launch an activity with a soft keyboard:
When I click on the Cancel button, it calls finish() to exit to the first page. Then when I launch the activity again i get this:
The layout with the buttons is now hidden behind the keyboard.
In another scenario if i do it this way:
Launch activity.
Click back button to dismiss keyboard.
Click back button to go to first page.
Launch activity.
I get picture 1. The buttons don't get hidden. Seems like I have to destroy the keyboard before calling finish().
How do I solve this problem?
Edit: Added example of what's in the Manifest
<activity
android:name=".SignUp"
android:theme="#style/DefaultTitle" />
This is in my manifest as well, I added it after reading some other posts, didn't work for me.
<uses-sdk
android:minSdkVersion="4"
android:targetSdkVersion="7" />
Ok LOL. Weird discovery. If I do this:
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(cancel.getApplicationWindowToken(), 0);
try {
Thread.sleep(100);
} catch (Exception e) {
}
finish();
it works! Not sure if this is a good workaround....
I think you are showing the SoftKeyBoard Using STATE_ALWAYS_VISIBLE Thats Why it remain visible when you comes back to the activity.
So Try to open keyboard on Button Click this way..
EditText editText = (EditText) findViewById(R.id.myEdit);
InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
// only will trigger it if no physical keyboard is open
mgr.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
And if there are any windowSoftInputMode in Manifest,Remove them cause will not need them for above method.
Put this line in your activity tag and let me know what happen,
<activity android:windowSoftInputMode="stateVisible" . . . >
For more info look at android:windowSoftInputMode
Try using setfocus method on object of edittext
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.