I have an activity which opens up a dialog that contains edittexts on a button click. As soon as the dialog opens the softkeyboard shows. I want to prevent this. Only when I click on the edittext in the dialog should the softkeyboard appear. I am using Android 4.
Thanks in advance
To achieve this, set the soft input mode just before you call dialog.Show():
// Build and create your dialog.
AlertDialog dialog = new AlertDialogBuilder(...).create();
// Hide soft keypad by default (will be displayed when user touches any edit field).
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
// Show the dialog.
dialog.show();
This should work is any case:
public void hideKeyboard() {
mActivity.runOnUiThread(new Runnable() {
public void run() {
InputMethodManager inputManager = (InputMethodManager) mActivity
.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(mActivity
.getCurrentFocus().getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
}
});
}
adding this to your Manifest is all you hav to do:
android:windowSoftInputMode="stateHidden"
Simple, but in case someone was looking here
:)
Related
I have a edittext in my android app where I can put a person birthdate. When I click on it a dialog fragment appears, but I have to click two times. After first click there is an keyboard and I don't want it.
I know that I can use button instead, but i prefer editext, because it looks better :)
How can I change it? Can I open diagram in first edittext click? Thanks in advance!
You can make your EditText not focusable by adding android:focusable="false" in XML declaration and then use simple onClickListener on the EditText.
Else, you can also use the following method to hide the keyboard:
public void hideSoftKeyBoard(Activity activity) {
// Check if no view has focus:
View view = activity.getCurrentFocus();
if (view != null) {
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}
This can be invoked as follows:
hideSoftKeyBoard(this) from an activity or hideSoftKeyBoard(getActivity()) from a fragment.
Hope this helps!
I want window of my app to be resized when soft keyboard is launched. I thus added following tag to main activity in manifest.xml file
android:windowSoftInputMode="adjustResize"
however even at time of app lunch keyboard is displayed and window is resized.
I want that at launch atleast keyboard not to be displayed automatically and window to be resized. only when some one is type should the window resizing should occur.
any guidance
just to further add I have editText which gets the control at time of launch. before adding android:windowSoftInputMode="adjustResize" , complete screen use to get displayed and when I touched to type was keyboard displayed. I want the same behavior, that is keyboard should not to be displayed by default but displayed and window resized when someone touches to type
You can set the Activity Attribute by adding this to your Manifest File here
Like so, but haven't tried it.
android:windowSoftInputMode
<activity android:windowSoftInputMode="stateHidden|adjustResize" . . . >
How the main window of the activity interacts with the window containing the on-screen soft keyboard.
You can also refer here
Do below changes to your code
To hide the keyboard on activity launch add below code
public void hideSoftKeyboard() {
if(getCurrentFocus()!=null) {
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
}
}
To show keyboard when edittext gets focused, use below code
public void showSoftKeyboard(View view) {
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
view.requestFocus();
inputMethodManager.showSoftInput(view, 0);
}
Call hideSoftKeyboard() in your onCreate method.
Once edittext gets focused use below code.
EditText editText = (EditText)findViewById(R.id.editText);
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean b) {
//Toast.makeText(getApplicationContext(),"Focused",Toast.LENGTH_SHORT).show();
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE); // Resizing window programatically
showSoftKeyboard(view);
}
});
I have a dialog includes edittext, when user tabs the dialog and not on the keyboard or on the edittext, dialog will dismiss and so does softkeyboard, what does system deal with this action? It can dismiss the keyboard sometimes and others does not, I am confused about it. Thank you!
use
public void hideKeyboard(){
Activity activity = (Activity) mContext;
View view = activity.getCurrentFocus();
if (view != null) {
InputMethodManager imm = (InputMethodManager)activity.getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}
add
softInputmode:statehidden inside of manifest activity tag
Every time I have focus on some edit field and Dialog shows up, it will pull up soft keyboard as soon as I dismiss(); Dialog. I have tried every way to remove it after click event, but whatever I do it still shows up.
public static void hideSoftInput(FragmentActivity _activity){
if(_activity.getCurrentFocus() != null){
InputMethodManager inputManager = (InputMethodManager) _activity.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(_activity.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
}
public static void hideSoftInput(View _v, Context _c){
if(_v.getWindowToken() != null){
InputMethodManager inputManager = (InputMethodManager) _c.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(_v.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
}
I can't find it now but someone else on here suggested to wrap the call to hide the keyboard in a postDelayed call.
It worked for me when many other options failed. The only thing is that it may give a funny jiggle of the screen because you will be suppressing the keyboard while it is trying to show. Without the postDelayed it seems it will try to hide the keyboard before Android tries to show it after the dialog has closed. So ultimately we have to fight a timing issue in Android.
Something like this:
view.postDelayed(new Runnable()
{
#Override
public void run()
{
hideKeyboard();
}
}, 50);
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.