This question already has answers here:
How to get the screen size of the device?
(4 answers)
Closed 9 years ago.
How can I detect the screen size of a android phone in inches?getSize method returns size from pixels ,but when adapting to different screen sizes it not work.There are screen with same pixel size but smaller in inches,in that case some views not get required space in screen.If can't get size in inches can do same thing using pixel size?
You will be getting pixels right convert to inches by,,,
To get the size of a lets say how much 1pixel = x inches. Ask the person to get a ruler and put it on the screen and measure the width of a line e.g. 320 pixel's line. Then you'll know 320pixels = x inches, given x 320/x = 1 inch.
Or
For your map just let x pixels = y inches and the map would be scaled vertically and horizontally by different factors the person would have a good idea in size provided you show a horizontal and vertical scale.
Edit
Hey I got a better way to do this, but it doesn't work when the screen does not fill the entire monitor (commonly on CRT monitors). Just ask for the size of the monitor the person is using and your set with everything you need. The ratios of 1024x768, 800x600, 640x480 are all 4/3, so is the height and width of the physical monitor. 15" monitors go up by 9" and left by 12". so the height in pixels is split evenly in 9" and width is split evenly in 12". Therefore:
x_screen_resolution = 12"
y_screen_resolution = 9"
Now you have your conversion factor from pixels to inches for a 15" monitor. The ratio 4/3 is probably a standard so if you get in inches Z of the size of the monitor then
x*x + y*y = Z*Z ... eq1
x/y = 4/3 .... eq2
x = 4y/3 ...
16yy/9 + yy = ZZ
25yy/16 = 9ZZ/16
yy = 9ZZ/25
-------------
y = 3Z/5
We now know y solve for screen width x
x = sqrt(ZZ-yy)
and thus
x_screen_resolution = x inches
y_screen_resolution = y inches
hope it helps
Related
I have a major issue I am working on for days now. It is much understandable by looking at the requirement first. I will list down my requirement as simple as possible in point form below.
I have 5 android phones. 5 different brands, different screen sizes.
Imagine the screen sizes are 4inch, 4.5inch, 5inch, 5.1inch and 5.2inch
I have an android app and it has a drawing canvas.
Now using canvas.drawCircle(x / 2, y / 2, radius, paint) I am drawing a circle. Imagine the radius is 100 (100 pixels?)
I install this app in my smallest screen phone, 4inch. Then I use a ruler and measure the the circle diameter. Imagine the circle diameter is "exact" 3cm.
Now I install this in my other phones.
Unfortunately, in my 4.5 inch phone the circle diameter is 3.2cm. In 5 inch phone it is 3.3 cm. In 5.1 inch phone it is 2.8cm and so on.
However I want my circle diameter to be 3cm (exact 3cm) in every phone.
Above requirement is something I am trying for days now. I tried the following to make sure I get a circle with no size change across all screens.
Using ImageView - I tried using an ImageView and added a circle image. I have given the width, height fixed. I have tryied setting the values in px, dp, inches etc. I also tried scalling options available for ImageView. Also tried placing the same image in drawable-nodpi folder. Tried creating drawable folders for all sizes (ex: drawable-mdpi, drawable-hdpi). Non of this stopped the image from being scaled.
Using Canvas - As explained in the above, I tried using canvas and drawing the image. The drawing scales.
Finding pixels per inch.- Tried finding pixels per inch of each phone programatically thinking I can find a way to develop a logic from it to dynamically draw the images in same size. I used several links including - Calculate PPI of Android Device. Most of them talk about getting screen resolution only.
How can I fulfill my requirement of drawing same size circles? If it can be done by knowing PPI, how should I do it?
The Answer before has worked for me but as long as it didn't for you I will try explain something I think it will help.
I have a device that have a density of 1 (160 px per inch), and the Display metrics told me that i have 320 * 480 pixels, with simple calculation I should know my mobile width and height in inch like this : 320 /160 = 2 inch for width , 480/160 = 3 . for now everything was just fine until i got the ruler and measured it and the surprise was this : I have a 1.65 * 2.48 inch !!.
Then I noticed that there is something called physical pixels per inch of the screen this was the relief for me and you can get it like this :
getResources().getDisplayMetrics().xdpi; //for width physical dpi
getResources().getDisplayMetrics().ydpi; //for height physical dpi
And now I can calculate my Physical width and height of my device like this :
my physical dpi for both width and height is 193.5238 so...
320 / 193.5238 = 1.65 inch width
480 / 193.5238 = 2.48 inch height
And that was correct !!.
Now back to your problem lets get the 3cm in pixels for any Phone :
getting the width in cm :
width in cm = width in inch * 2.54 (because 1 inch equals 2.54 cm)
getting the amount of pixels in each cm :
width in pixels / width in cm = Pixels per cm for width (px/cm)
now you want a 3cm then the pixels according to it is:
3 * Number = 3cm Pixels
all of the above can be shortened like this :
float devicePixelsWidth = getResources().getDisplayMetrics().widthPixels;
float deviceActualDpi = getResources().getDisplayMetrics().xdpi ;
float deviceActualInchWidth = devicePixelsWidth / deviceActualDpi ;
float deviceActualCMWidth = deviceActualInchWidth * 2.54f ;
float PixelsForActual3CM = devicePixelsWidth / deviceActualCMWidth * 3;
In the end, all of the above has been tested and approved by me and has the same accuracy of the previous Answer method :)
maybe this is not answer, but might be helpful
float _100MmAsPx = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_MM,
100/*mm unit set*/, getResources().getDisplayMetrics());
above gives me 1661.5354 on Nexus 5x and 1889.7638 Nexus 5 (emulator). both have fullHD display, but 5 has 4.95 inch versus 5.2 inch in 5x.
it means 100 millimeters is 1662 pixels on 6 and 1890 on 5x
I am new to android studio; so please excuse if the query is trivial. My problem is with understanding the layouts.
My layout and the corresponding values folder is as shown below. My problem is that a correct layout is not being picked up. For example, for Nexus 4 (4.7inch, 768x1280, xhdpi), in the landscape mode, the layout is being picked up from the 'layout' folder. As per my understanding (which might be totally wrong :) ) it should have been picked from layout-sw720dp-xhdpi. Any suggestions please?
layout-problem
sw stands for smallest width. So if the a device has a width of 320dp in portrait mode and 720dp in landscape mode then you end the device's smallest width is 320dp.
Try use layout-w720dp-xhdpi instead of layout-sw720dp-xhdpi and see if it works.
How to calculate device dimensions in dp
Calculate the PPI (Pixel per inch) of the device.
Calculate the dimensions of the device using dp
Where:
= Screen width in pixels
= Screen height in pixels
= Screen diagonal in pixels
= Pixels per inch
= Screen diagonal in inches
Edit: According to Wikipedia "The Nexus 7 (2013) screen now has a 1920×1200 pixel resolution (960dp × 600dp)". So its smallest width is 600dp which is why your layout isn't being used.
Proof
PPI = (√(19202 + 12002))/7.02 = 322.53
Width in dp = (1920*160)/322.53 = 952.47 = Roughly 960dp
Height in dp = (1920*160)/322.53 = 595.29 = Roughly 600dp
So I'm implementing admob banners into my Unity 4.6 Android game. I have the banner at the bottom and need to account for it in my UI. Specifically I need to know exactly how tall it is, but I'm having trouble with figuring this out.
Based on the information found here the Smart Banner should be 32, 50, or 90 pixels tall, depending on device height. This doesn't seem to typically be the case though.
Some searching would seem to indicate this is because of density pixels. So I attempt to convert the stated pixel height using px = dp * (Screen.dpi/ 160). So for example if I determine the banner height should be 90 pixels, I would use bannerHeight = 90 * (Screen.dpi / 160). This seems to work on some devices but not others.
For example my Nexus 4 has a DPI of 320. Using the above it would seem to indicate that the banner should be 180 pixels tall, but the banner appears to actually be about 90 pixels tall. But on the Nexus 7 (which has a dpi of 166), the banner appears to be about 120 pixels tall when the formula would indicate it should be ~93.
So I guess I have no idea how to figure out how tall the banner is actually going to be, and I haven't found a way to get this information from the API. My code for calling the banner is pretty stock:
string adUnitId = "my_id";
BannerView bannerView = new BannerView(adUnitId, AdSize.SmartBanner, AdPosition.Bottom);
AdRequest request = new AdRequest.Builder().Build();
bannerView.LoadAd(request);
First you need to calculate which Ads google will give you (32 or 50 or 90 ?)
for this you can calculate it by screen sizes.
height/dpi = actual height in inches
so, Google says above 720 (with dp of 160dpi) is acctauly
720/160 = 4.5 inch height, above this height, the ads are 90 pixels (dp!)
below its 50 dp!
400/160 = 2.5 , below this, ads will be 32 dp
SO!
if i have Xiaomi mi3 with 1920x1080
thats,
1920/480dpi = 4 inch height..which will give us 50 dp ads.
with the formula of converting DP to pixel
px = dp * (Screen.dpi/ 160)
50 * (480/160) = 150 pixels height for the ad!
For landscape you need to use as "height" 1080 instead of 1920
1080/480dpi = 2.25 height in landscape
this means the ads will be 32 pixels dp
which converts to:
32* (480/160) = 96 pixels in landscape
its too bad google doesn't give enough examples so we can really check ourselves.
Your info page is:
https://developers.google.com/admob/android/banner
There is answer:
https://stackoverflow.com/a/14204959/1900546
int heightPixels = AdSize.SMART_BANNER.getHeightInPixels(this);
The following function may come in handy to those of you using Admob Smart Banners:
public static float adHeight(){
float f = Screen.dpi / 160f;
float dp = Screen.height / f;
return (dp>720f)? 90f * f
:(dp>400f)? 50f * f
:32f * f;
}
To use it:
r.offsetMin = new Vector2(r.offsetMin.x, adHeight());
where r is the RectTransform of the fullscreen Canvas / Panel.
I am attaching a 'loadBannerAd' script to every panel which requires it.
The link you gave says that the Smart banner will have a height of
32 dp for phones in landscape
50 dp for phones in portrait
90 dp for tablets in either orientation
Let's take the two examples you gave.
The Nexus 4 has a DPI of ~320 dp. Given that it's a phone, it'll have an ad with a height of 50 dp.
Using the formula, it works out to 50 * 320 / 160 = 100 pixels (close to your actual banner's height)
The Nexus 7s I looked up have either a 216 dp (2012) or 323 dp (2013), so I don't quite know which model has 166 dp.
But let's assume that it's 216 dp, then being a tablet, ad height in dp is 90. Therefore, pixels = 90 * 216 / 160 = 121.5, once again close to your banner's actual height.
I realize that Google has also mentioned the following
For devices with heights between 400 & 720, an ad with a height of 50 is used
For devices with heights above 720, an ad with a height of 90 is used.
However, I believe that the 400 & 720 MIGHT also be expressed in dp? i.e. 400 pixels in an mdpi device. Your Nexus 4 being an xhdpi device (320 dp), with a height of 1280 pixels then would be the same as an mdpi device of height 640 pixels, falling within the range for an ad height of 50 dp.
Bottomline, your calculations seem valid. Stick to the first set of rules, and you should see consistent results.
bannerView.SetPosition(AdPosition.Top);
or something similar to this is how you change a position of an active banner
I would like to get size of the Android Phone but size in mm and not in pixels.
I know how to get size in pixels, however I would like to do some RND and for that I want to get width of the screen in mm.
Like here Galaxy S4 size is Dimensions 136.6 x 69.8 x 7.9 mm (5.38 x 2.75 x 0.31 in)
136.6 x 69.8 x 7.9 mm (5.38 x 2.75 x 0.31 in)
^^^
Does any one knows what is this size for?
Though I am iPhone developer BUT I am beginner (1 day baby) for Android. Well below is what I want to do as RND.
Let's say designer gives me design for the app of size 1080*1920 and have title of size 30px (which covers 80% exactly) of the screen size.
That means 10% space on left side, 80% text and 10% space on right side.
I know in Android we have something called sp or android:textAppearance="#android:style/TextAppearance.Large", but that won't give the 10% spaces.
What I want to achieve is regardless of size, design should go same.
Considering design to fit with width, if height increases, I would have scrollview and I am okay with that.
"Pixel density" is the number of pixels per inch. So dpi gives you the scale factor you need so that the number of pixels is consistent across devices.
Another way of saying this is that if you have a 480 pixel screen with hdpi, then a 360 pixel screen at mdpi is about the same physical size. While the screen sizes may vary some, it generally is not that important. Even so, as mentioned, see this post about doing the actual calculation:
Is there a way to determine android physical screen height in cm or inches?
However, you are talking about percentages of screen size in your example. If you know the screen size in pixels, then the physical size doesn't matter, just do your calculations using pixels. If you have 480 pixels, 10% is 48.
You cannot do this in XML, which it sounds like you might be wanting to do. You can use "weighted linear layouts" for runtime calculations of the screen size. You can also use different res folders depending on screen size (like res/layout-hdpi).
You may want to read this from Google regarding supporting different screen sizes: http://developer.android.com/guide/practices/screens_support.html
Use DisplayMetrics. The API is: http://developer.android.com/reference/android/util/DisplayMetrics.html
You can perform a calculation on the given fields and obtain the height and width in inches, then convert that to mm.
I'm trying to define a line in Android which measure must be 10cm wide.
I tried to put the width in mm and inches, but the results weren't satisfactory.
Also, I tried to follow this link: draw square 3x3 cm in Android
Desperately, i tried to measure 10cm in dip on the screen, but not is the same length in all screens
Is there any way to get it?
Thanks
I think:
height of screen (cm) / height resolution(pixel) * 3 = 3 cm height in pixel for that screen and resolution.
width of the screen (cm) / width resoluzion(pixel) * 3 = 3 cm width in pixel for that screen and resolution.
Not sure about this.
REFER LINK TO GET dpi
nbpixel(1 inch) = densityDpi
so convert 1 cm to inch who give approximativly 0.4 inch
nbpixel(1cm) = 0.4 * densityDpi
So for make a ligne of one cm you must draw 0.4*DPI pixel
To draw a line of 1 cm, you must use multiple dpi (pixel density) to find the number of physical pixels.
dpi/2.54
Mobile device displays also have something which is called devicePixelRatio. You might want to check that too. When building RealRuler (online ruler) I used to multiple mobile screen width/height with that ratio to get actual size. Not sure if it impacts actual size in cm in Android.
All this is very tricky.
An inch or a mm are unit of linear measurement on a surface, which could be a screen or a piece of paper.I'm strange if the result is not satisfactory for you.See this answer for more details.