Given that Android apps can run on different devices, there are situations where we must know if the device where the app is running on is of type smartphone or television. This allows our app to adapt itself to the running device without the must of generate different app versions for each device type.
The way to know it in Android is very simple, using the method getCurrentModeType() of the class android.app.UiModeManager.
Using it in libGDX
The way I use this in libGDX is by declaring an enum that defines the supported game modes.
1 2 3 4 5 6 7 8 | /** List of supported game modes. */ public static enum GameMode { /** With touch screen: smartphones, tablets, etc. */ PHONE, /** Without touch screen: televisions. */ TV } |
On the other hand, in the game core I have a configuration class, with different constants that define the game behavior. One of this values is the gameMode:
/** Game configuration. */ public class GameConfig { /** Game mode: TV by default. */ public static GameMode gameMode = GameMode.TV; ... } |
From the Android launcher of the game, class AndroidLauncher, it’s setted the value of the variable gameMode:
public class AndroidLauncher extends AndroidApplication { @Override protected void onCreate (Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Determines if the hardware is a television or not UiModeManager uiModeManager = (UiModeManager) getSystemService(Context.UI_MODE_SERVICE); if (uiModeManager.getCurrentModeType() == Configuration.UI_MODE_TYPE_TELEVISION) { GameConfig.gameMode = GameMode.TV; } else { GameConfig.gameMode = GameMode.PHONE; } AndroidApplicationConfiguration config = new AndroidApplicationConfiguration(); initialize(new MyGdxGame(), config); } .... } |
That’s all. Now, we can check anytime the stablished game mode and do the needed actions for each case:
... if (GameConfig.gameMode == GameMode.PHONE) { // do ... } else { // do ... } ... |
Regards!