When I click on the layout, some view should be shown, and when I click anywhere else, this view should disappear. I am writing the logic in the dispatchTouchEvent (MotionEvent ev) function that is responsible for this, can it be done in another way, are there any other options? I would be grateful for the answer.
1 answer
You can try to implement in two ways:
Create a root RelativeLayout where to place an empty view and your main view with a button (any other elements). To make a blank view match_parent and by clicking on it, make your main view disappear.
Hang on the view.setOnFocusChangeListener (). If the focus is lost from the view you need, set the logic to hide this view. It is necessary to add an attribute to the markup (including the parent elements, too, if they exist)
android: focusableintouchmod = true
- Thanks, I use the first option, and I also thought the second one, but the focus is not lost when you click on another normal
view, here you need to manage the focus yourself, if I don’t correct me. - Maria Halushko - If I understand you correctly, it turns out that the second view also needs setOnFocusChangeListener () to “grab” a new focus. For example, I use editText with focus, when I click on the button, the focus goes to the button, it is lost from editText. - Ivan Vovk
- Yes, and if you just have a
view, then you needonTouch()callrequestFocus(). - Maria Halushko
|