Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide Android soft keyboard in Fragment when clicked on outside

I have a fragment containing an EditText for input, but now I want to close the keyboard when the user clicks on the screen outside of the EditText.

I know how to do this in an activity, but it seems to be different for fragments.

i am calling this method on view.onTouchListener

public static void hideSoftKeyboard() {
InputMethodManager inputMethodManager = (InputMethodManager) getActivity().getSystemService(Activity.INPUT_METHOD_SERVICE);

inputMethodManager.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), 0);
}

anyone have solution, thanks

like image 647
Mehul Gajjar Avatar asked Apr 27 '16 11:04

Mehul Gajjar


People also ask

How do I turn off soft keyboard on Android after clicking outside?

Ok everyone knows that to hide a keyboard you need to implement: InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE); imm. hideSoftInputFromWindow(getCurrentFocus(). getWindowToken(), 0);

How do I prevent the soft keyboard from pushing my view up in fragment?

Setting android:isScrollContainer = "false" inside the ScrollView worked for me. According to the documentation, settings "isScrollContainer" to true means that the scroll view can be resized to shrink its overall window so that there is space for an input method.

How do I hide my soft keyboard?

Hiding the Soft Keyboard Programmatically 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 the soft keyboard on Android after clicking outside EditText Kotlin?

This example demonstrates how to hide a soft keyboard on android after clicking outside EditText using Kotlin. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.


3 Answers

In the parent Activity of the fragment override the following method:

 @Override
public boolean dispatchTouchEvent(MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        View v = getCurrentFocus();
        if ( v instanceof EditText) {
            Rect outRect = new Rect();
            v.getGlobalVisibleRect(outRect);
            if (!outRect.contains((int)event.getRawX(), (int)event.getRawY())) {
                v.clearFocus();
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
            }
        }
    }
    return super.dispatchTouchEvent(event);
}

And in the layout of the fragment use this attribute:

android:focusableInTouchMode="true"

Hope this will help you.

like image 165
Kanchan Chowdhury Avatar answered Oct 20 '22 20:10

Kanchan Chowdhury


Use this method its works fine

public static void hideKeyBoardMethod(final Context con, final View view) {
        try {
            view.post(new Runnable() {
                @Override
                public void run() {
                    InputMethodManager imm = (InputMethodManager) con.getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
                }
            });

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
like image 20
Nitesh Pareek Avatar answered Oct 20 '22 20:10

Nitesh Pareek


You could use this method is working fine for me. Just pass the reference of the root element of your layout like this

setupUI(rootView.findViewById(R.id.rootParent))

code for the setupUI is below..

public void setupUI(View parentView) {

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

        view.setOnTouchListener(new View.OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {
                hideSoftKeyboard();
                handleCallBack();
                return false;
            }
        });
    }
like image 41
Akshat Vajpayee Avatar answered Oct 20 '22 18:10

Akshat Vajpayee