OpenGL ES 2: GLSurface View Showing Black Screen When SetContentView is set to an XML File

949 views Asked by At

I'm fairly new to OpenGL and my problem is when I do SetContentView("R.layout.main") nothing happens, however when I do SetContentView("mGLView")the game is displayed fine. My main.xml file contains an surface view and therefore I would of thought that it would be displayed, since main.xml has an surface view.

I suspect a way to fix this, will be done with addview or findviewbyid, however my implication of this have failed.

Relevent MainActivity Code:

public class OpenGLES20Activity extends Activity {
private GLSurfaceView mGLView;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mGLView = new MyGLSurfaceView(this);
    setContentView(R.layout.main);

main.xml code:

    <FrameLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

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

            <SurfaceView 
android:id="@+id/SurfaceView" 
android:layout_width="500dp" 
android:layout_height="478dp">
</SurfaceView>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/Timer"
                android:id="@+id/Timer"/>
        </RelativeLayout>

    </FrameLayout>

Any help is appreciated.

1

There are 1 answers

6
Odney On BEST ANSWER

You need to set your extended GLSurfaceView instead of just SurfaceView in the xml file so just change SurfaceView to com.example.yourapp.MyGLSurfaceView:

<com.example.yourapp.MyGLSurfaceView 
    android:id="@+id/SurfaceView" 
    android:layout_width="500dp" 
    android:layout_height="478dp"/>