0

I have a main FrameLayout and a collection of balls 5 by 7 (each a View) under it. I added onTouchEvent() to switch colors. But regardless which ball I click, only the bottom right ball responds to my touch event (it was the last one to be drawn). Is there a way to get each ball recognize its own touch event?

A thought: I think this might be happening because I'm inserting each Ball (a View) into the main FrameLayout which is supposed to show only one thing at a time. I think it's like a stack of Views and only the top one is responding. Any ideas?

in the Ball class

public class Ball extends View {
    private final Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    ...
}

protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    canvas.drawCircle(x, y, r, mPaint);
}

public boolean onTouchEvent(MotionEvent event) {
    int eventAction = event.getAction();

    if(eventAction == MotionEvent.ACTION_DOWN) {
        switchcolor();
        this.postInvalidate();
        return true;
    } else {
        return false;
    }
}

private void switchcolor() {
    mPaint.setColor(touched ? 0xFFFFFFFF : 0xFFA606E2);
    touched = !touched;
}

in main Activity

FrameLayout main = (FrameLayout) findViewById(R.id.main_view);
...

some loop {
    main.addView(new Ball(this, x, y, radius - margin));
}
chizzle
  • 4,142
  • 5
  • 18
  • 18
  • That's all well and good, but where's your touch listener code? – dmon Jun 02 '11 at 00:06
  • @dmon oops I forgot. it's up there now. :) – chizzle Jun 02 '11 at 00:24
  • Errrr... and `switchcolor()`? Basically everything we need to follow the flow through and through. – dmon Jun 02 '11 at 00:27
  • @dmon sry 'bout that. let me know if u need anything else. – chizzle Jun 02 '11 at 00:33
  • Hmmm I believe (disclaimer: I might be wrong since there's quite a bit of code missing still, and I have yet to build a completely custom `View`), that you need to check if the touch event lies within your ball's bounds. I bet that if you return false in `onTouchEvent()` ALL of your balls would switch colors. – dmon Jun 02 '11 at 00:37
  • @dmon you're absolutely right. returning false from `onTouchEvent()` surely toggles all the balls. Any tips for boundary checking? I've never done it b4 – chizzle Jun 02 '11 at 03:44
  • Use trigonometry to find if the distance between the center of the circle and your point is less than the radius (see [this answer](http://stackoverflow.com/questions/481144/how-do-you-test-if-a-point-is-inside-a-circle)). – dmon Jun 02 '11 at 12:22

2 Answers2

0

As you are creating custom builds, just create a field called last one or something like that... and change the color of the ball just if that is the last one:

some loop {
    Ball ball = new Ball(this, x, y, radius - margin);
    if(this is the last one)
        ball.setLast(true);
    else
        ball.setLast(false);
    main.addView(ball);
}

Then, inside the Ball class in its onTouchEvent:

if(isLast()){
    // do it
}
Cristian
  • 198,401
  • 62
  • 356
  • 264
  • Thank you for your reply. I'm not sure if I described my problem clearly. I want the each 35 balls to recognize its own touch. Currently, regardless of which ball I click only the last one will change color even if I clicked on some other ball LOL! – chizzle Jun 01 '11 at 23:46
0

solved with 2 LinearLayout(s).

see here for a good answer. I tried both answers one using LinearLayout(s) and the other, TableLayout. The one using LinearLayout(s) will keep the cells the same size regardless of the text length

Community
  • 1
  • 1
chizzle
  • 4,142
  • 5
  • 18
  • 18