In my activity, I display a camera preview and a button (a view
that looks like a button) that I draw over it. The problem is that when I set the click listener on that View, no matter where I click on the screen (even on the camera preview) the click event is fired.
This is the code :
FrameLayout preview = (FrameLayout) findViewById(id.camera_preview);
preview.addView(mCameraPreview);
CameraButtonView cameraButton = new CameraButtonView(getBaseContext());
preview.addView(cameraButton);
cameraButton.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Intent intent = new Intent(CameraTestActivity.this, MenuActivity.class);
startActivity(intent);
finish();
}
});
So the FrameLayout
is the Camera Preview. The CameraButtonView
is the view that gets drawn on it (somewhere at the bottom .. it doesn't take much space) and the listener is really set on the view.
Why is it that when I click anywhere on the FrameLayout
my event gets triggered ? Also, how could I fix it so that only the button is subscribed to the event.
Camera Button View :
public class CameraButtonView extends View
{
Paint buttonPaint = new Paint();
Paint textPaint = new Paint();
SizeF size = new SizeF(500, 125);
public CameraButtonView(Context context)
{
super(context);
buttonPaint.setColor(Color.RED);
textPaint.setColor(Color.BLACK);
textPaint.setTextSize(35);
}
@Override
public void onDraw(Canvas canvas)
{
RectF surface = RectangleHelper.create(0, 0, canvas.getWidth(), canvas.getHeight());
PointF alignedPoint = RectangleHelper.align(surface, size, ContentAlignment.BottomCenter, AlignmentType.Internal);
RectF content = RectangleHelper.create(alignedPoint, size);
Rect textRectangle = new Rect();
String buttonText = "Click to take a pic.";
buttonPaint.getTextBounds(buttonText, 1, buttonText.length(), textRectangle);
PointF alignedTextPoint = RectangleHelper.align(content, new SizeF(textRectangle.width(), textRectangle.height()), ContentAlignment.MiddleLeft, AlignmentType.Internal);
canvas.drawRect(content, buttonPaint);
canvas.drawText(buttonText, alignedTextPoint.x, alignedTextPoint.y, textPaint);
}
}