Cavasta Cavasta - 1 year ago 225
Android Question

Libgdx viewport not stretching

I'm currently having a problem creating a stretchedViewPort in libgdx.
As you can see in the image I added the gamescreen is not stretched over the whole screen and there are black spaces. I'm wondering what I'm doing wrong.
I provided the relevant parts of the code below.

Furthermore I also tried to use asign the viewport in to the stage in the oncreate function like this: m_stage = new Stage(m_viewport); But in that case I got a black screen and nothing was rendered (tough there were no compile errors).

Can someone point out what I'm doing wrong or what I'm missing.
Thanks in advance!

Kind regards,



import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.badlogic.gdx.utils.viewport.StretchViewport;

public class GameMenu implements Screen {

private BlockIt m_blockIt;
private Texture m_btexture = new Texture(Gdx.files.internal("mainmenu1.png"));
private Image m_background = new Image(m_btexture);
private Texture m_starttexture = new Texture(Gdx.files.internal("buttons/c_start.png"));
private Image m_startGame = new Image(m_starttexture);
private Texture m_optionstexture = new Texture(Gdx.files.internal("buttons/c_controls.png"));
private Image m_options = new Image(m_optionstexture);
private Texture m_highscoretexture = new Texture(Gdx.files.internal("buttons/c_highscores.png"));
private Image m_highscore = new Image(m_highscoretexture);
private Texture m_continueTexture = new Texture(Gdx.files.internal("buttons/c_resume_game.png"));
private Image m_continue = new Image(m_continueTexture);
private Texture m_playerIcon = new Texture(Gdx.files.internal("player/player_move_front.png"));
private Image m_player = new Image(m_playerIcon);
private int m_buttonPressed = -1;

private boolean m_gameInProgress = false;

private StretchViewport m_viewport;
private PerspectiveCamera m_camera;
private Stage m_stage = new Stage();

public GameMenu(BlockIt blockIt, boolean gameInProgress) {
m_blockIt = blockIt;

m_camera = new PerspectiveCamera();
m_viewport = new StretchViewport(,, m_camera);
m_viewport.update(,, true);
m_stage = new Stage();


public void show() {

if (!m_gameInProgress) {
m_startGame.addListener(new ClickListener() {
public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
// m_blockIt.startNewGame();

public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
// switch to a new texture
/*Texture newTexture = new Texture(Gdx.files.internal("buttons/c_new_game.png"));
m_startGame.setDrawable(new SpriteDrawable(new Sprite(newTexture)));
m_buttonPressed = 0;
return true;

} else {
m_continue.addListener(new ClickListener() {
public void touchUp(InputEvent event, float x, float y, int pointer, int button) {



m_stage.addActor(m_background); //adds the image as an actor to the stage

if (m_gameInProgress) {
} else {



public void render(float delta) {
m_camera.update();, 0, 0, 1); //sets clear color to black; //clear the batch

m_stage.act(); //update all actors
m_stage.draw(); //draw all actors on the Stage.getBatch()

public void resize(int width, int height
) {

public void pause() {

public void resume() {

public void hide() {


public void dispose() {


Answer Source

You have to set the camera and viewport to the value u used to program the world: eg. If your world is 800*480:

camera = new OrthographicCamera(800,480);
fitViewport = new FitViewport(800, 480, camera);