SoftInputMode cannot be set to hidden - android

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..

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.

How to open a keyboard with lossing focus from parent

I have a problem and i not find answer in google.
I open a keyboard with button:
private void openKeyboard() {
keyboardButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
InputMethodManager imm = (InputMethodManager) getActivity()
.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 3);
}
});
}
But my layout going crap after keyboard is visible. How to open it with lost any parent from parent?
try with this snippet..
InputMethodManager inputMethodManager =
(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.toggleSoftInputFromWindow(
linearLayout.getApplicationWindowToken(),
InputMethodManager.SHOW_FORCED, 0);
or try this
In your manifest file, try adding the following to the <activity> that you want to show the keyboard when the activity starts:
android:windowSoftInputMode="stateVisible"or "stateAlwaysVisible"
or try this too..
youredittext.requestFocus();
youredittext.setFocusableInTouchMode(true);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(youredittext, InputMethodManager.SHOW_FORCED);

Controlling the Soft Keyboard during onClick - 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);

How to open Keyboard on Button click in android?

How can I open Keyboard on Button click in android?
What I want to be like this:
Please try this
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(view, flags)
an example may be:
InputMethodManager imm = (InputMethodManager) RouteMapActivity.this
.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(mapView, InputMethodManager.SHOW_IMPLICIT);
Write this code inside the Button click event to TOGGLE the keyboard:
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);

Categories

Resources