I have a view
and I want to show it when I click on button/layout and hide it when I touch somewhere else. How can I do it?
I wrote some code in dispatchTouchEvent(Motion Event)
and it's working. But, I think there must be another way to do it.
Asked
Active
Viewed 2,021 times
1

Maria Halushko
- 13
- 5
-
What do you mean with "view with RecyclerView"? Do you mean a item of the recyclerView? – SpiralDev Oct 10 '16 at 14:27
-
It means RecyclerView inside view, in this context I think it doesn't matter – Maria Halushko Oct 10 '16 at 14:31
-
checkout my answer on another question: https://stackoverflow.com/questions/6685690/android-make-view-disappear-by-clicking-outside-of-it/62009387#62009387 – Fartab May 25 '20 at 19:33
1 Answers
0
You can fill the outside of your RecyclerView with another clickable view and implement setOnTouchListener method for that view. Here's an example:
Let's say we have got RecyclerView at the top of our RelativeLayout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?attr/selectableItemBackground"
android:clickable="true"
android:focusable="true">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="300dp"
android:scrollbars="vertical" />
<!--View below is just to fill the remaining space. We will use this view to catch outside touch-->
<View
android:id="@+id/outside_detector"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/recyclerView"
android:clickable="true"
android:focusable="true"/>
</RelativeLayout>
And we want to hide and show our recyclerview when we click outside of RecyclerView:
((View) findViewById(R.id.outside_detector)).setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
if(arg1.getAction() == MotionEvent.ACTION_DOWN){
if(recyclerView.getVisibility() == View.VISIBLE){
recyclerView.setVisibility(View.INVISIBLE);
}else{
recyclerView.setVisibility(View.VISIBLE);
}
}
return true;
}
});
If you want to show recyclerview on button click, then just write recyclerView.setVisibility(View.VISIBLE)
method inside button ClickListener!
Hope this helps!

SpiralDev
- 7,011
- 5
- 28
- 42
-
thaks, but if I'll have a lot of views in this `RelativeLayout` create one more `view` for easier intercepting events, is it correct? – Maria Halushko Oct 11 '16 at 10:14
-
-
-
yes, might have. For further information refer to [this](http://stackoverflow.com/questions/16155285/how-to-handle-touch-outside-the-view-in-android) and [this](http://stackoverflow.com/questions/6685690/android-make-view-disappear-by-clicking-outside-of-it) links – SpiralDev Oct 11 '16 at 15:21
-
You are welcome! :) As far as I can see you speak russian language well. What if I help you with ur programming (plus with ur english), and, in return, you help me with my russian. – SpiralDev Oct 13 '16 at 10:34
-