GLSurfaceView onTouchEvent not working at all.

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):


<FrameLayout xmlns:android=“



public class MainActivity extends AppCompatActivity {

private OpenGLView openGLView;

protected void onCreate(Bundle savedInstanceState) {

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

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

            if(e.getAction() == MotionEvent.ACTION_DOWN)

            return true;

protected void onResume() {

protected void onPause() {

} (This is my GLSurfaceView class)

public class OpenGLView extends GLSurfaceView
public OpenGLView(Context context) {

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

private void init()
    setRenderer(new OpenGLRenderer());

} (My renderer class)

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!

1 Like

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


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:

    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!


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<font color="#800000"><strike>GLSurfaceView</strike></font><font color="#339966">OpenGLView</font>.Renderer