LibGDX Tutorial - Change orientation to landscape - hide Android virtual buttons (How To 2)

Edit:: As of 18/05/2019 the following line

<uses-sdk android:minSdkVersion="9" android:targetSdkVersion="28" />

has been removed from the  "AndroidManifest.xml" file.

Original Post

In the second part of the How To tutorials we are going to hide the Android virtual buttons and change the orientation of our game to landscape. After that, we are going to place the logo i spoiled in the How To 1 tutorial as our games first screen. First of all, download the image below. You can use it for the purpose of these tutorials. Obviously, you can create your own unique logo and use that if you wish.

Note;; To download the image at the original resolution you must first click on the image and then download it.

Gamengineering logo for tutorials
Game logo

How to hide Android virtual buttons

On the top left of Android Studio click "android", then click on the "manifests" and "java" folders and finally double click "AndroidLauncher".

Android studio project files
Android studio top left view 1
Android studio project files
Android studio top left view 2

The code in AndroidLauncher class should look like this;

package com.getest.game;

import android.annotation.TargetApi;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import com.badlogic.gdx.backends.android.AndroidApplication;
import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration;

public class AndroidLauncher extends AndroidApplication {
   @Override 
   protected void onCreate (Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);      
       AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
       initialize(new GeTest(), config);
   }
}

And now we just have to add the following code;

@TargetApi(19)
private void hideVirtualButtons() {
   getWindow().getDecorView().setSystemUiVisibility(
         View.SYSTEM_UI_FLAG_LAYOUT_STABLE 
         | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION  
         | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN     
         | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY        
         | View.SYSTEM_UI_FLAG_FULLSCREEN           
         | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
}

@Override
public void onWindowFocusChanged(boolean hasFocus) {
   super.onWindowFocusChanged(hasFocus);  
   if (hasFocus) {
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
         hideVirtualButtons();    
      }
   }
}

  • The hideVirtualButtons method tells the device to stop showing the virtual buttons (as the name suggests). 
  • The onWindowFocusChanged method tells the device to hide them even if for example we minimize the game, lets say someone called us and we picked up the phone, and then we go back to it. It is called automatically every time the device "changes focus".
  • The onCreate method is actually called when we create the game, so we have to call hideVirtualButtons method from there. To do so add the following lines of code inside onCreate.

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
   hideVirtualButtons();
}

Now, AndroidLauncher class looks like this;

package com.getest.game;

import android.annotation.TargetApi;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import com.badlogic.gdx.backends.android.AndroidApplication;
import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration;

public class AndroidLauncher extends AndroidApplication {
   @Override
   protected void onCreate (Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);  
      AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();  
      initialize(new GeTest(), config);
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
         hideVirtualButtons();
      }
   }

   @TargetApi(19)
   private void hideVirtualButtons() {
      getWindow().getDecorView().setSystemUiVisibility(
            View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION  
                | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN       
                | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY     
                | View.SYSTEM_UI_FLAG_FULLSCREEN       
                | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);  
   }

   @Override 
   public void onWindowFocusChanged(boolean hasFocus) {
      super.onWindowFocusChanged(hasFocus); 
      if (hasFocus) {
         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            hideVirtualButtons();  
         }
      }
   }
}

How to change Android orientation to landscape

After hiding the virtual buttons, we have to change the orientation to landscape. If you have checked out Black Dodger then you probably already know that it runs in landscape orientation! Open "AndroidManifest.xml" and search for this line of code;

android:screenOrientation="landscape"

If it already is in landscape mode then everything is fine, nothing has to be changed. But if the orientation is "portrait" then simply change it to "landscape". "AndroidManifest.xml" should be like this;

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.getest.game" >
    
    <application  
        android:allowBackup="true" 
        android:icon="@drawable/ic_launcher"      
        android:label="@string/app_name"      
        android:theme="@style/GdxTheme" >    
        <activity            
            android:name="com.getest.game.AndroidLauncher"  
            android:label="@string/app_name"  
            android:screenOrientation="landscape"      
            android:configChanges="keyboard|keyboardHidden|navigation|orientation|screenSize">          
            <intent-filter>              
                <action android:name="android.intent.action.MAIN" />          
                <category android:name="android.intent.category.LAUNCHER" />          
            </intent-filter>       
        </activity>   
    </application>
</manifest>

How to change badlogic logo

Now, for the last step of this tutorial, rename the image you downloaded to "splashScreen.png". Go to C:\Users\user\AndroidStudioProjects\Getest\android\assets and there you will see the badlogic logo. This is the the place we are going to put all our assets in, but we will organize them better in the upcoming tutorials. Copy our "splashScreen.png" image in there.

It is time to open the actual libGDX classes. On the top left of Android Studio click "core", then click on the "java" and "com.getest.game" folder and finally double click "GeTest".


Android studio project files
Android studio top left view 3


Android studio project files
Android studio top left view 4
On the code you see there just replace "badlogic.jpg" with "splashScreen.png".

@Override
public void create () {
   batch = new SpriteBatch();
   img = new Texture("splashScreen.png");
}

Run the game and you should see our logo instead of the badlogic one. If you get a messy screen like the following, don't worry. Just run the game again and it should be fixed.

LibGDX messy logo
Messy logo

Wait wasn't it fixed? Why? Well, this is most probably because we are using a 1280 x 720 image and your device has less or more resolution than that. If it has more, for example 1920 x 1080, then you probably get the whole image but there is a red margin above the logo and on the right of it. If you have less resolution, then you cannot see the whole logo! That's only natural.

In order to fix that problem we are going to use a method called Virtual resolution which we will explain further in the upcoming tutorial.


That's it for today, feel free to ask any questions in the comments below!

Comments