13

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

Jack
  • 2,625
  • 5
  • 33
  • 56

2 Answers2

22

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.

David
  • 15,894
  • 22
  • 55
  • 66
svdree
  • 13,298
  • 4
  • 28
  • 21
  • My Surface View doesn't have an attribute set should that matter? – Jack Oct 26 '11 at 17:58
  • 1
    Just implementend the above says that it has trouble inflating class. Error comes from the xml file – Jack Oct 26 '11 at 18:03
  • `public class MyGLSurfaceView extends GLSurfaceView {` and the constructor `public MyGLSurfaceView(Context context){ super(context);` – Jack Oct 26 '11 at 18:15
  • But now any calls I make to my surface view (ie: `glSurfaceView.requestRender`) have stopped working =( – Jack Oct 26 '11 at 18:22
  • NullPointerException (sorry forgot to specify) – Jack Oct 26 '11 at 18:22
  • I seem to have fixed that last thing but I'm confused as to why my original life cycle seems to have gone out of the window. – Jack Oct 26 '11 at 18:33
  • Any idea why glSurfaceView.onResume would throw a nullPointer in my SurfaceView has been created and (I can only assume that my renderer has been set) No evidence of a surface being created though maybe thats the problem – Jack Oct 26 '11 at 18:36
  • 3
    Weird Its all working now. Turns out you do all you said except you need to fetch the glSurfaceView with `glSurfaceView = new MyGLSurfaceView(this);` and then set the xml file as the content view How annoying. could you update you answer to fit please. – Jack Oct 26 '11 at 18:43
2

If everything corect, like you write in the xml-layout, the full Path to the Glsurfaceview-class : (and classname)

it works only if the class, GLSurfaceView, written in a own File. in this file for shure, the construktors need to be written correctly.

i read about ,1 constructor for xml-refer, and one for comunication between classes. constructor for xml-refer, and one for comunication between classes, can be found, if written correcly, inside GLSurfaceView . GLSurfaceView, is where you set the Renderer, set it in the the xml constructor, must be the only way, it works fine. (shown in anwer 1)

xml-constructor:

public MyGLSurfaceView(Context context, AttributeSet attrs) { super(context, attrs); setEGLContextClientVersion(2);
renderer = new Renderer(context); setRenderer(renderer);

If you some of this peoples, who cant get SurfaceView work, in xml-layout ore, who buy this book from Apress - Beginning 3D- Game-Development. dont be angry ore hurt yourself. At page 44-45 it be written, in one file. Write GLSurfaceView, like in my answer, in own file. Renderer is own file, where: onSurfaceCreated, onSurfaceChanged, onDrawFrame..can be found And The MainActivity

azuztekztorem
  • 117
  • 1
  • 7
  • if you realy learn gl es for android, i would buy the print from the book"Beginning And...3D" - Robert Chin, to it beside you desk , see the new bosten. – azuztekztorem May 29 '15 at 09:31