views:

116

answers:

1

Hello, I'm trying to draw a shape on a Canvas and get information about the clicks that the user performs on that Canvas.

I created a class which extends the Button class.

  class CustomDrawableButton extends Button {
    private final ShapeDrawable mDrawable;
    int x = 50;
    int y = 50;
    int width = 30;
    int height = 30;

    public CustomDrawableButton (Context context) {
       super(context);
       mDrawable = new ShapeDrawable (new OvalShape ());
       mDrawable.getPaint().setColor(Color.GREEN);
       mDrawable.setBounds(x, y, x + width, y + height);
    }

    protected void onDraw(Canvas canvas) {
      mDrawable.draw(canvas);
    }
  }

Then, in a class which also extends View, I added the instantiation and a listener:

  CustomDrawableButton mCustomDrawableButton = new CustomDrawableButton(getBaseContext());

  layout.addView(mCustomDrawableButton);

  mCustomDrawableButton.draw(canvas);

  mCustomDrawableButton.setOnClickListener(new View.OnClickListener() {
    public void onClick(View view) {
      System.out.println("Yey clicked");
     Toast.makeText(view.getContext(), "Yey", Toast.LENGTH_LONG).show();
    }});

When this is executed, it is not able to detect the clicks on the image / button. I have read that ShapeDrawable objects can't be "clickable", but I was expecting this to work since I'm extending the Button (which is a View and is "clickable").

Is this possible? If not this way, can you direct me to some way to get information about the screen coordinates that were pressed on a Canvas or a View ?

Thanks in advance.

A: 

After some searching here's a tutorial that shows how to do it using by getting the touched screen position.

http://marakana.com/tutorials/android/2d-graphics-example.html

Still don't know how to do it by automatically binding the touched event to a button. If anyone knows how to do it, please say so.

Thanks.

MyNameIsZero