-1

Problem

I want to change mobile number using OTP verification. I have a change button on user profile screen. While clicking on change button, I am opening one dialog box. Once dialog-box will open user is typing his new contact number and pressing resend otp button of dialog box then dialog box will dismiss and key board should also hide. But, keyboard is not hiding. One thing after change_no_dialog-box is dismiss I am showing one more dialog-box for Enter OTP once user will enter the OTP and press submit button again same problem is coming keyboard is not hiding.

Here what i have tried

Edittext on Dialog box

        <android.support.design.widget.TextInputLayout
            android:layout_marginTop="36dp"
            android:theme="@style/TextLabel"
            android:id="@+id/no_layout"
            android:layout_marginLeft="24dp"
            android:layout_marginRight="24dp"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <EditText
                android:paddingLeft="8dp"
                android:id="@+id/dialog_box_mobile"
                android:layout_width="match_parent"
                android:layout_height="60dp"
                android:inputType="number"
                android:imeOptions="actionDone"
                android:singleLine="true"
                android:textColorHint="@color/navigation_header"
                android:hint="@string/mobno"
                android:textSize="@dimen/sisteen"
                android:maxLength="10"/>

        </android.support.design.widget.TextInputLayout>

Onclick of resend otp button

  if (v.getId() == R.id.resend_otp_btn) {

            if (dialog_box_mobile.getText().toString().equals(user_mobile_no)) {
                hideKeyboard(EditProfileActivity.this);
                dialog.dismiss();
                bottomSnackbarMessage("It looks like same number !");
                // Toast.makeText(EditProfileActivity.this, "It looks like same number!", Toast.LENGTH_SHORT).show();

            } else {

                if (dialog_box_mobile.getText().length() != 0) {
                    if (dialog_box_mobile.getText().toString().length() < 10) {

                        dialog_box_mobile.setError("Invalid phone number !");
                        resend_otp_btn.setClickable(true);

                    } else {
                        if (CheckNetwork.isConnected()) {
                            resend_otp_btn.setClickable(false);
                            dialog.dismiss();
                            startPhoneNumberVerification(dialog_box_mobile.getText().toString());
                        } else {
                            dialog.dismiss();
                            bottomSnackbar();
                        }

                    }


                } else if (dialog_box_mobile.getText().toString().equalsIgnoreCase("")) {
                    dialog_box_mobile.setError("Mobile number required");
                    resend_otp_btn.setClickable(true);

                }


            }


        }

Keyboard hide method

  public static void hideKeyboard(Activity activity) {
        InputMethodManager imm = (InputMethodManager) 
             activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
        //Find the currently focused view, so we can grab the correct window 
              token from it.
        View view = activity.getCurrentFocus();
        //If no view currently has focus, create a new one, just so we can 
          grab a window token from it
        if (view == null) {
            view = new View(activity);
        }
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }

Adding this on mainifest and every Activity Layout

 android:windowSoftInputMode="stateHidden"

kindly see the ScreenShot

On change button clicked opening dialog

Here i want to hide keyboard while dialog box is dismiss

Phantômaxx
  • 36,442
  • 21
  • 78
  • 108

1 Answers1

-1

try this:

        public static void hideKeyboard(EditText edt) {
            InputMethodManager imm = (InputMethodManager) 
                 edt.getContext().getSystemService(Activity.INPUT_METHOD_SERVICE);              
            imm.hideSoftInputFromWindow(et.getWindowToken(), 0);
        }

Also Add below dismiss listener:

           dialogBox.setOnDismissListener(new DialogInterface.OnDismissListener() {
        @Override
        public void onDismiss(DialogInterface dialogInterface) {
                   hideKeyBoard(editText);
            dialogBox.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

        }
    });
vivek mahajan
  • 515
  • 3
  • 16