Manji
Manji

Reputation: 137

Multiple TextViews in a SurfaceView

I am creating an android game, I tried using the canvas.drawText() method to display my score and level but it caused errors. Now I am trying to use TextViews to display them, I am using a SurfaceVeiw and I wanted to know is it possible and would it be a good way of doing it.

Upvotes: 1

Views: 1681

Answers (1)

Brian
Brian

Reputation: 8085

You can't really put TextViews into a SurfaceView. What you can do instead, though, is add TextViews on top of the SurfaceView. Let me know if you need help on doing that if you don't know how.

// You can use a FrameLayout to hold the surface view
FrameLayout frameLayout = new FrameLayout(this);
frameLayout.addView(surfaceView);

// Then create a layout to hold everything, for example a RelativeLayout
RelativeLayout relativeLayout= new RelativeLayout(this);
relativeLayout.addView(frameLayout);
relativeLayout.addView(textview1);
relativeLayout.addView(textview2);
setContentView(relativeLayout);

When adding your views, it may be helpful to using LayoutParams to help organize things. Use this documentation for LayoutParams. Hope this helps!

If you're using an XML layout, I would use a RelativeLayout, as mentioned above, contain the SurfaceView inside a FrameLayout. Placing the TextView's on top of the FrameLayout is just a simple matter of configuring the RelativeLayout parameters for each view. You seem new to Android, perhaps this guide will help you with RelativeLayouts and XML layouts in general.

Upvotes: 3

Related Questions