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
dynamically?
I thought I could just start another Activity
and have the Play Screen extend Game
. And the new Activity
would intialize
the Play Screen. But it appears that you can't call startActivity
or the like, obviously, as LibGDX
is platform independent.
I use Gdx.graphics.getWidth()
and Gdx.graphics.getHeight()
to align Sprites
and Actors
, so it looks "right" on different devices.
I've also tried swapping the Gdx.graphics.getWidth()
and Gdx.graphics.getHeight()
, respectively, but can't seem to get the position of sprites and actors correct.
Also, in my manifest I have orientation="landscape"
.
Here are some images that should make more sense.
So this is my Main Menu in landscape
:
![](https://www.manongdao.com/static/images/pcload.jpg)
And this is my Main Menu in portrait
:
![](https://www.manongdao.com/static/images/pcload.jpg)
The position and size of Actors
and Sprites
are all based on Gdx.graphics.getWidth()
and Gdx.graphics.getHeight()
. The only thing that changes between the two images is orientation="landscape"
and orientation="portrait"
in my manifest.
Now I want my Main Menu in portrait
but the Play Screen, where the game is on, should be in landscape
.
I initially started working on the Play Screen and the sizes and positions where set based on orientation="landscape"
, and now that I am working on the Main Menu it is based on orientation="portrait"
. Hence why I want to change the orientation dynamically.
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.
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;
public class AndroidLauncher extends AndroidApplication {
@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration();
PlatformAndroid platform = new PlatformAndroid();
platform.setActivity(this);
initialize(new MyGdxGame((Platform) platform ), cfg);
}
}
PlatformAndroid implements Platform. These two classes are made to access platform specific objects. Let's look at them :
public interface Platform {
public void SetOrientation(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;
public class PlatformAndroid extends AndroidApplication implements Platform {
private Activity activity;
@Override
public void SetOrientation(String string) {
if (string == "landscape"){
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
}
public void setActivity(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");
I had the same problem and this solved it:
@Override
public void resize(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.
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
public interface GameOrien {
public static final int PORTRAIT=1;
public static final int LANDSCAPE=2;
void setOrientation(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.