Help
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Copilot Lvl 3
Message 1 of 3

GLSurfaceView onTouchEvent not working at all.

Solved! Go to Solution.

I have a simple Android application with OpenGL (GLES Version 2) that draws a simple colored square and works without a problem so far. I have the GlSurfaceView set as my own custom View and the Renderer where the square is drawn.

I am confused with the onTouchEvent. I want to change the square's color to a random one every time I touch the screen. The process of setting up random values within a range for the square's color array is easy to do, but getting the touch listener to work appears not.

So far, I've been only getting results online with outdated GLES versions or the solutions I'm looking for, but they don't seem to work for me at all. First thing I tried to do is overriding the onTouchEvent in my GLSurfaceView class where my custom function in the Renderer gets called to change the square's color when there's a touch event, in this case when I simply tap on my screen. Calling the @Override onTouchEvent in GLSurfaceView didn't do anything. Then I tried setting the touch listener for the GLSurfaceView view in the Main Activity. Still nothing. It seems to be only not working for me for some reason.

 

Here's what I did. (Currently you're seeing setting the listener for the GLSurfaceView view in the MainActivity):

 

activity_main.xml

Spoiler
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <com.example.opengltest.OpenGLView
        android:id="@+id/openGLView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</FrameLayout>

 

MainActivity.java

Spoiler
public class MainActivity extends AppCompatActivity {

    private OpenGLView openGLView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        openGLView = (OpenGLView) findViewById(R.id.openGLView);
        openGLView.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent e)
            {
                OpenGLRenderer mRenderer = new OpenGLRenderer();

                if(e.getAction() == MotionEvent.ACTION_DOWN)
                {
                    mRenderer.changeColor();
                }

                return true;
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        openGLView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        openGLView.onPause();
    }
}


OpenGLView.java (This is my GLSurfaceView class)

Spoiler
public class OpenGLView extends GLSurfaceView
{
    public OpenGLView(Context context) {
        super(context);
        init();
    }

    public OpenGLView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init()
    {
        setEGLContextClientVersion(2);
        setPreserveEGLContextOnPause(true);
        setRenderer(new OpenGLRenderer());
    }
}

 

OpenGLRenderer.java (My renderer class)

Spoiler
public class OpenGLRenderer implements GLSurfaceView.Renderer
{
    //... onSurfaceChanged, onSurfaceCreated, onDrawFrame, ...

    // This is the function called to change the square's color. I tried putting it inside onDrawFrame to change the color on every new frame and it works without a problem.
    public void changeColor()
    {
        mySquareColorData[0] = (float) ThreadLocalRandom.current().nextDouble(0.0, 1.0);
        mySquareColorData[1] = (float) ThreadLocalRandom.current().nextDouble(0.0, 1.0);
        mySquareColorData[2] = (float) ThreadLocalRandom.current().nextDouble(0.0, 1.0);
    }
}

I sure hope I haven't missed something. I only gave you the necessary information. Everything GLES related is working except the onTouchEvent.

Thanks in advance!

2 Replies
Commander Lvl 1
Message 2 of 3

Re: GLSurfaceView onTouchEvent not working at all.

You might check to see if there is a problem with touch mode. Try calling 

isInTouchMode()

in the view to confirm that touch is enabled.

 

It's possible (see this) your onTouch override in the activity needs to read as so:

    @Override
    protected boolean onTouchEvent(MotionEvent event) {
        Log.i("motion", event);
        return true;
    }

Would a click listener do the trick?

 

 

Please follow-up to let us know how you made out. For good karma, mark a reply as the answer if it helped!

Please follow-up to let us know how you made out. For good karma, mark a reply as the answer if it helped!

Solution
Copilot Lvl 3
Message 3 of 3

Re: GLSurfaceView onTouchEvent not working at all.

Hello, sorry for the late reply. I have solved my problem.

The renderer class should implement the GLSurfaceView class that I have made instead.

public class OpenGLRenderer implements GLSurfaceViewOpenGLView.Renderer