1
\$\begingroup\$

I am using the following method to hide the keyboard when the user touches outside edit text.

I am passing most top view into my method Hidekeyboard(final View mView) to check if this view group or view. If view group, it goes in each view to check if this view EditText or not. If it's not EditText, it handles user touch and hiding the keyboard.

In this case, I am passing LoginRelativeLayout as it the most top layout.

I need someone to suggest better ways to do the same method or any tips refine my code. Also, can I post this method in one place and reference it as I post this code in each activity?

XML

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginLeft="60dp"
    android:layout_marginRight="60dp"
    android:layout_marginTop="90dp">

    <ImageView
        android:id="@+id/logo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:src="@drawable/app_logo"
        android:text="Validate Account" />

    <EditText
        android:id="@+id/et_account"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/logo"
        android:layout_marginTop="55dp"
        android:background="@drawable/et_rounded"
        android:drawablePadding="15dp"

        android:drawableRight="@drawable/user_icon"
        android:ems="10"
        android:textSize="20sp"
        android:textColor="@color/text_color"
        android:gravity="right"
        android:hint="@string/account"
        android:padding="10dp"
        android:paddingRight="25dp" />

    <EditText
        android:id="@+id/et_password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textPassword"
        android:textColor="@color/text_color"
        android:layout_below="@+id/et_account"
        android:layout_marginTop="15dp"
        android:background="@drawable/et_rounded"
        android:drawablePadding="15dp"
        android:drawableRight="@drawable/password_icon"
        android:ems="10"
        android:textSize="20sp"
        android:gravity="right"
        android:hint="*****"
        android:padding="10dp" />

    <Button
        android:id="@+id/bt_login"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/et_password"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="15dp"
        android:background="@drawable/et_rounded_white"
        android:textColor="#434a54"
        android:textAllCaps="false"
        android:text="@string/register"
        android:textSize="25sp" />

    <TextView
        android:id="@+id/tv_forget_pass"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/bt_login"
        android:layout_centerInParent="true"
        android:layout_marginTop="25dp"
        android:textSize="18sp"
        android:textColor="@android:color/white"
        android:text="@string/ForgetPassword" />
</RelativeLayout>

Java

 /*

    this method made to hide keyboard when touch outside edittext
    @param  view
    return void
     */

    private void Hidekeyboard(final View mView) {

        if (mView instanceof ViewGroup) {
            for (int i = 0; i < ((ViewGroup) mView).getChildCount(); i++) {
                View innerView = ((ViewGroup) mView).getChildAt(i);
                Hidekeyboard(innerView);
            }
        }
        if (!(mView instanceof EditText)) {
            mView.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View view, MotionEvent motionEvent) {
                    // this call new methodto hide keyboard from non edittext
                    HideSoftkeyboard(mView);
                    return false;
                }
            });

        }

    }

    private void HideSoftkeyboard(View mView) {
        if (mView != null) {
            InputMethodManager inputmanger = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
            if (inputmanger != null) {
                if (Build.VERSION.SDK_INT < 11) {
                    inputmanger.hideSoftInputFromWindow(mView.getWindowToken(), 0);
                } else {
                    if (this.getCurrentFocus() != null) {
                        inputmanger.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
                    }
                    mView.clearFocus();
                }
                mView.clearFocus();

            }
        }
    }
    ///////////////////////////////////////////////////////////////////
    ///                         end of method                      ///
    //////////////////////////////////////////////////////////////////
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.