swati swati - 18 days ago 17
Android Question

how to hide virtual keyboard on touch of a spinner

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" />

Answer

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;
            }
        }) ;