0

so i made a Surface view with a canvas, so i could do some graphics. is there any way of having a xml layout with a canvas, so i can have buttons and what not on the screen, or is there any other option to making buttons on a canvas?

steven minkus
  • 131
  • 2
  • 2
  • 14

2 Answers2

0

Yes and Yes, You can have Button Views on the layout but they will exist outside the Canvas but they can overlay the canvas too. Or you can can render your own button directly on the surface and go through the overhead of writing your own touch detection. If you're creating a little framework for a game (or something game-like with "sprites") then the overhead is something you're already doing. Making these buttons look exactly like a Button view may be a bit difficult but it is possible.

This ultimately comes down to a design decision about what is easier or better to implement.

Dan S
  • 9,139
  • 3
  • 37
  • 48
  • I'm trying to implement my own button right now, but the problem i think im going to run into is what happens if the phone is flipped from portrait to landscape, so i might have to set the phone to one orientation, but i would rather not.... – steven minkus May 24 '12 at 20:06
  • Consider how you're solving the orientation problem for the other things in the surface and a solution should come out of that. – Dan S May 24 '12 at 21:02
0

You will have to made one class that having that canvas in it. And then Import that class in the xml layout.

See this example: FingerPaint Example.

In above example there is myView class that contain the canvas to draw on it.

What you need to do is:

First Create any Layout in your xml as like below:

<RelativeLayout
        android:id="@+id/drawingLayout"     
        android:layout_width="wrap_content"     
        android:layout_height="wrap_content">

    </RelativeLayout>

Now let say main.xml is your main xml layout file then add that class in to that layout as like below:

   setContentView(R.layout.main);

    drawingLayout = (RelativeLayout)findViewById(R.id.drawingLayout);
    System.out.println("The Layout is: "+drawingLayout);
    myView = new MyView(this);
    drawingLayout.addView(myView);

Hope you got the point. If not then let me know.

Enjoy. :))

Shreyash Mahajan
  • 23,386
  • 35
  • 116
  • 188
  • heys.. i tried your solution. But may canvas added below my xml. i want it in background and button above of it.. I also have to handle touch event of button to drag the,,..Any help is appreicated.. – SweetWisher ツ Sep 02 '13 at 05:28