1

I am creating my first Android app using this guide as a reference. Currently, I have a red button on my canvas and when the user clicks the button a boolean (green) will be set to true in order for the button's bitmap to a green button.

That part of the application works, however it works regardless where the user clicks on the canvas. I only want the boolean to be changed when the user clicks on the button's bitmap. Here is what I currently have in my code:

The onTouchEvent() method

    @Override
public boolean onTouchEvent(MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN) {

        button.handleActionDown((int)event.getX(), (int)event.getY());

        if (button.isTouched()) {
            green = true;
        }
    } if (event.getAction() == MotionEvent.ACTION_MOVE) {

    } if (event.getAction() == MotionEvent.ACTION_UP) {
        if (button.isTouched()) {
            green = false;
            button.setTouched(false);
        }
    }
    return true;
}

The handleActionDown() Method

    public void handleActionDown(int eventX, int eventY) {
    if (eventX >= (x - bitmap.getWidth() / 2) && (eventX <= (x + bitmap.getWidth()/2))) {
        if (eventY >= (y - bitmap.getHeight() / 2) && (eventY <= (y + bitmap.getHeight()/2))) {
            setTouched(true);

        } else {
            setTouched(false);
        }
    } else {
        setTouched(false);
    }
}

Can anybody see what I am missing in order for the ACTION_DOWN event to make it so it only triggers when the bitmap's bitmap is touched?

Regards

Ollie
  • 36
  • 4
  • When code behaves in a way contrary to what you expect, a good first resort can be to create some debug output - put some logging in your handleActionDown(), specifically eventX vs x and event Y vs y. Or even display these numbers on the screen. You might end up finding something simple such as x and y not being initialized. Or maybe your bitmap doesn't have the dimensions you think it does, or... – Chris Stratton Jun 04 '12 at 16:29
  • Seems to me if all you are trying to do is create a button with a down state and an up state that you could do this simply within a button xml file. – trumpetlicks Jun 04 '12 at 16:31
  • Are you really trying to create your own custom Button recognition? Because it seems that your onTouchEvent is being implemented on your top level view. – trumpetlicks Jun 04 '12 at 16:32
  • Try this for implementing your multi-state buttons. http://stackoverflow.com/questions/2604599/android-imagebutton-with-a-selected-state – trumpetlicks Jun 04 '12 at 16:35
  • Still having issues. I tried the above method but it force closes my application. All I am wanting to do is draw a button onto my canvas and when it is clicked the button switches to the green sprite instead of the red sprite. – Ollie Jun 07 '12 at 15:23

1 Answers1

0

Avoid using onTouchEvent with buttons, it is better to use onClick because the OS detects whether or not a specific button is clicked instead of you having to calculate the position of a button

First, set the onClickListener

btn.setOnClickListener(this);

This assumes the current class implements View.OnClickListener, so if you don't implement it you either have to use a different class or create an anonymous inner class.

Then, in the onClick method, make sure the ID matches and add whatever action you want to do inside an if-statement (or alternatively a switch statement)

@Override
public void onClick(View v) {

    if(v.getId() == R.id.btn1){
        //do whatever you want on press
    }
}
Zoe
  • 27,060
  • 21
  • 118
  • 148