force soft keyboard to show when EditText gets focus
This is how I show the ketyboard:
EditText yourEditText= (EditText) findViewById(R.id.yourEditText);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(yourEditText, InputMethodManager.SHOW_IMPLICIT);
Android:How do I force the soft keyboard to close when it has been forced to open?
InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.hideSoftInputFromWindow(Your Button.getWindowToken(), 0);
Open soft keyboard programmatically
I have used the following lines to display the soft keyboard manually inside the onclick event, and the keyboard is visible.
InputMethodManager inputMethodManager =
(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.toggleSoftInputFromWindow(
linearLayout.getApplicationWindowToken(),
InputMethodManager.SHOW_FORCED, 0);
But I'm still not able to open this while the activity gets opened, so are there any solution for this?
Android: How do I prevent the soft keyboard from pushing my view up?
You can simply switch your Activity's windowSoftInputMode
flag to adjustPan
in your AndroidMainfest.xml
file inside your activity tag.
Check the official documentation for more info.
<activity
...
android:windowSoftInputMode="adjustPan">
</activity>
If your container is not changing size, then you likely have the height set to "match parent". If possible, set the parent to "Wrap Content", or a constraint layout with constraingts to top and bottom of parent.
The parent container will shrink to fit the available space, so it is likely that your content should be inside of a scolling view to prevent (depending on the phone manufacturer and the layout choosen...)
- Content being smashed together
- Content hanging off the screen
- Content being inacccessable due to it being underneath the keyboard
even if the layout it is in is a relative or constraint layout, the content could exhibit problems 1-3.
Android: force keyboard to appear and focus on EditText
Thanks to @Shubham's link I was able to figure it out. The solution was not the answer given in the link, however. It was the second answer.
editText.requestFocus();
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
Edit:
once using the above solution the keyboard will remain on the screen until a user presses either the back button or the home button (sometimes it takes a few times). To remove the keyboard use this
imm.toggleSoftInputFromWindow(rootView.getWindowToken(), 0,0);
in my case rootView is the rootView of the current activity. I have not tested this to see if this will work on child views.
Related Topics
Restoring State of Textview After Screen Rotation
Android Studio Not Deploying Changes to App
Using Seekbar to Control Volume in Android
Android: Scrollview Force to Bottom
How to Programmatically Force Bluetooth Low Energy Service Discovery on Android Without Using Cache
What Is Android Keystore File, and What Is It Used For
Out of Memory Error Imageview Issue
Popupwindow - Dismiss When Clicked Outside
Android - Save/Restore Fragment State
What Is the Class R in Android
How to Determine If One of My Activities Is in the Foreground
Proguard Hell - Can't Find Referenced Class
Compile with Proguard Gives Simexception: "Local Variable Type Mismatch"
Use Camera Flashlight in Android
Android Webview "Tel:" Links Show Web Page Not Found
How to Change a Layer-List Drawable
Android: Http Communication Should Use "Accept-Encoding: Gzip"