Soft keyboard doesn't get hide programmatically in android
Asked Answered
S

4

5

I am newbie to android and working on a demo for alert dialog box,I want to close the soft keyboard once one of the buttons from the alert is clicked.I have tried it programaticaly but keyboard remains open,can you pls help me for this issue, code

  public void Show_Dialog() {
        final AlertDialog.Builder alertDialog = new AlertDialog.Builder(
                SwipeActivity.this);
        LayoutInflater inflater = this.getLayoutInflater();
        final View layout = inflater.inflate(R.layout.add_albom_dialog, null);
        alertDialog.setView(layout);

        final InputMethodManager inputManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
        //android:digits="abcdefghijklmnopqrstuvwxyz1234567890 "

        alertDialog.setPositiveButton("Create",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        EditText txts = (EditText) layout
                                .findViewById(R.id.addAblum_edit);
                        hideSoftKeyboardDialogDismiss(SwipeActivity.this);
                        if(txts.getText().toString().trim().length() > 0) {
                            Add_album(txts.getText().toString());

                        } else {

                            AlertDialog alertDialog = new AlertDialog.Builder(SwipeActivity.this).create();
                            alertDialog.setTitle("Error");
                            alertDialog.setMessage("Name can't be emtpy");
                            alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
                                    new DialogInterface.OnClickListener() {
                                        public void onClick(DialogInterface dialog, int which) {
                                            dialog.dismiss();
                                            inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
                                            inputManager.hideSoftInputFromInputMethod(getCurrentFocus().getWindowToken(), 0);

                                        }
                                    });
                            alertDialog.show();

                        }
                        dialog.cancel(); // Your custom code
                    }
                });

        /* When negative (No/cancel) button is clicked */
        alertDialog.setNegativeButton("Cancel",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        hideSoftKeyboardDialogDismiss(SwipeActivity.this);
                        dialog.cancel();
                        //  finish();

                    }

                });
        alertDialog.show();
    }
Signor answered 10/3, 2016 at 11:36 Comment(0)
H
4

Try this:

protected void hideSoftKeyboard(EditText mSearchView) {
    InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    mgr.hideSoftInputFromWindow(mSearchView.getWindowToken(), 0);
}
Hulahula answered 10/3, 2016 at 12:4 Comment(5)
Rosu alin -Awesome ,you are great brother...i am having some more issues.can you help with that?Signor
raise them on stackoverflow, leave them in a comment here? I will take a lookHulahula
ok,brother.i am telling you,but i cant post question it says you can post every 90 mins..:(Signor
Hi,I am explaining you my issue,I am having a listView and i am updating it on scroll,But when data comes and added to list,my list goes to first item ,any idea?Signor
check so that you do not set the adapter again. Make a new function in the adapter, where you add data to the data you already have, without needing to set the adapter on the list againHulahula
I
2
dialog.setOnDissmissListener(){
   void onDismiss(){

    inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
    inputManager.hideSoftInputFromInputMethod(getCurrentFocus().getWindowToken(), 0);

   }
} 
dialog.dismiss();
Inhale answered 10/3, 2016 at 12:3 Comment(0)
S
1

Try doing it the following way

final InputMethodManager inputManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);

final AlertDialog alertDialog = new AlertDialog.Builder(SwipeActivity.this).create();
                  alertDialog.setTitle("Error");
                  alertDialog.setMessage("Name can't be emtpy");
                  alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
                      new DialogInterface.OnClickListener() {
                          public void onClick(DialogInterface dialog, int which) {

                                 inputManager.hideSoftInputFromInputMethod(alertDialog.getCurrentFocus().getWindowToken(), 0);
                                 dialog.dismiss();

                          }
                  });
                 alertDialog.show();

Use your alertDailog's current focus not your activity

Stahl answered 10/3, 2016 at 11:45 Comment(0)
M
1

Actually there must be delay so use this code

  public static void hideSoftKeyboardDialogDismiss(final Activity activity) {
    new Handler().postDelayed(new Runnable() {

        @Override
        public void run() {
            activity.runOnUiThread(new Runnable() {

                @Override
                public void run() {
                 InputMethodManager inputMethodManager =  (InputMethodManager) activity
                  .getSystemService(Activity.INPUT_METHOD_SERVICE);
                 if (null != activity.getCurrentFocus()) {
                  inputMethodManager.hideSoftInputFromWindow(activity
                   .getCurrentFocus().getWindowToken(), 0);
                  }
                }
            });
        }
    }, 1);
}
Marrs answered 10/3, 2016 at 11:48 Comment(5)
Hi,Thanks ashish,I have to call it? but where.can u help me little more brother..:)Signor
when you are hiding dialog by dialog.dismissMarrs
on its button clicks.there are two buttons "create" and "cancle". on both button i am dismising it.Signor
please use this dialog.dismiss(); hideSoftKeyboardDialogDismiss(SwipeActivity.this);Marrs
if still don't work then please try with other values of delay like 100 in place of 1 in hideSoftKeyboardDialogDismissMarrs

© 2022 - 2024 — McMap. All rights reserved.