3

I'm creating a custom widget by expanding LinearLayout. One of the elements in my custom widget is a linear layout, inflated from another layout. When I set the OnClickListener it is not responding. Can you please advise?

Thanks!

marci
  • 31
  • 1
  • 2

3 Answers3

3

Instead of using setOnClickListener use setOnTouchListener This code will work as a 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 intercept child touch events

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    return true; //will prevent child touch events 
}
Majid K
  • 43
  • 5
1

Have you declared the LinearLayout clickable?

You can do this either in the XML:

android:clickable="true"

or in Java code:

myLinearLayout.setClickable( true );

Also see the other StackOverflow thread on this question:

onClickListener on a LinearLayout

Community
  • 1
  • 1
mportuesisf
  • 5,587
  • 2
  • 33
  • 26
1

Make sure you put all childs inside LinearLayout to android:clickable="false". Than myLinearLayout.setClickable( true ) works for me.

Wolfgang
  • 21
  • 1