title: Android onTouchEvent for custom view
body: <p>I created a service that displays a custom <code>View</code> inside a custom <code>LinearLayout</code>. The service works fine, and the <code>View</code> is shown on the screen, but I cannot get the <code>View</code> or the <code>LinearLayout</code> to receive any touch events (I just want either one of them to receive touch events, I don't care which). This is what I have:</p>
  
  <p>MyLayout.java</p>
  
  <pre><code>public class MyLayout extends LinearLayout {
  
      public MyLayout(Context context, AttributeSet attrs, int defStyle) {
          super(context, attrs, defStyle);
      }
  
      public MyLayout(Context context, AttributeSet attrs) {
          super(context, attrs);
      }
  
      public MyLayout(Context context) {
          super(context);
      }
  
      @Override
      public boolean onInterceptTouchEvent(MotionEvent ev) {
          onTouchEvent(ev);
          return true;
      }
  
      @Override
      public boolean onTouchEvent(MotionEvent event) {
          Toast.makeText(this.getContext(), "Touched layout", Toast.LENGTH_SHORT).show();
          Log.d("TOUCH", "Touched layout");
          return true;
      }
  }
  </code></pre>
  
  <p>MyView.java</p>
  
  <pre><code>public class MyViewextends View{
  
      public MyView(Context context, AttributeSet attrs, int defStyle) {
          super(context, attrs, defStyle);
      }
  
      public MyView(Context context, AttributeSet attrs) {
          super(context, attrs);
      }
  
      public MyView(Context context) {
          super(context);
      }
  
      @Override
      public boolean onTouchEvent(MotionEvent event) {
          Toast.makeText(this.getContext(), "Touched View", Toast.LENGTH_SHORT).show();
          Log.d("TOUCH", "Touched View");
          return true;
      }
  }
  </code></pre>