Search code examples
androidopengl-esbackgroundlibgdx

Background transparency in libgdx


How can I make background of screen transparent if I use libgdx in Android?

The code I tried to use doesn't work.

Gdx.gl.glClearColor( 0, 0, 0, 0 );
Gdx.gl.glClear( GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT );

Solution

  • Just found a solution!

    Just add this code to the class that extends AndroidApplication.

    AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration();
    cfg.r = cfg.g = cfg.b = cfg.a = 8;
    
    cfg.useGL20 = false;
            
    View view = initializeForView(new LineDrawing(), cfg);
    
    if (graphics.getView() instanceof SurfaceView) {
                SurfaceView glView = (SurfaceView) graphics.getView();
                glView.getHolder().setFormat(PixelFormat.TRANSLUCENT);
                glView.setZOrderOnTop(true);
    }
    

    Don't forget to remove Gdx.gl.glClearColor from your render() method.

    For fragment, use the same code inside your onCreateView method.