Is there a way to programmatically find whether the device the app is installed on is a 7 inch tablet or a 10 inch tablet?
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
- How to create Circular view on android wear?
They way that Android specifies screen sizes is through four generalized sizes: small, normal, large and xlarge.
While the Android documentation states that the size groups are deprecated
Generally the size qualifier large specifies a 7" tablet. And a size qualifier of xlarge specifies a 10" tablet:
The nice thing about triggering on the the size qualifier, is that you can guarantee that your assets and code are in agreement on which asset to use or code path to activate.
To retrieve the size qualifier in code make the following calls:
Voila!
Great information, just what I was looking for! However, after trying this out I found that when using the metrics mentioned here the Nexus 7 (2012 model) reports having dimensions 1280x736. I also have a Motorola Xoom running Jelly Bean and it incorrectly reports a resolution of 1280x752. I stumbled upon this post here that confirms this. Basically, in ICS/JB the calculations using the metrics mentioned above appear to exclude the dimensions of the Navigation Bar. Some more research led me to Frank Nguyen's answer here that uses different methods that will give you the raw (or real) pixel dimensions of the screen. My initial testing has shown that the following code from Frank correclty reports the dimensions on the Nexus 7 (2012 model runnin JB) and my Motorola Xoom running JB:
Another way:
Create 2 more folders: values-large + values-xlarge
Put:
<string name="screentype">LARGE</string>
in values-large folder (strings.xml)Put:
<string name="screentype">XLARGE</string>
in values-xlarge folder (strings.xml)In code:
String mType = getString(R.string.screentype);
if (mType != null && mType.equals("LARGE") {
// from 4~7 inches
} else if (mType != null && mType.equals("XLARGE") {
// from 7~10 inches
}
The above doesn't always work when switching portrait vs. landscape.
If you are targeting API level 13+, it is easy as described above -- use Configuration.smallestScreenWidthDp, then test accordingly:
Otherwise, if you can afford this, use the following method which is a very accurate approach to detect 600dp (like 6") vs. 720dp (like 10") by letting the system tell you:
1) Add to layout-sw600dp and layout-sw720dp (and if applicable its landscape) an invisible view with proper ID, for example:
For 720, on layout-sw720dp:
For 600, on layout-sw600dp:
2) Then on the code, for example, the Activity, test accordingly:
I have two android device with same resolution
Device1 -> resolution 480x800 diagonal screen size -> 4.7 inches
Device2 -> resolution 480x800 diagonal screen size -> 4.0 inches
It gives both device diagonal screen size -> 5.8
the solution to your problem is..
see details here..