How can I draw text using Libgdx/Java?

I don’t see much reason of creating separate batch for text drawing. Using gdxVersion = ‘1.4.1’ (built with gradle in Android Studio) that code draws text successfully:

BitmapFont font = new BitmapFont(); //or use alex answer to use custom font

public void render( float dt )
  {
    batch.setProjectionMatrix(camera.combined); //or your matrix to draw GAME WORLD, not UI

    batch.begin();

    //draw background, objects, etc.
    for( View view: views )
    {
      view.draw(batch, dt);
    }

    font.draw(batch, "Hello World!", 10, 10);

    batch.end();
  }

Note, that here you draw in game world coordinates, so if your character moves (in platformer, for example), than text will move too. If you want to see text, that it will be fixed on screen, something like Label/TextField or how it is called in different UI frameworks, than I recommend to use Stage (and TextArea for text), see for example on how to use Stage here: http://www.toxsickproductions.com/libgdx/libgdx-basics-create-a-simple-menu/

Leave a Comment

tech