Forcing the default Android soft-keyboard to show - android

I have an activity (forced into protrait mode) that is required to show the default soft-keyboard. So I thought I'd set focus to the edittext when the activity is launched... so far so good.
Two issues
the soft-keyboard shall not be
dismiss-able for this activity
the soft-keyboard needs to be
elevated 50 pixels from the bottom
of the screen
Looking for assistance to solve these two issues. thanks.

to solve #1
private void hideSoftKeyboard()
{
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(mAutoCompleteTextView.getApplicationWindowToken(), 0);
}
private void showSoftKeyboard()
{
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,InputMethodManager.HIDE_IMPLICIT_ONLY);
}
#2 has no solution

Related

Not able to hide keyboard while using Search Widget

I am using Search Widget to implement search interface in my app in action bar. As soon as the activity starts the keyboard pops up even if I have not interacted with the search widget. So for that I added the attribute in the activity tag in manifest file windowSoftInputMode giving value either stateHidden or stateAlwaysHidden but this did not worked. So I tried to do it programmatically using the hideSoftInputFromWindow method but this also did not worked.
What does this causes to pop up the keyboard even after the attributes are set not to show and how to solve this issue? Please help!
Try to Use this functions to show OR hide the keyboard:
/**
* Hides the soft keyboard
*/
public void hideSoftKeyboard() {
if(getCurrentFocus()!=null) {
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),
0);
}
}
/**
* Shows the soft keyboard
*/
public void showSoftKeyboard(View view) {
InputMethodManager inputMethodManager = (InputMethodManager)
getSystemService(INPUT_METHOD_SERVICE);
view.requestFocus();
inputMethodManager.showSoftInput(view, 0);
}
Just set focus as false at first time when that screen loaded. By default when screen loaded, the focus goes to first edittext and Keyboard appears.set like this
view.setFocusable(false);
InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
and on clickListener of that view make that view as focused. by using
view.requestFocus();
You can try to set the focus on any other view of the activity like this:
view.requestFocus();
I spotted the problem and solved the issue. I was using android.widget.SearchView and changed it to android.support.v7.widget.SearchView, after which I didn't even need to use windowSoftInputMode in the manifest or the hideSoftInputFromWindow() method. Please note!

Hiding soft keyboard without having focus

