I have two fragments in one Activity and both of the fragments contain one EditText. When the the first EditText is focused (keyboard shown) and the user presses the next button of the keyboard, I am transferring the focus to the EditText in the second fragment by using this code:
View next = autoCompleteTextView.focusSearch(View.FOCUS_DOWN);
if (next != null) {
next.requestFocus();
}
The second EditText receives the focus as it should (the cursor starts blinking in it) but the keyboard that was shown, gets hidden.
I don't understand why this happens. I tried million different solutions, to force the keyboard to be shown again but nothing works. I don't know why it gets hidden in the first place, I am just transferring focus.
The only thing that worked for me is this:
mComposeMsgBody.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus && mComposeMsgBody.isEnabled()) {
mComposeMsgBody.post(new Runnable() {
#Override
public void run() {
final InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(mComposeMsgBody, InputMethodManager.SHOW_IMPLICIT);
}
});
}
}
});
But it is not ideal, since the keyboard tries to get hidden, and then I am forcing it go up, so there is this 1 second down-up movement that the keyboard does. If someone has a better solution for just transferring focus without the keyboard doing anything, please post an answer.
Related
This question already has answers here:
How to stop EditText from gaining focus when an activity starts in Android?
(54 answers)
Closed 5 years ago.
I have an Activity with only one EdtiText. When that Activity starts, the EditText is focused and the soft keyboard is shown. This seem to happen after onResume, because when I programmatically hide the keyboard in onResume it doesn't work. When I do this:
#Override
protected void onResume() {
super.onResume();
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
InputMethodManager imm = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
//Find the currently focused view, so we can grab the correct window token from it.
//If no view currently has focus, create a new one, just so we can grab a window token from it
imm.hideSoftInputFromWindow(etBarcode.getWindowToken(), 0);
}
}, 500);
}
it hides it (after popping up shortly).
Is there an event on an EditText I can use to preven the keyboard popping up? Or some other way of preventing it to show?
Update focusableInTouchMode does not do what I want, because when set to true the keyboard pops up, when set to false it is not focusable at all.
// Add following code in activity onCreate
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
For parent layout android:focusableInTouchMode="true"
You can set property of Layout like
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"
The problem is very complicated, as it's about views getting the focus, and how it's handled by all the layout, about the touch mode being focusable, and last but not least about how the soft keyboard handles that.
But this works for me:
In manifest:
android:windowSoftInputMode="stateHidden|stateAlwaysHidden"
In layout:
android:focusable="true"
android:focusableInTouchMode="true"
and last but not least, touch listener set to the EditText to prevent the soft keyboard to show up once touched:
mMyText.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// forward the touch event to the view (for instance edit text will update the cursor to the touched position), then
// prevent the soft keyboard from popping up and consume the event
v.onTouchEvent(event);
disableSoftKeyboard(MyActivity.this);
return true;
}
});
whereas the method does more or less what you're doing already:
public void disableSoftKeyboard(#NonNull Activity activity) {
View view = activity.getCurrentFocus();
if (view != null) {
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
} else {
activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
}
}
Hope it helps, and also that I didn't forget anything :)
I have an EditText where I want to handle the inputs myself, so I don't want the soft keyboard to show up when I click it (or when selection changes, focus changed, long clicked, etc). However, I still want to be able select the text, change cursor position, copy/past, etc.
I have tried putting android:windowSoftInputMode="stateAlwaysHidden" in the manifest, but that doesn't seems to do much. I also tried adding the following
edittext.setOnTouchListener(new OnTouchListener() {
#Override public boolean onTouch(View v, MotionEvent event) {
EditText edittext = (EditText) v;
int inType = edittext.getInputType(); // Backup the input type
edittext.setInputType(InputType.TYPE_NULL); // Disable standard keyboard
edittext.onTouchEvent(event); // Call native handler
edittext.setInputType(inType); // Restore input type
return true; // Consume touch event
}
});
which disables the keyboard, but also prevent the cursor from working.
Currently I'm basically trying to add listeners for all the situations where the keyboard might pop up to toggle it off, but this is very clunky and I can't catch all the cases. Is there a better way to disable the soft keyboard for a particular EditText or fragment?
Obviously the best solution would be if Google gave an inputType that works like this.
The following tends to work. It will sometimes flicker as the keyboard is loaded and then is instantly murdered. But, it just listens for when you click on the textview and then when that happens it murders the keyboard.
It can't account for things like if the textfield gets focus some other way, but for my purposes (I have a textfield for a barcode reader that gets a barcode read into it by a barcode reader (hardware keyboard)), so a softkeyboard makes no sense.
editView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) {
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
//imm.hideSoftInputFromWindow(v.getWindowToken(), InputMethodManager.HIDE_IMPLICIT_ONLY);
}
}
});
I also added the other line you might want in there and commented it out. Namely if you want to hide the Android launched soft keyboard or if a user loads the keyboard by holding menu if that might close too.
Try this code.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Disable IME for this application
getWindow().setFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM,
WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
setContentView(R.layout.activity_layout);
Ok - I've bonked my head quite a bit with this. I have a custom dialog which has multiple edittexts. I've set my layout so that it does not automatically focus on the first as both are initially filled with default values (thus the user may just press 'accept')
I want any edittext to clear itself when touched and open a keyboard for numeric input. They may change one or both fields. If they touch and do not input, the field should change back to a default value.
I have implemented this with setOnFocusChangedListeners in addition to the addTextChangedListeners.
My first problem occured with the realization that the keyboard was toggling itself open/closed when a user touched both edittexts. I resolved this by using SHOW_FORCED,HIDE_NOT_ALWAYS as parameters to toggleSoftInput. Note that this was the only set of parameters which kept the keyboard open when a second field was touched.
Unfortunately, this has created a second problem which I do not understand - on exiting the dialog, I can no longer close the keyboard (ie, it remains visible on the following view). Previously, when I did not make an effort to clear the input, keyboards closed out ok. Using SHOW_IMPLICIT (ignoring the toggle issue) also has no problem with an open keyboard being closed on exit.
So.. how the * do I get this to work?
Below are some relevant sections of code:
edQuantity.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
Log.i(TAG,"edQuantity focus changed");
if (hasFocus) {
Log.i(TAG,"edQuantity HAS FOCUS");
edQuantity.setHint("");
edQuantity.setText("");
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,InputMethodManager.HIDE_NOT_ALWAYS);
}
// was the other field left empty after a change attempt?
if (String.valueOf(edPrice.getText()).length()==0) edPrice.setText("0.01");
}
});
edPrice.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
Log.i(TAG,"edPrice focus changed");
if (hasFocus) {
Log.i(TAG,"edPrice HAS FOCUS");
edPrice.setHint("");
edPrice.setText("");
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,InputMethodManager.HIDE_NOT_ALWAYS);
}
// was the other field left empty after a change attempt?
if (String.valueOf(edQuantity.getText()).length()==0) edQuantity.setText("1");
}
});
protected static void dismissCustomDialog(Dialog dialog, Context context) {
if (dialog != null) {
// hide the soft keyboard
if (dialog.getCurrentFocus() != null) {
Log.i(TAG,"trying to hide a keyboard");
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(dialog.getWindow().getDecorView().getWindowToken(), InputMethodManager.HIDE_IMPLICIT_ONLY);
imm.hideSoftInputFromWindow(dialog.getWindow().getDecorView().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
if(dialog.isShowing()) dialog.dismiss();
}
}
As per my comment above, used a toggle to see if a keyboard was already opened.
When i click on the edittextview then some times keyboard shown or some times keyboard are not shown.
In android 2.1 it show the keyboard when i click on the edittextview
but when i start same application it on android 2.2 then it not show the keyboard.
Help me how to show that problem.
OK, This might be a late response, but it worked.
I have met this problem on android 2.1 and 2.3.x(not tested on other versions of SDKs).
I noticed a strange thing that when my click on the EditText was unable to open the keyboard, I pressed the BACK button to show an alert dialog and then I canceled(closed) it, and clicked the EditText again, now the keyboard was brought to life again.
From that I can conclude that the keyboard will always show for the EditText if the EditText does not previously own focus(showing an alert dialog over the EditText view will make the EditText to lose focus).
so call the function below on your EditText when it is brought to front:
mEditText.clearFocus();
or
parentViewThatContainsEditTextView.clearFocus();
I had a similar problem on Galaxy S3 (displaying EditText controls on a PopupWindow - the keyboard was never showing). This solved my issue:
final PopupWindow popUp = new PopupWindow(vbl.getMainLayout());
[....]
popUp.setFocusable(true);
popUp.update();
I didn't want to EditText lose a focus using editText.clearFocus(). Came up to this solution.
#Override
public void onResume() {
super.onResume();
if (Build.VERSION.SDK_INT < 11) {
editText.clearFocus();
editText.requestFocus();
}
}
here's a possible solution:
editText.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(final View v, final boolean hasFocus) {
if (hasFocus && editText.isEnabled() && editText.isFocusable()) {
editText.post(new Runnable() {
#Override
public void run() {
final InputMethodManager imm = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText,InputMethodManager.SHOW_IMPLICIT);
}
});
}
}
});
code is based on the next link:
http://turbomanage.wordpress.com/2012/05/02/show-soft-keyboard-automatically-when-edittext-receives-focus/
In my case it was in a PopupWindow and I simply needed to call popupWindow.setFocusable(true)
I had this same problem when displaying an EditText in a DialogFragment. Despite the EditText getting focus (i.e., when clicked, it showed the flashing caret), the keyboard did not display.
My solution was to add a dummy EditText to the uppermost view of my DialogFragment.
<EditText
android:id="#+id/editTextFix"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/fix"
android:importantForAutofill="no"
tools:targetApi="o"
android:inputType="text"
android:visibility="gone"/>
Possible scenarios:
1) On clicking the EditText, usually the keyboard comes up. But if you press the back key button in the emulator the keyboard (not the screen keyboard) dimisses.
2) In code you can disable the keyboard on clicking the EditText by setting a flag.
InputMethodManager inputmethodmgr= (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
inputmethodmgr.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
It works like a charm, In a case if you even want to hide on click of the edittextView.
textView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
displayKeyboard();
}
});
private void displayKeyboard(){
if (textView != null) {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInputFromWindow(textView.getApplicationWindowToken(), InputMethodManager.SHOW_FORCED, 0);
}
}
In your parent view check if there is android:descendantFocusability="blocksDescendants"
remove it.
I have 2 editfields in my Custom Dialog which is called from ACtivity, among them 1 is of "trxtPassword" and other of "text" type. Keyboard doesn't appear in "text" type editbox, but just comes on "textPassword" edittext, and then doesn't go only.
I tried the following, but nothing works:
InputMethodManager inputManager = (InputMethodManager)mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
//inputManager.hideSoftInputFromWindow(txt_username.getWindowToken(), 0);
//inputManager.hideSoftInputFromWindow(txt_password.getWindowToken(), 0);
If I make txt_password.setInputType(0); then others can see the password easily, so that can't be used.
What more can be doen to achieve the goal? I did to trap the onLostFocus on txt
txt_password.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus == false) {
InputMethodManager inputManager = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(LoginDialog.this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_IMPLICIT_ONLY);
}
}
});
But unfortunately, once entered, if I click anywhere else or any checkbox, then also the focus is not lost from the txt_password field. That is only lost if I click another editText and then the onFocusChange event is fired and throws error and the application shuts down.
Any idea how to accomplish this?
Use that to keep keyboard hidden on activity start
<activity
android:name=".views.DrugstoreEditView"
android:windowSoftInputMode="stateHidden"></activity>
And there is one usefull answer: How to hide soft keyboard on android after clicking outside EditText?