The Blog

Discerning if the user is using your android app from a phone, tablet or even Google Tv.

24 Ago 12

At some point every Android developer will need to develop an app that works ( exploiting each device functionalities ) on both Phones and Tablets ( or even Google TV ).

At the very beginning the first question that pops up in the dev mind is ( at least this was the question that popped out in mine ):

Should I make a single app version that works on both android phones and tablets? Or should i leverage the Google Play Store console benefits and upload different version of the same app optimized per each device?

Lets figure it out using a PRO/CONS approach

Single App

PROs (against to the multiple Apps):

  • Less code to maintain — when making a modification to some device agnostic code you don’t need to replicate it anywhere
  • Much more scalable

CONs:

  • You’ll be required to handle all the logic that routes specific device activities.

Multiple App

PROs (against to the Single App approach):

  • All the routing staff is made upfront from the Google Play store + AndroidManifest filtering.

CONs:

  • More code to maintain  — Fixing a bug on some shared code will require you to make 2+ app updates.

In this post i’ll cover how to understand if the used device is a tablet, a phone or a google tv. This is mostly useful for the Single App approach ( but in some cases could be helpful for in the Multiple App ).

The goal (yes again) is to have some piece of code that will help you understand if the device is a phone, tablet or a google tv.

I started googling and I found a couple of valuable resources on stackoverflow which helped a bit. But I didn’t find a complete and elegant solutions in none of them so I started my own research.

I needed to find an open source app that fullfilled the above mentioned prerequisites. It was surprisingly simple since Google, each year, releases the source code of their Google IO app. ( which uses the single app approach ).

If you never used the app I can assure you it works on both tablet and phones.

Lets talk no more and get to the real juice: Google IO Sched uses a  class called UIUtils which provides some static methods. Among of them you’ll be able to find the following methods:

  • hasHoneycomb: which returns true if the device is “at least” honeycomb apis => ics returns true
  • isTablet(Context): which returns true if the device is recognized as a tablet
  • isHoneycombTablet(Context): which returns true if both isTablet(Context) and isHoneycomb() returns true.
  • isGoogleTV(Context): which returns true if the device is a GoogleTV

Now you’ll probably wondering why there are 2 methods in order to recognize if the device is a tablet or not. Well, the answer is simple: There are tablets out there that are pre-honeycomb so you won’t be able to use the (out of the stock) Fragment apis available since Honeycomb.

Here the source code of those methods:

Now lets use this goodness to make something real. Lets suppouse you’ve 2 activities:

  • MapActivity: which is the activity that will be used for phones and eventually google tv
  • MapMultiPaneActivity: which is the activity designed specifically for tablets.
You could use the approach shown in the code below that also exploits the Facade pattern:
Warning: none of the previous code is mine. Everything was taken from the source code of Google IO Sched. BTW, I hope you enjoyed my effort of making this easy to understand/use