I am using a FrameLayout to show an EditText and a ListView (with checkboxes) alternately. When showing an EditText, I would like the soft keyboard to be shown. And when showing the ListView, I would like the soft keyboard to be hidden. Now usually a focus is needed in order to hide the soft keyboard. When my ListView gets shown, then getCurrentFocus() returns null. Is there a way to hide the soft keyboard, without having a focus?
I am showing the soft keyboard like that:
public static void requestFocusAndMoveCursorToTheEndAndShowKeyboard(final EditText editTextParam, final Activity activityParam) {
if (editTextParam == null) {
return;
}
if (editTextParam.requestFocus()) {
editTextParam.setSelection(editTextParam.getText().length()); // move Cursor to the end of the EditText
InputMethodManager imm = (InputMethodManager) activityParam.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
}
}
And I am trying to hide the soft keyboard like that:
public static void hideSoftInputKeyboardFromWindow(Activity activityParam) {
if (activityParam == null) {
return;
}
View view = activityParam.getCurrentFocus();
if (view != null) {
InputMethodManager inputMethodManager = (InputMethodManager) activityParam.getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
}
Make use of .clearFocus(); on edittext when you don't want a focus on it.
In your AndroidMenifest.xml add this:
<activity android:name="com.your.package.ActivityName"
android:windowSoftInputMode="stateHidden" />
Try this:
Write following method to youractivity or to your utility class
/**
* Hide soft keypad
*
*/
public static void hideKeyboard(Activity activity, View v) {
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
In your hideSoftInputKeyboardFromWindow method, try:
inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
instead of
inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
Edit: ok same answer as Dorami
How do you show them alternately? Using different fragments? Or do you simply inflate different layouts? Provide more details with your complete code
Thank you for your answers. Finally I got it solved using a View.OnFocusChangeListener for the EditText, like described here:
Hide soft keyboard on losing focus

How to hide only soft keyboard android

In my application i am using tab activity and activity group. In one of the activity i have edit texts. When the user click on the edit text, soft keyboard appears. But when the user click on the back button the soft key board dismisses and the previous activity on the activity stack appears.
Earlier on other application when i press back button when there is soft keyboard on the screen, only soft keyboard dismiss and it wouldn't go back to previous activity unless i press the back button again. I want this to happen in my current application as well. Plz Help.
Use below in your On create Method. it is working fine.
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
can try this-
**
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
**
InputMethodManager imm = (InputMethodManager)
this.getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm.isAcceptingText()) {
System.out.println("Software Keyboard was shown");
} else {
System.out.println("Software Keyboard is hidden");
}
This does the trick. I checked if the soft key board is opened it stays at the current activity, otherwise it goes to previous activity.
code
InputMethodManager imm = (InputMethodManager)getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
check below link also :-
http://developer.android.com/guide/topics/resources/runtime-changes.html#HandlingTheChange
http://developer.android.com/reference/android/view/inputmethod/InputMethodManager.html
I think this will help you
protected void hideSoftKeyboard(EditText input) {
input.setInputType(0);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(input.getWindowToken(), 0);
}

android softkeyboard showSoftInput vs toggleSoftInput

showSoftInput() doesn't show the keyboard for me, but toggleSoftInput() does. I saw some other post that said to disable the hard keyboard when using the emulator, but I'm not using an emulator. I'm loading my APK on an actual device with no hard keyboard. Shouldn't both methods work? Why doesn't showSoftInput() work? I would like to explicitly associate the keyboard with a specific text field.
Doesn't work:
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
editText.setText("textchange"); //i see the text field update
imm.showSoftInput(editText, InputMethodManager.SHOW_FORCED);
Works:
InputMethodManager imm = (InputMethodManager) getDelegate().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
It seems that the keyboard is initially displayed but hidden by something else, because the following works (but is actually a dirty work-around):
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
editText.postDelayed(new Runnable()
{
#Override
public void run()
{
editText.requestFocus();
imm.showSoftInput(editText, 0);
}
}, 100);
And when looking at logcat I suspect the cause behind this message hides the keyboard initially shown:
Hide Clipboard dialog at Starting input: finished by someone else... !
Show Keyboard + focus and also if you want to Hide the keyboard
#Override
public void onResume () {
super.onResume();
inputSearch.setFocusableInTouchMode(true);
inputSearch.requestFocus();
// Show Keyboard
InputMethodManager imm = (InputMethodManager) getSherlockActivity().getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(inputSearch, InputMethodManager.SHOW_IMPLICIT);
}
P.S inputSearch = (EditText) getSherlockActivity().findViewById(R.id.inputSearch);
// Hide Keyboard
InputMethodManager imm = (InputMethodManager) getSherlockActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(inputSearch.getWindowToken(), 0);
ShowSoftInput works if the imm's target view is same with the editText.
You can check this by imm.isActive(editText) or editText.isInputMethodTarget().
ToggleSoftInput is just toggling the keyboard of the current target of imm.
Target view of imm is set after the focus has been changed by editText.requestFocus(). I think some post processes exist between these two tasks since one post runnable was not enough. I tested double post and it worked for me.
editText.requestFocus();
editText.post(new Runnable() {
#Override
public void run() {
editText.post(new Runnable() {
#Override
public void run() {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) {
imm.showSoftInput(editText, 0);
}
}
});
}
});
The precise answer to this question why does showSoftInput doesnt work and toggleSoftInput does?
Is that the View to which you are trying to display the Keyboard doesn't have the focus. So to solve this problem and to use the method showSoftInput you will have to call the following to methods on your view:
setFocusable(true);
setFocusableInTouchMode(true);
Calling the above methods will make sure that when you click on the View retains and captures the focus.
showSoftInput() does not work when device has a hard (slide-out) keyboard
Android show softkeyboard with showSoftInput is not working?
Try this:
public void showTheKeyboard(Context context, EditText editText){
InputMethodManager imm = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
}
If this doesn't work read the tutorial from here
public void hideKeyboard() {
myTextView.setFocusable(true);
myTextView.setFocusableInTouchMode(true);
imm.hideSoftInputFromWindow(myTextView.getWindowToken(), 0);
}
WORKS
public void hideKeyboard() {
imm.hideSoftInputFromWindow(myTextView.getWindowToken(), 0);
}
DOES NOT WORK
imm is dealt with earlier as I am using a Fragment so:
Declare imm in the Fragment
private InputMethodManager imm;
Then in the fragment add:
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
imm = (InputMethodManager)
getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
}
He says after 3 to 4 hours of research and failures !!
Thanks user_CC ! :-)
Phil
I know this post is pretty old, but for those who came for answers from this date and none of the above worked. The code below worked for me on an Activity when an Alert Dialog popped up.
InputMethodManager keyboard = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
to show keyboard:
keyboard.toggleSoftInput(editText.getPaintFlags(), 0);
to hide keyboard:
keyboard.hideSoftInputFromWindow(editText.getWindowToken(), 0);

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

Categories

Resources