Search code examples
androidandroid-edittextandroid-recyclerview

Hide keyboard when Edit Text in Recycler View is scrolled off screen


I have a RecyclerView that contains EditText child elements. I would like to hide the soft keyboard when the selected EditText is scrolled off screen. How can I tell when the EditText is no longer on screen? Is there some event listener that I can attach to the EditText element to tell?


Solution

  • Implement onTouchListener like this:

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