How to check what platform libGDX is running on. Mobile or Desktop?

In libGDX, it’s possible to write one set of code and build for one of many targets. Desktop, Mac, Android, iOS – etc.

It’s versatile and saves time, but there are times where it really does make sense to have one set of rules for one platform, and another for another.

Some differences between the platforms could be:

  • Hooking up the right inputs (Keyboard/Mouse/Controller vs Touch screen)
  • Having different layouts for mobile and desktop
  • Or changing instructions such as “Tap to begin” vs “Press any key”

So it makes sense to check which platform you’re running on.

How to check which platform libGDX is running on.

There is an easy way to do this with libGDX and I recommend building it statically into all of the apps made for multi-platform deployment. That way, you can call it from anywhere without repeating the same code.

So go on and pack this beauty or one like it into a class of your choice.

public static boolean isMobile() {
   return Gdx.app.getType().equals(Application.ApplicationType.Android)
      || Gdx.app.getType().equals(Application.ApplicationType.iOS);
}

So much for that topic, it’s back to building games.

Enjoy

 

Leave a Reply