Question

At the beginning I set the content view to a new class, which extends SurfaceView (see the example).

RenderView surfaceView = new RenderView();
setContentView(surfaceView);

But I cannot seem to be able to add a layout from xml file, because inflating requires the first layer that I am adding something on to be View and throws me an error, when I try to inflate like I would do a normal way.

So the question is, how to add some layout from .xml file on top of SurfaceView?

This is the important part of the code of RenderView class:

public class RenderView extends SurfaceView implements Runnable {

Context context;
Thread thread = null;

SurfaceHolder holder;
boolean running = false;

public RenderView(Context context) {
    super(context);
    this.context = context;
    holder = getHolder();
}

public void run() {
    while (running) {
        if(!holder.getSurface().isValid()) continue;
        Canvas c = holder.lockCanvas();


        holder.unlockCanvasAndPost(c);
    }
}
Was it helpful?

Solution

Call this method in your Activity:

http://developer.android.com/reference/android/app/Activity.html#addContentView(android.view.View, android.view.ViewGroup.LayoutParams)

The View view parameter can be obtained by inflating your XML layout file

LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
View secondLayerView = LayoutInflater.from(this).inflate(R.layout.my_layout, null, false);
addContentView(secondLayerView, lp);

OTHER TIPS

If you want it all in xml, surround the surface view in a frame layout. I've done this in my game and I've overlayed a top bar with score and time on it:

<LinearLayout
    android:id="@+id/topBar"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/scoreText"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="#F7F7F7"
        android:ems="10"
        android:gravity="center"
        android:paddingLeft="5dp"
        android:textColor="#000000"
        android:textSize="20sp" >
    </TextView>

    <TextView
        android:id="@+id/timeText"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="#F7F7F7"
        android:ems="10"
        android:gravity="center"
        android:paddingLeft="5dp"
        android:textColor="#000000"
        android:textSize="20sp" >
    </TextView>
</LinearLayout>

Change/tweak the heights and positioning etc to your needs.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top