1

You can check if your app is running on different versions of Android using the following:

int sdk = Build.VERSION.SDK_INT;
if (sdk <= Build.VERSION_CODES.ECLAIR){
    //do stuff
} else {
    //do some other stuff
}

Is there such a check for tablet use? Currently I have two ways to find out that I don't like:

Configuration configuration = getResources().getConfiguration();
if (configuration.orientation == Configuration.ORIENTATION_LANDSCAPE) {
   try {
       return configuration.screenWidthDp >= 1024; 
    } catch (NoSuchFieldError ex) {
        return false;
    }
} else {
    return false;
}

And, if I use a different layout file with different view ids:

return findViewById(R.id.mytabletlinearlayout) == null;

Is there something like the version codes available for this? Or is there another more elegant solution?

Edit

For some clarity, I need to perform some different actions if I'm running my app on a tablet, because then I am using a multi-pane layout.

nhaarman
  • 98,571
  • 55
  • 246
  • 278
  • Some clarification: Are you looking to see _if_ your app _can run_ on a particular tablet? Or _if_ it _is running_ on a tablet? – NominSim Jul 02 '12 at 20:50
  • If it currently is running on a tablet. If it is running on a tablet I need to call some other methods than the methods I would call on a smartphone. – nhaarman Jul 02 '12 at 20:52

2 Answers2

3

Rather than attempting to detect tablets, consider detecting screen sizes and detecting features that the device supports, since the difference between a phone and a tablet isn't exactly well defined (and subject to change).

To detect a feature, use the PackageManager class, specifically hasSystemFeature or getSystemAvailableFeatures. For detecting screen sizes, your second approach of sniffing changes in the layouts in your different folders is an appropriate way to handle it.

If you check available features at runtime, you won't be forced to make generalizations about tablets ahead of time, like assuming they don't have a back facing camera, or other features like that.

wsanville
  • 37,158
  • 8
  • 76
  • 101
  • +1 For "consider detecting screen sizes and detecting features that the device supports". – NominSim Jul 02 '12 at 20:55
  • I'm not really using the difference of features, but indeed rather the screen size for a multi-pane layout. The snippet I provided in my question is ugly, because the `screenWidthDp` field does not exist in older Android versions. – nhaarman Jul 02 '12 at 21:07
  • Since you're just looking to branch based on screen sizes, I usually prefer your option of just testing for non-null views in your layouts that correspond to each screen size. – wsanville Jul 02 '12 at 22:09
0

To check if it is running on a tablet this answer should be sufficient. Bear in mind though that you may be asking the wrong question. Your program should be more worried about the specific characteristics of the device that you are using. (e.g. Screen Size, Memory Capabilities, etc.)

Community
  • 1
  • 1
NominSim
  • 8,447
  • 3
  • 28
  • 38