android – how to hide virtual keyboard on touch of a spinner-ThrowExceptions

Exception or error:

I have a edittext and a spinner. When I touch on the edittext the keyboard appears, after completed the text editing I touch on the dropdown arrow of the spinner but the keyboard doesn’t disappears automatically. Please give me some solution.
I tried this code

 InputMethodManager imm=(InputMethodManager)getApplicationContext().getSystemService(SetUpProfileActivity.this.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(mDateOfBirth.getWindowToken(), 0);

This is the xml

<LinearLayout
            android:id="@+id/outerlayout"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:padding="10dp" 

            >

            <TextView
                android:id="@+id/name_view"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="@string/profile_name"
                android:textColor="#ffffff" />

            <EditText
                android:id="@+id/profile_name"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:background="@drawable/txtbox"
                android:singleLine="true" />

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:text="@string/dateofbirth"
                android:textColor="#ffffff" />

            <Spinner
                android:id="@+id/dob"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:background="@drawable/dropdown" />
How to solve:

Try this code, I hope it will work for you.

mSpinner.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                InputMethodManager imm=(InputMethodManager)getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(mEditText.getWindowToken(), 0);
                return false;
            }
        }) ;

###

If you are having Spinner and EditText on an activity then this is the problem you are gonna feel for sure ,

call onTouchListener on your spinner and inside that take the reference of your edittext and hide softkeyboard.

 mySpinner.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            InputMethodManager inputMethodManager=(InputMethodManager)getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
            inputMethodManager.hideSoftInputFromWindow(mReuestBloodActNotes.getWindowToken(), 0);
            return false;
        }
    }) ;

###

Try this:

// hide the keyboard if open
            inputManager.hideSoftInputFromWindow(getParent().getCurrentFocus()
                    .getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);

Leave a Reply

Your email address will not be published. Required fields are marked *