Hide the keyboard whenever I want in the app on Android - android

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);

Related

Hide soft keyboard showed by other application

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

how do you make it so the Android keyboard doesnt open up when the user opens up my app?

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

Android weird display behaviour with soft keyboard

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

How to open input Keyboard on load of activity

I have an EditText in my activity class. What I want is, whenever my activity starts it automatically opens the input keyboard along with EditText. So how that can be done? Please anyone suggest it to me, if possible with example
If you want to always show soft keyboard whenever your activity is started, the simplest way is to add this piece of code in your Android Manifest file:
<activity android:name=".YourActivity"
android:windowSoftInputMode="stateAlwaysVisible" />
This will just work without you doing anything else with your code.
add this in your manifest file this will do
<activity android:windowSoftInputMode="stateVisible|adjustResize" . . . >
here is another method:-
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);
set this flag in activity's onCreate() methodgetWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);

Can't get keyboard to load onCreate()

My aim is to get the keyboard to open as soon as the app is loaded. Using this code,
InputMethodManager inputMgr = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
inputMgr.toggleSoftInput(0, 0);`
on a button, I can get the keyboard to load when the button is pressed. However, when placed in the override onCreate() section, nothing happens.
Add the following line to the activity in AndroidManifest.xml
android:windowSoftInputMode="stateVisible|adjustPan"

Categories

Resources