Skip to content Skip to sidebar Skip to footer

Change Game From Portrait To Landscape Dynamically

I have multiple classes that implement Screen. I want to, say, have the Main Menu screen portrait, then the Play Screen landscape. Is there anyway of changing the orientation dynam

Solution 1:

This is a good question :) I develop noone's answer (which is correct)

How to set lanscape screen with Android sdk specific classes ?

And how to import android classes in a libgdx project ?

First of all note that Android classes can only be imported in the android project and not in the core. So there we save the Activity in a platform dependant object. Here is my AndroidLauncher:

import android.os.Bundle;

import com.badlogic.gdx.backends.android.AndroidApplication;
import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration;
import com.mygdx.game.MyGdxGame;
import com.mygdx.game.util.Platform;

publicclassAndroidLauncherextendsAndroidApplication {
    @OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        AndroidApplicationConfigurationcfg=newAndroidApplicationConfiguration();
        PlatformAndroidplatform=newPlatformAndroid();
        platform.setActivity(this);
        initialize(newMyGdxGame((Platform) platform ), cfg);
}

}

PlatformAndroid implements Platform. These two classes are made to access platform specific objects. Let's look at them :

publicinterfacePlatform {
   publicvoidSetOrientation(String string);
}

and

package com.mygdx.game.android;
import android.app.Activity;
import android.content.pm.ActivityInfo;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.backends.android.AndroidApplication;
import com.mygdx.game.util.Platform;

publicclassPlatformAndroidextendsAndroidApplicationimplementsPlatform {
    privateActivity activity;
    @OverridepublicvoidSetOrientation(Stringstring) {
       if (string == "landscape"){
       activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
       }
    }
    publicvoidsetActivity(Activity activity){ this.activity = activity;   }
}  

You can also create a PLatformDesktop called by the desktop launcher. Anyway, keep the platform variable in your classes and when you want to set the screen to landscape just call:

 platform.SetOrientation("landscape");

Solution 2:

You should not have multiple Activities. To learn how to switch the orientation programmatically you can have a look at this link.

As you said, LibGDX is platform-independent. That's why you cannot access your Android-Activity from your "core" code. To learn how to workaround that, have a look at Interfacing with platform specific code.

When you call your interface to change the orientation from your game when doing a Screen transition, LibGDX will automatically call resize(width, height) with the new values and you can align your Stage and so on according to the new values.

Solution 3:

I had the same problem and this solved it:

@Overridepublicvoidresize(int width, int height) {
    stage.getViewport().setScreenSize(width, height);
}

For some reason, even though the screen resolution changed, the stage didn't know that. So I've told it to the stage.

Solution 4:

Just create a interface in core project and and declare a method with any name than implement that interface in your AndroidLauncher class. Give definition of your method.

For me GameOrien.java is interface having method declaration

publicinterfaceGameOrien {

    publicstaticfinalint PORTRAIT=1;

    publicstaticfinalint LANDSCAPE=2;

    voidsetOrientation(int orientation);
}

In AndroidLauncher.java class which is in Android project.

public class AndroidLauncher extends AndroidApplication implements GameOrien {

**...**

    @Override
    public void setOrientation(int orientation) {

        if(orientation==PORTRAIT)
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        else if(orientation==LANDSCAPE)
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    }

}

When you want to change orientation for GamePlay just call setOrientaion() method of interface. Your orientation changed.

Post a Comment for "Change Game From Portrait To Landscape Dynamically"