Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to hide virtual keyboard on touch of a spinner

Tags:

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" />
like image 332
swati Avatar asked Oct 29 '12 06:10

swati


People also ask

How can I hide my keyboard while typing?

Tap the back button on your Android. It's the left-pointing arrow button at the bottom of the screen, either at the bottom-left or bottom-right corner. The keyboard is now hidden. The back button may be a physical button or on the touch screen.

How do I hide my keyboard from clicking?

To hide keyboard, use the following code. InputMethodManager inputMethodManager = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE); inputMethodManager. hideSoftInputFromWindow(v. getApplicationWindowToken(),0);

How do you hide keyboard on Android?

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.


Video Answer


1 Answers

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;
            }
        }) ;
like image 168
Niranj Patel Avatar answered Sep 18 '22 09:09

Niranj Patel