Using GLSurfaceView class with android xml layouts

Jack picture Jack · Oct 26, 2011 · Viewed 20.6k times · Source

I want to make use of the android xml layouts. I have put a glSurfaceView in a frame layout to use in conjunction with a linear layout like so...

<FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1">

<android.opengl.GLSurfaceView android:id="@+id/surfaceviewclass"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

</FrameLayout>

<LinearLayout android:id="@+id/gamecontrolslayout"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:layout_weight="5"
          android:background="@drawable/backdrop"> 
//some layout stuff

</LinearLayout>
<LinearLayout>

I then call my layout like so

setContentView(R.layout.main);
    GLSurfaceView glSurfaceView = (GLSurfaceView)findViewById(R.id.surfaceviewclass);

in onCreate();

How can I call my glSurfaceView so that I can make use of the xml layouts like this and also reference my own GLSurfaceView class (below is code that references my own GLSurfaceView class)...

glSurfaceView = new MyGLSurfaceView(this);
    setContentView(glSurfaceView);

Is there anyway of combining these two? I want to do this cos I've got a load of stuff going on in my glSurfaceView class like file loading and touch events. And only I've just thought about implementing this new layout

Answer

svdree picture svdree · Oct 26, 2011

Just reference your own class (with full packagename) in the xml, the same way you reference android.opengl.GLSurfaceView. Make sure that your subclass implements the proper constructor, and passes the context & attributes to the parent:

public MyGLSurfaceView(Context context, AttributeSet attrs)
{
   super(context, attrs);

Then you can fetch it using findViewById:

MySurfaceView glSurfaceView = 
             (MySurfaceView)findViewById(R.id.surfaceviewclass);

That should do the trick.