I have a problem which occurs only when I use AnySoftKeyboard.
I'm trying to show/hide the keyboard according to EditText focus.
I used the methods I found in this post
When I'm hiding the keyboard, there is a strange behavior -
When I rotate the screen, the text that was in the EditText is doubled.
I thought that it has to do with the onCreate method, but I can see the it happens also when I click "back" (Finish()). I see it for a split second before the Activity is closed.
When I start a new activity, (ActivityB from ActivityA) then clicking "Back" once doesn't do anything (probably "closing" the invisible keyboard).
When I click "back" again, ActivityB closes but I can see for a split second the text from ActivityA keyboard in a big font across the screen (like a 100 millisecond pop-up )
Does anyone has an idea how to deal with it?
Apparently it is a bug in AnySoftKeyboard.
I didn't happen when I use other keyboards.
I solved it by doing setText to the EditText view before hiding it - its probably resets some stuff on keyboard object.
Here is my code:
View view = getWindow().getCurrentFocus();
if (view==null)
return;
IBinder binder = view.getWindowToken();
if (binder == null)
return;
// I used this to fix the strange behaviour
if (view instanceof EditText)
((EditText)view).setText(((EditText)view).getText().toString());
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(binder, InputMethodManager.HIDE_NOT_ALWAYS);
Surprisingly it works!
Try this:
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
Related
Like the title says, I've got a viewPager with some editText fields inside. I can select the editText fields perfectly fine (emulator and physical device). I see the cursor, and I see the underline color change, indicating it's selected, but I can't type. Tapping on the editText fields doesn't open the softKeyboard.
Is this a known issue with a simple fix, or is code needed to identify what's going wrong here?
Finally bumped in to an answer that's working for me, which can be found here.
Basically, if you're using the onCreateDialog() method, you leave that as is, and add the onResume() method below to your DialogFragment's java file.
#Override
public void onResume() {
super.onResume();
Dialog dialog = getDialog();
dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
}
You can force softKeyboard with this code:
EditText yourEditText= (EditText) findViewById(R.id.yourEditText);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(yourEditText, InputMethodManager.SHOW_IMPLICIT);
I have an EditText inside a fragment, which is in itself inside an actionbarsherlock tab. When I touch inside the EditText box a soft keyboard appears with one of the keys having a magnifying glass (search) icon. When I type some text and click on the search key I can process the typed-in-string in my onEditorAction, but the soft keyboard remains on display. How can I close it programatically?
By the way if one answer is that I could configure some setting for EditText such that it closes automatically on search, I would still like to know if the soft keyboard can be closed with a method call as I also have my own search button on screen (nothing to do with the soft keyboard) and I would like the soft keyboard to close when that's pressed too.
Note: Before anyone rushes to claim this question is a repeat of a previous question, I have seen many Q&A's about hiding the soft keyboard at various points. Many of the answers seem inordinately complicated and in many it is not clear whether the idea is to permanently hide the keyboard or just just temporarily close it till the user taps on an EditText field again. Also some answers require calls to methods not available in fragments.
In my fragments I close the keyboard simply in this way:
public static void closeKeyboard(Context c, IBinder windowToken) {
InputMethodManager mgr = (InputMethodManager) c.getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.hideSoftInputFromWindow(windowToken, 0);
}
closeKeyboard(getActivity(), yourEditText.getWindowToken());
This is working code to hide soft keyboard for android.
try {
InputMethodManager input = (InputMethodManager) activity
.getSystemService(Activity.INPUT_METHOD_SERVICE);
input.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
}catch(Exception e) {
e.printStackTrace();
}
I'm using this code in a fragment
InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(text.getWindowToken(), 0);
when I click on an action bar icon and it's working, I don't see why it shouldn't work in your case (maybe I misunderstood the question).
You can check my answer here. It was the only way that worked for me inside fragment.
A clear way to close keyboard and clearfocus of an EditText inside a fragment, is to make sure that your EditText XML has :
android:id="#+id/myEditText"
android:imeOptions="actionDone"
Then set listener to your EditText (with Kotlin, and from a fragment):
myEditText.setOnEditorActionListener({ v, actionId, event ->
if (actionId == EditorInfo.IME_ACTION_DONE) {
myEditText.clearFocus()
val imm = activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.hideSoftInputFromWindow(view!!.windowToken, 0)
}
false
})
Working in Fragment
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
As the title says. I have tried the usual,
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(root.getWindowToken(), 0);
in OnResume(), but it does not work, presumably because the keyboard is open from the Gmail window, not my app's window.
Stepping through the code shows imm.mActive == false at this point, if that matters. I tried putting this code in OnPostResume(), but still no effect.
I have tried using InputMethodManager.HIDE_NOT_ALWAYS instead of 0, but also no effect.
The built-in gallery app does not have this problem, so I think a solution is possible.
I think you need get the applications context in order to hide the keyboard. Something like this:
View v = (Your Base Layout) findViewById(R.id....);
InputMethodManager imm = (InputMethodManager) v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
If that doesn't work you can also try this:
this.getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
hope this helps :)
My solution was to make sure my EditText no longer had focus before navigating to the Gmail activity:
contactSearchView.clearFocus()
Short question: Is it possible (and how) to display the soft-keyboard from a Service?
Long question: I wrote a service which creates a "top bar", displayed on top of all activities, containing an EditText. I want to display the soft-keyboard when that EditText is clicked, but this is not happening.
Of course I've tried this from the Service's onFocusChange() and onClick():
InputMethodManager imm = (InputMethodManager)this.getSystemService(Service.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText, InputMethodManager.SHOW_FORCED);
The workaround I came up with is to request the current activity to show the keyboard, by extending the Activity class and adding an AIDL interface. The drawback is that each key event has to be sent back to the Service (via another AIDL inteface) and manually converted into Unicode.
Moreover, if the current activity contains an EditText, the soft-keyboard only works for the activity and doesn't show up anymore when the service's EditText is selected.
What prevents the service's soft-keyboard to be displayed if the current activity has an EditText? Could it be an Android limitation?
I have faced a similar issue. I have solved it now. Maybe it's too late but may it helps someone.
Possible Cause of Error:
Setting FLAG_NOT_FOCUSABLE during LayoutParams
Window flag: this window won't ever get key input focus, so the user
can not send key or other button events to it. Those will instead go
to whatever focusable window is behind it.
Simply Replace FLAG_NOT_FOCUSABLE with 0
Use this below code:
InputMethodManager imm = null;
mView.findViewById(R.id.editText).setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus){
if(imm==null){
imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
}
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0 );
}
}
});
2nd When creating LayoutParams
private WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
0,
PixelFormat.TRANSLUCENT);
I am facing similar issue. But in my case, problem is due to use of WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE flag while creating WindowManager.LayoutParams for view.
And for Keyboard visibility
InputMethodManager inputManager = (InputMethodManager) context.
getSystemService(Context.INPUT_METHOD_SERVICE);
if (inputManager != null) {
inputManager.toggleSoftInput(0, InputMethodManager.SHOW_IMPLICIT);
}
If you want to show Soft Keyboard on edittext's touch, Why don't you consider using onTouchListener with that edittext. I beleive edittext.requestfocus() will also do this. If not, listener would definately work.
Moreover for the kind of view you mentioning, it would have been best to use fragments instead of service to create view.
Hi I wrapped edittext control onto a control that is being displayed on the screen at users request. It overlays the whole screen until user presses 'done' button on the keyboard.
I am not able to explicitly show the control on the screen. only when user taps into control only then its shown. Am I missing something?
I even try this and it does not brin it up when I launch the overlay that Edit Text exists on:
customCOntrol.showKeyboard();
public void showKeyboard()
{
InputMethodManager imm = (InputMethodManager)_context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(this._textView.getWindowToken(), InputMethodManager.SHOW_IMPLICIT);
}
here is the settig I have on the screen itself in the config file android:windowSoftInputMode="stateHidden|adjustPan"
Thank you in advance
In your showKeyboard function you are calling:
imm.hideSoftInputFromWindow(this._textView.getWindowToken(), InputMethodManager.SHOW_IMPLICIT);
This will hide the softInput keyboard from the window!
Do you want to show the keyboard? If yes then would you use:
imm.showSoftInput(view, flags, resultReceiver);
EDIT: I think you can also toggle the keyboard from the InputMethodManager, try:
imm.toggleSoftInput(0, 0);
#dropsOfJupiter
You can do: editText.requestFocus() as you launch the Activity or Fragment containing your EditText reference. This will give the focus to the EditText and will bring uo the SoftKeyboard.
I hope this helps.
PROBLEM:
I faced with this keyboard not showing up problem. I wrote the following solution inspired by this answer but not their solution! It works fine. In short the reason for this mess is that the request focus and the IMM provided service can only run on a view that is created and active. When you do all these on the creation phase onCreate(Bundle savedInstance).. or onCreateView(LayoutInflater inflater... and the view is still in initializing state, you won't get an active view to act on! I have seen many solutions using delays and checks to wait for that view to get active then do the show keyboard but here is my solution based on the android frame work design:
SOLUTION:
in your activity or fragment override the following make sure your view has the access (define it in the top of the activity/fragment):
#Override
public void onStart() {
yourView.requestFocus();
showSoftKeyboard(yourView);
super.onStart();
}
public void showSoftKeyboard(View view) {
if(view.requestFocus()){
InputMethodManager imm = (InputMethodManager)mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(view,InputMethodManager.SHOW_IMPLICIT);
}
}