Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide keypad in android while touching outside Edit Text Area

I know that the code for dismiss tyhe keypad in android is

InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);

Can anyone suggest me a method to hide the keypad if we touch the area outside the text area other than the keypad in the screen.

like image 396
Nithin Michael Avatar asked Dec 13 '13 05:12

Nithin Michael


People also ask

How do I hide the keyboard by one tap outside of an Edittext?

and put the following code in the onTouch method. InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE); imm. hideSoftInputFromWindow(getCurrentFocus(). getWindowToken(), 0);

How do I make my keyboard invisible 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.

How do I hide my keyboard?

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.


2 Answers

Code to dismiss Softkeyboard is below:

public static void hideSoftKeyboard(Activity activity) {
    InputMethodManager inputMethodManager = (InputMethodManager)  activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
    inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
}

You can put it in Utility Class or if you are defining it within an activity, avoid the activity parameter, or call hideSoftKeyboard(this).

You can write a method that iterates through every View in your activity, and check if it is an instanceof EditText if it is not register a setOnTouchListener to that component and everything will fall in place. In case you are wondering how to do that, it is in fact quite simple. Here is what you do, you write a recursive method like the following.

public void setupUI(View view) {

    //Set up touch listener for non-text box views to hide keyboard.
    if(!(view instanceof EditText)) {

        view.setOnTouchListener(new OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {
                hideSoftKeyboard();
                return false;
            }

        });
    }

    //If a layout container, iterate over children and seed recursion.
    if (view instanceof ViewGroup) {

        for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {

            View innerView = ((ViewGroup) view).getChildAt(i);

            setupUI(innerView);
        }
    }
}

Call this method after SetcontentView() with paramet as id of your view like:

RelativeLayoutPanel android:id="@+id/parent"> ... </RelativeLayout>

Then call setupUI(findViewById(R.id.parent))

like image 164
Sharmilee Avatar answered Sep 23 '22 06:09

Sharmilee


Best way you can use is DONE button besides EditText make your onClickListener to do like,

done.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View arg0) {
        InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
}
});
like image 39
Jaydipsinh Zala Avatar answered Sep 24 '22 06:09

Jaydipsinh Zala