Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overlay an Android layout on a surfaceview

I am designing a simple Android game using a surfaceview similar to the Lunar Lander sample provided by Google. I want to have various things such as highscores, messages, menus etc pop up on screen with the surface view still in the background.

The problem is that I want the "pop ups" to contain many design elements such as images, textboxes etc. Is there a way that I can create a layout in XML to design the pop up I want and then display that over the surfaceview as one object rather than having to construct each individual element one by one in code and paint them all to the canvas?

I want to animate the pop ups in from the side of the screen, so it would be good if I design the layout in XML so that all the object know their relation to each other and then animate that layout as one object.

It strikes me as something quite simple but I just cant seem to find how to achieve it.

like image 843
BGH Avatar asked Oct 18 '10 02:10

BGH


2 Answers

You can overlay elements on top of SurfaceView either in code or in your XML layouts.

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    >
<android.view.SurfaceView
        android:id="@+id/mysurface"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
</android.view.SurfaceView>
</FrameLayout>

And in your Actiivty:

TextView myText = new TextView(this);
myText.setText("Something");
addContentView(myText, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

Don't know about the performance issues though.

like image 99
Abhinav Avatar answered Sep 29 '22 10:09

Abhinav


A simple way of doing this is creating your popup interface as a different Activity. You would then give a transparent background theme to it, like the Dialog theme, in your manifest file.

Look at the CustomDialog, Translucent and TranslucientBlur samples in API Demos. They create such activites. The fact that the underlying activity is in a surface shouldn't change anything.

like image 21
Jean Avatar answered Sep 29 '22 11:09

Jean