Mach Mitch
Mach Mitch

Reputation: 323

setting onClickListener on views drawn on a canvas

So basically I have some image views drawn over my canvas, but when I try to set up onclick listeners and try to handle events, it doesn't work.

public class DrawView extends View implements OnClickListener{
    Paint paint = new Paint();
    RectF rf = new RectF(30, 30, 80, 80);
    BallView ball = new BallView(getContext());
    Bitmap bmp = BitmapFactory.decodeResource(getContext().getResources(), R.drawable.ic_launcher);

    public DrawView(Context context) {
        super(context);
    }

    @Override
    public void onDraw(Canvas canvas) {
        ball.setOnClickListener(this);
        ball.draw(canvas);
        canvas.drawBitmap(bmp, 110, 10, paint);

    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        Toast.makeText(getContext(), "Mock", Toast.LENGTH_SHORT).show();

    }

}

BallView.java

public class BallView extends ImageView {
Paint b = new Paint();
public BallView(Context context) {
    super(context);
    // TODO Auto-generated constructor stub
}

public void onDraw(Canvas c){
    b.setColor(Color.BLUE);
    c.drawCircle(50, 50, 40, b);
}

}

Upvotes: 2

Views: 5048

Answers (1)

sandrstar
sandrstar

Reputation: 12643

Basically the answer is: Your view is not receiving touch events, because it's not in views hierarchy, so click callback is also not called. Actually, view should be in View hierarchy (in other words, added to some upper level view and finally, to the window) in order to participate is user interactions.
So, if You want to implement somthing covering Your view, then it should be ViewGroup or shouldn't be related to view at all and be some abstract container.
E.g. using the way below click works without any issues (but You will need second view or modify BallView to draw bmp): DrawView:

public class DrawView extends FrameLayout implements View.OnClickListener {
    BallView ball = new BallView(getContext());

    public DrawView(Context context) {
        this(context, null, 0);
    }

    public DrawView(final Context context, final AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public DrawView(final Context context, final AttributeSet attrs, final int defStyle) {
        super(context, attrs, defStyle);
        addView(ball);
        ball.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        Toast.makeText(getContext(), "Mock", Toast.LENGTH_SHORT).show();
    }
}

BallView class stays the same.
Layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
    <com.ruinalst.performance.tests.views.DrawView
            android:id="@+id/oscillator"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_centerInParent="true" />
</RelativeLayout>

Upvotes: 1

Related Questions