Controlling the Soft Keyboard during onClick - Android - android

I had an issue where if user was typing and hit submit, the keyboard would not go away, so I found this code to fix that issue (by placing this in the onClick method):
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
However, if the user manually closed the keyboard, then clicked submit, I found the code above would bring the keyboard back -- not good.
The question:
Is there better code to use? OR can I just say something like =
if (keyboard = displayed) {
// do code above
} else {
// do nothing
}

To hide the keyboard:
final InputMethodManager inputMethodManager =
(InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
To show it:
final InputMethodManager inputMethodManager =
(InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.showSoftInput(view, InputMethodManager.SHOW_FORCED);

Related

How to always show soft keyboard and do not let it be closed?

I want to know 2 things
how to always show soft keyboard and do not let it be closed ( even when back or OK button was pressed ) ?
and how can I get an input from it?
I have already tried this code:
EditText yourEditText= (EditText) findViewById(R.id.ed);
InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
with these variants:
imm.showSoftInput(yourEditText, InputMethodManager.SHOW_IMPLICIT);
imm.toggleSoftInputFromWindow( yourEditText.getApplicationWindowToken(), InputMethodManager.SHOW_FORCED, 0);
imm.showSoftInput(yourEditText, InputMethodManager.SHOW_IMPLICIT);
Use android:windowSoftInputMode="stateAlwaysVisible" in to AndroidManifest.xml file
Like this:
<activity android:name=".YourActivity"
android:label="#string/app_name"
android:windowSoftInputMode="stateAlwaysVisible" /> // OR stateVisible
If that activity having EditText so when ever Activity will start
your Keyboard automatically open
If you want to still open Keyboad after use done any operation then do this via programetically
InputMethodManager imm =
(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInputFromWindow(
linearLayout.getApplicationWindowToken(),
InputMethodManager.SHOW_FORCED, 0);
OR
To Show Soft Keyboard
InputMethodManager imm = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(EDITABLE_VIEW,
InputMethodManager.SHOW_IMPLICIT);
OR
EDITABLE_VIEW can be any view which has focus on screen like
mEditText = (EditText) findViewById(R.id.editText);
InputMethodManager imm = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(mEditText ,
InputMethodManager.SHOW_IMPLICIT);
OR
((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE)).showSoftInputFromInputMethod(editText.getWindowToken(), 0);
Documentation

Keyboard auto on in Android

I want to implement a tablet in Android app that keyboard auto on the screen. Now my situation is I add a edit-text in a xml file, and when clicking the edit-text, keyboard appears, I want to show keyboard auto on.
When users click on the check mark, the keyboard disappears, but I want that keyboard will not be dismissed and clicking on check mark means checking the input.
Any samples or help is appreciated!
Use below method with activity
//To show the keyboard
public void showKeyboard(Activity activity) {
if(activity.getCurrentFocus()!=null) {
InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(INPUT_METHOD_SERVICE);
inputMethodManager.showSoftInputFromInputMethod(activity.getCurrentFocus().getWindowToken(), 0);
}
}
//To hide the keyboard
public void hideKeyboard(Activity activity) {
if(activity.getCurrentFocus()!=null) {
InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
}
}
OR
Use below method with EditText
//To show the keyboard
public void showSoftKeyboard(EditText editText) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
}
//To hide the keyboard
`public void hideSoftKeyboard(EditText editText) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
}
Use above method as per your requirement
This will work and also always try to use etText.requestfocus(); it will automatically open a keyboard
and
public static void ShowKeyboard(Activity activity, View v)
{
InputMethodManager inputMethodManager =
(InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.toggleSoftInputFromWindow(
view.getApplicationWindowToken(),
InputMethodManager.SHOW_FORCED, 0);
}
Check with this:
Add this on your checkbox click
To Show Soft Keyboard:
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(EDITABLE_VIEW,InputMethodManager.SHOW_IMPLICIT);
OR
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
To Hide Soft Keyboard:
InputMethodManager imm = InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(FOCUSABLE_VIEW.getWindowToken(), 0);
Here, “FOCUSABLE_VIEW” can be any view which is visible on screen like
Refer:http://chintanrathod.com/show-hide-soft-keyboard-programmatically-in-android/
Below are the methods to show and hide keyboard. Have a look.
public static void ShowKeyboard(Activity activity, View view) {
InputMethodManager inputMethodManager =
(InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.toggleSoftInputFromWindow(
view.getApplicationWindowToken(),
InputMethodManager.SHOW_FORCED, 0);
}
public static void HideKeyBoard(Activity activity) {
try {
InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
if (inputMethodManager.isAcceptingText())
inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
} catch (Exception e) {
e.printStackTrace();
}
}
If you want to open keyboard on activity start, you can add following code in <activity> tag in manifest:
android:windowSoftInputMode="stateVisible"
this will show the keyboard whenever you start activity.
You can use the following code,
editext.requestfocus();
this will automatically brings the keyboard.
Do not use,
android:windowSoftInputMode="stateHidden"
inside your android manifest file...
just remove "android:windowSoftInputMode" from your Activity.

On Button Click Hide Keyboard

I have a meme creator application, I have two text fields and a button, I want when the button is pressed to hide the keyboard, is this possible?
public void dismissKeyboard(Activity activity) {
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
if (null != activity.getCurrentFocus())
imm.hideSoftInputFromWindow(activity.getCurrentFocus()
.getApplicationWindowToken(), 0);
}
Activity have to be passed to this method, Keyboard will get dismissed.
You can hide sof keyboard with this lines
InputMethodManager inputManager = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
Put this in the onClick(View view) event.
You need to import android.view.inputmethod.InputMethodManager;
The keyboard will hides when you click the button.
What you want should already be happening. When you click the button, the focus changes from the text field to the button, so the keyboard will automatically hide.
EditText editText = (EditText)findViewById(R.id.textBox);
InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(editText.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
this should be in button click event
Android Kotlin
On button click hide the keyboard in kotlin
fun dismissKeyboard(activity: Activity) {
val imm = activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
if (null != activity.currentFocus) imm.hideSoftInputFromWindow(
activity.currentFocus!!.applicationWindowToken, 0
)
}
And use in your class like this
button.setOnClickListener {
dismissKeyboard(this)
}

SoftInputMode cannot be set to hidden

I created an Activity with a Dialog theme and it has an EditText.
I don't want to show soft input on every activity so I used the following code on that activity:
EditText editTextData=GetView(R.id.txtData);
editTextPin.requestFocus();
InputMethodManager mgr = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
mgr.showSoftInput(editTextData, InputMethodManager.SHOW_IMPLICIT);
This makes the input keyboard visible.
After the input, I tried to make it hidden with this code:
InputMethodManager mgr = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
mgr.hideSoftInputFromInputMethod(editTextData.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
It remains visible. What else should I call?
try ..
mgr.hideSoftInputFromInputMethod(editTextData.getWindowToken(),
1);
or
try
InputMethodManager mgr = (InputMethodManager)getSystemService(
Context.INPUT_METHOD_SERVICE); mgr.hideSoftInputFromWindow(editTextData.getWindowToken(), 0);
You can use below function to hide the keyboard..
private void hideSoftKeyboard(Activity act) {
InputMethodManager inputMethodManager = (InputMethodManager) act.getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(act.getCurrentFocus().getWindowToken(), 0);
}
Hope it will help you..

How to display the soft keyboard only on the click event of edittext in android?

My android Activity has an EditText. Whenever the screen loads the soft keyboard automatically pops up. This obscures the visibility of my screen. I want to display the soft keyboard only on the click event of that EditText.
How can I do this?
In my code I got something like this:
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(password.getWindowToken(), 0);
imm.hideSoftInputFromWindow(username.getWindowToken(), 0);
where password and username are TextEdit views.
And to show:
imm.showSoftInputFromWindow(username.getWindowToken(), 0);
Try with this..
EditText yourEditText= (EditText) findViewById(R.id.yourEditText);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(yourEditText, InputMethodManager.SHOW_IMPLICIT);
To Close u can use
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(yourEditText.getWindowToken(), 0);
This is how to show and/or how to close it.
So when you start the activity, use first the close code.

Categories

Resources