LinearLayout OnClickListener not responding

I create my own widget by expanding LinearLayout. One element of my custom widget is a linear layout, inflated from another layout. When I install OnClickListener, it is not responding. Could you advise?

Thank!

+3
source share
2 answers

Instead of setOnClickListener use setOnTouchListener This code will work as an onclick event

YourLinearLayout.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            boolean returnValue = true;
            if(event.getAction()==MotionEvent.ACTION_UP){ //on touch release
                returnValue = false; //prevent default action on release
                //do something here
            }
            return returnValue;
        }
    });

And then add this to your LinearLayout class to catch child touch events

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    return true; //will prevent child touch events 
}
+3
source

Have you announced that LinearLayout can be clicked?

You can do this either in XML:

Android: = ""

Java-:

myLinearLayout.setClickable(true);

. StackOverflow :

onClickListener LinearLayout

+1

Source: https://habr.com/ru/post/1762660/


All Articles