Search code examples
javaandroidlibgdx

How to set home screen in portrait and other screens in landscape in android Libgdx?


I want to set home screen(first screen of app) in portrait mode and other screens in landscape mode using libgdx in android. If I change manifest file then either it will apply that mode to all screen or it will change according to rotation of screen. I do not want that. i want Home screen in portrait(fixed) and others in landscape mode(fixed). Please help me.


Solution

  • You can use a hadler. and make your androidclass implements that handler and change orientation accordingly

    make an interface name it CustomHandler(edit: custom handler class goes in your core module) like this:

    public interface CustomHandler {
    public void setOrientationPortrait();
    public void setOrientationLandscape();
    }
    

    now make your android class implements CustomHandler like this

    public class AndroidLauncher extends AndroidApplication implements CustomHandler {
    
        @Override
        public void setOrientationPortrait(){
            this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }
    
        @Override
        public void setOrientationLandscape(){
        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        }
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            androidPref = getSharedPreferences("MyPref", Context.MODE_PRIVATE);
    
            View gameView = initializeForView(new GameClass(this), config);
            setContentView(gameView);    
    }
    

    and in your GameClass that is in core module

    simply call whenever you want to change orientation

    public static CustomHandler handler;
    
    public GameClass(CustomHandler handler){
        this.handler = handler;
    }
    

    then on any screen or stage

    call it like this

    GameClass.handler.setOrientationPortait();
    //or
    GameClass.handler.setOrientationLandscape();