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?
You can use the
DisplayMetrics
to get a whole bunch of information about the screen that your app is running on.First, we create a
DisplayMetrics
metrics object:From this, we can get the information required to size the display:
This will return the absolute value of the width and the height in pixels, so 1280x720 for the Galaxy SIII, the Galaxy Nexus etc.
This isn't usually helpful on its own, as when we're working on Android devices, we usually prefer to work in density independent pixels, dip.
You get the
density
of the screen usingmetrics
again, in the form of a scale factor for the device, which is based on the Android Design Resources formdpi
,hdpi
etc.From this result, we can calculate the amount of density independent pixels there are for a certain height or width.
The result you get from this will help you decide what type of screen you are working with in conjunction with the Android Configuration examples, which give you the relative dp for each screen size:
Using the above information, we know that if the smallest-width of the device is greater than 600dp, the device is a 7" tablet, if it's greater than 720dp, the device is a 10" tablet.
We can work out the smallest width using the
min
function ofMath
class, passing in theheightDp
and thewidthDp
to return thesmallestWidth
.However, this doesn't always give you an exact match, especially when working with obscure tablets that might be misrepresenting their density as hdpi when it isn't, or that might only be 800 x 480 pixels yet still be on a 7" screen.
Further to these methods, if you ever need to know the exact dimensions of a device in inches, you can work that out too, using the
metrics
method for how many pixels there are per inch of the screen.You can use the knowledge of how many pixels are in each inch of device and the amount of pixels in total to work out how many inches the device is.
This will return the height and width of the device in inches. This again isn't always that helpful for determining what type of device it is, as the advertised size of a device is the diagonal, all we have is the height and the width.
However, we also know that given the height of a triangle and the width, we can use the Pythagorean theorem to work out the length of the hypotenuse (In this case, the size of the screen diagonal).
From this, we can work out whether the device is a tablet or not:
And that's how you calculate what kind of device you're working with.
You can use the below method to get the screen size in inches, based on that simply you can check which tablet or phone the device is.
There's nothing that says
7"
or10"
AFAIK. There are roughly two ways do get screen dimensions that the system uses when decoding bitmaps and whatnot. They're both found in the application'sResources
object found in theContext
.The first is the
Configuration
object which can be obtained bygetContext().getResources().getConfiguration()
. In it you have:Configuration#densityDpi
- The target screen density being rendered to, corresponding to density resource qualifier.Configuration#screenHeightDp
- The current height of the available screen space, in dp units, corresponding to screen height resource qualifier.Configuration#screenWidthDp
- The current width of the available screen space, in dp units, corresponding to screen width resource qualifier.Configuration#smallestScreenWidthDp
- The smallest screen size an application will see in normal operation, corresponding to smallest screen width resource qualifier.With that, you can pretty much use the screen guidelines to figure out if your device is pulling from the respective specialized resource folders (
hdpi
,xhdpi
,large
,xlarge
, etc.).Remember, these are some of the buckets:
small screens are at least 426dp x 320dp
320dp: a typical phone screen (240x320 ldpi, 320x480 mdpi, 480x800 hdpi, etc).
More info
The second is the
DisplayMetrics
object obtained bygetContext().getResources().getDisplayMetrics()
. In that you have:DisplayMetrics#density
- The logical density of the display.DisplayMetrics#densityDpi
- The screen density expressed as dots-per-inch.DisplayMetrics#heightPixels
- The absolute height of the display in pixels.DisplayMetrics#widthPixels
- The absolute width of the display in pixels.DisplayMetrics#xdpi
- The exact physical pixels per inch of the screen in the X dimension.DisplayMetrics#ydpi
- The exact physical pixels per inch of the screen in the Y dimension.This is handy if you need exact pixel count of the screen rather than density. However, it is important to note that this is all the screen's pixels. Not just the ones available to you.
You'll have to make a little bit of computation using data given by the DisplayMetrics class.
You have heightPixel and widthPixels ( the screen resolution in pixels)
You need the diagonal since the 'inch screen size' always describe the diagonal length. You can get the screen diagonal in pixel (using pythagore)
diagonalPixel = √(heightPixel² + widthPixels² )
then you can convert the pixel value to inches thanks to the densityDPI value :
inchDiag = diagonalPixel / densityDPI.
I hope I didn't make mistakes here, be aware that the values you get from the DisplayMetrics class are given by the constructor, it appears (in very rare cases) that they are not well set according to the physical material...
This will give you the physical screen size but its probably not the better way to manage multiple layouts. More on this topics
place this method in onResume() and can check.
generally tablets starts after 6 inch size.
I was storing a value in values folder that gives me screen is 7 inch or 10 inc but we can do it for any device using values folder.
like create different-2 values folder for different-2 devices. But this thing depends upon the requirement.