Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android drawing button to canvas with custom view?

How can I draw a button on top of the canvas in a custom view? (Preferably on the mid-right side) Is there something I have to call before doing the button.draw(canvas)?

    public class MyClass extends View {
    public Simulation(Context context) {
            super(context);
            pauseButton.setText("TestButton");
            pauseButton.setClickable(true);
            pauseButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
            Log.i(TAG, "Button Pressed!");
            }
            });
    public onDraw(Canvas canvas) {
           super.onDraw(canvas);
           pauseButton.draw(canvas);
    }
    }

Thanks for your time

like image 944
unknownone Avatar asked Aug 13 '11 02:08

unknownone


1 Answers

You cannot insert a button into canvas. Canvas is an interface for bitmap or a bitmap buffer for a view. You can only draw other bitmap or pixels in it, not insert an object or a widget.

There are some solutions:

  1. as Nikolay suggested, use a FrameLayout and create two layers (views), first your custom view and the second LinerView or RelativeView, which will come on top, where you can have buttons etc

  2. draw an image of a buttun on Canvas then use onTouchEvent in your custom view and test for the coordinates of the touch, then do something... an example for onTouchEvent here: Make certain area of bitmap transparent on touch

like image 123
Lumis Avatar answered Sep 20 '22 01:09

Lumis