Reputation: 100458
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.
Upvotes: 1
Views: 2069
Reputation: 37516
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.
Upvotes: 3
Reputation: 8511
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.)
Upvotes: 0