Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Soft keyboard doesn't get hide programmatically in android

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();
    }
like image 798
sulphuric Acid Avatar asked Mar 10 '16 11:03

sulphuric Acid


People also ask

How do you close hide the Android soft keyboard programmatically?

Hiding the Soft Keyboard Programmatically You can force Android to hide the virtual keyboard using the InputMethodManager, calling hideSoftInputFromWindow, passing in the token of the window containing your edit field. This will force the keyboard to be hidden in all situations.

How do I hide the soft keyboard on Android after clicking outside EditText Kotlin?

This example demonstrates how to hide a soft keyboard on android after clicking outside EditText using Kotlin. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.


1 Answers

Try this:

protected void hideSoftKeyboard(EditText mSearchView) {
    InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    mgr.hideSoftInputFromWindow(mSearchView.getWindowToken(), 0);
}
like image 99
rosu alin Avatar answered Oct 05 '22 22:10

rosu alin