Note that there are some explanatory texts on larger screens.

plurals
  1. POClickListener and TouchListener don't work on a child view
    text
    copied!<p>I have 2 custom Views: 1. BoardView 2. BallView. I add The BallView to the BoardView and the BoardView to my layout.</p> <p>When I click on the BallView with ClickListener and TouchListener nothing happens. As if the BoardView overlaps the BallView area.</p> <p><em><strong>Code:</em></strong></p> <pre><code> board = new BoardView(this); ball = new BallModel(100, 200,50,screenWidth, screenHeight); ballView = new BallView(this, ball); ballView.setOnTouchListener(new OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { System.out.println("Hey"); return true; } }); ballView.setOnClickListener(new OnClickListener() { public void onClick(View v) { System.out.println("Hey"); } }); board.addView(ballView); </code></pre> <p><strong>Updated:</strong></p> <pre><code>public class BoardView extends View { private List&lt;View&gt; mShapes = new ArrayList&lt;View&gt;(); public BoardView(Context context) { super(context); } public void addView(View view) { mShapes.add(view); } @Override public void onDraw(Canvas canvas) { super.onDraw(canvas); for(int i=0; i&lt;mShapes.size(); i++) { View view = mShapes.get(i); view.draw(canvas); } } } public class BallView extends View { BallModel mBall; Paint mBallPaint; public BallView(Context context, BallModel ball) { super(context); mBall = ball; } private Paint getPaint() { if(mBallPaint == null) { mBallPaint = new Paint(); mBallPaint.setColor(mBall.getColor()); } return mBallPaint; } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); Paint paint = getPaint(); paint.setColor(mBall.getColor()); canvas.drawCircle(mBall.getX(), mBall.getY(), mBall.getRadius(), paint); } } </code></pre> <p>Any ideas?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload