4

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 );
Beryllium
  • 12,808
  • 10
  • 56
  • 86
user1730694
  • 442
  • 4
  • 14
  • 1
    what do you mean with background? Actually all you do is just clear the screen with no colour. If you want something to be transparent you need something thats the background and shining through. You cant let the window and its content be transparent so you see the desktop! Please give us a bit more informations what do you want to do ? – bemeyer Jul 31 '13 at 09:27
  • You can use `.setAlpha(150);` – Kartheek Sarabu Jul 31 '13 at 09:39

2 Answers2

13

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);
}
user1730694
  • 442
  • 4
  • 14
  • worked as a fragment too, if your fragment extends AndroidFragmentApplication and on onCreateView you apply the later part on the view you just initializedForView ... worked for me :) don't forget NOT to do a Gdx.gl.glClearColor on your render – Marc Nov 28 '18 at 02:05
  • 1
    how to add other android view on top of glView after set "setZOrderOnTop(true);"? – diousk Feb 11 '19 at 10:03
1

Think it this way - transparency is visible when you have at least two things. If you have a black background and then draw a white one on top of it with transparency say 50%, you will see black background through your white layer. Now, at the start you have the screen. It can be of any color. Under that screen, there's nothing. So, if you need transparency, draw something on top of it with alpha channel.

Sajal Dutta
  • 18,272
  • 11
  • 52
  • 74