5

I tried to find answer in other posts, but I couldn't really find what i was looking for.

I'm building chat app and i want for keyboard to disappear when clicked on the RecyclerView with chat messages.

Simple way with code below doesn't work.

recyclerView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.d("RecyclerTest", "Clicked");
                hideKeyboard();
            }
        });

I know how to implement onClickListener on Items inside the RecyclerView, but i was wondering if there is a way to add onClick on the whole RecyclerView, or should i just not bother and add onClick on every item in the adapter, and hide keyboard when either one is clicked?

EDIT:

My Hide Keyboard method

private void hideKeyboard() {

        InputMethodManager imm = (InputMethodManager) ChatRoom.this.getSystemService(Activity.INPUT_METHOD_SERVICE);

        imm.hideSoftInputFromWindow(ChatRoom.this.getCurrentFocus().getWindowToken(), 0);

    }
Wiktor
  • 885
  • 1
  • 9
  • 23
  • What do you mean by "doesn't work"? What happens when you run your app and try to click on the recyclerview? – Code-Apprentice Dec 06 '18 at 16:50
  • can you post your hideKeyboard() as well please? – Nikos Hidalgo Dec 06 '18 at 16:52
  • also in the layout do you have the android:clickable=true for the recyclerView? – Nikos Hidalgo Dec 06 '18 at 16:54
  • You can create a custom RecyclerView and override onTouchListener where you can actually post an event to your Activity / Fragment when user touch your RecyclerView and hide the keyboard. – hardartcore Dec 06 '18 at 16:57
  • @Code-Apprentice Nothing happens when i click on that, keyboard doens't hide and i don't get that log that's indside. – Wiktor Dec 06 '18 at 17:07
  • @NikosHidalgo I do have clickable set to true in my recyclerView – Wiktor Dec 06 '18 at 17:07
  • 1
    Have a look here: https://stackoverflow.com/a/38257484. For simple `RecyclerView` items – i.e., items that aren't handling touches themselves, like with `Button`s or swipe listeners – the first example should be sufficient. You could also do that with a `SimpleOnItemTouchListener` – which I wasn't aware of, at the time – overriding its `onInterceptTouchEvent()`. – Mike M. Dec 07 '18 at 00:49
  • 1
    @MikeM. Yeah, that works exactly like i want it to, thanks. I've ended up using second option because first one gives warning about `performClick()` which from what i've read affects blind people. Second one calls it, so it should be fine right? Or am i missing something here? – Wiktor Dec 07 '18 at 15:46
  • 1
    Oh, right. No, that's perfectly fine. I wrote that a while ago, before that warning even existed, I think. Glad it worked for you. Cheers! – Mike M. Dec 07 '18 at 19:14

2 Answers2

0

Override onTouchListener() to your RecyclerView and call the hideKeyboard() method from it.

  • This works for click event, but when i override it, scrolling on RecyclerVeiw stops working – Wiktor Dec 06 '18 at 17:36
0

You can try this:

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

It works for me in other cases, i didn't tried with a RecyclerView yet