There are some screen resolutions already defined in Android.
They are:
QVGA (240×320, low density, small screen)
WQVGA (240×400, low density, normal screen)
FWQVGA (240×432, low density, normal screen)
HVGA (320×480, medium density, normal screen)
WVGA800 (480×800, high density, normal screen)
WVGA854 (480×854 high density, normal screen)
How do I know which type my device screen resolution is?
Use DisplayMetrics to get screen info from your device.
Sample code:
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
final int height = dm.heightPixels;
final int width = dm.widthPixels;
Have you tried to search the specifications of your device? i.e. from Wikipedia's Nexus One article, you can find Nexus one screen resolution:
Display 480 x 800 px (PenTile RGBG), 3.7 in (94 mm), 254 ppi, 3:5 aspect ratio, WVGA, 24-bit color AMOLED with 100,000:1 contrast ratio and 1 ms response rate
That's a starting point...
Related
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
I'm struggling with a pretty trivial task in the Android "multiple-screen sizes" domain.
What I'm trying to achieve
A layout matching the screen width, with a nine-patch background which resizes (only horizontally, since there is always enough vertical space). Here is a dummy image:
My goal is, depending on the screen resolution, to display the image at the highest resolution possible, by using a set of different sizes, eg. 320x45, 480x67, 600x87, 720x101, without any down-scaling. I'm hoping for a non-programmatic solution.
An example with the above mentioned image sizes would be:
3.7" Nexus One (480 x 800) - the 480x67 image would look best.
4.7" Galaxy Nexus (720 x 1280) - the 720x101 image.
4.7" Nexus 4 (768 x 1280) - again the 720x101 image, stretching to the full 768 px width and becoming 768x101.
Issue
The whole Android resource allocation revolves around dps (density-independent pixels), when in fact I want to display an image based on the actual available pixels.
If I allocate the 480x67 image to res/drawable-mdpi and a 600x87 to res/drawable-hdpi, then image would display correctly on a 5.4" display of 480x800, i.e. mdpi display. However, a 4" 480x800 displays qualifies as hdpi and the system would appoint the 600x87 image, which won't fit the screen.
I tried the smallestWidth parameter as described in the online guide, but that yields strange results. For instance, a 3.7" 480 x 800 display (hdpi) uses my drawable-sw320dp image, although there is a drawable-sw480dp resource available too.
What is the best way to assign a stretchable, width-matching image with the best possible quality? Isn't there any non-programmatic solution?
Thanks in advance!
I believe that by combining density and screen size resource qualifiers you can achieve a close to optimal behavior.
Lets assume this kind of resource folders structure:
drawable-normal-hdpi - A normal size dictates minimum width of
320dp. hdpi dictates 1.5X dp to pixel multiplier. So the minimum px
width of the normal hdpi bucket is 480px. We put here a 480px wide
image.
drawable-normal-xhdpi - Again size dictates 320dp but this time
with 2X multiplier. So we use a 640px wide image.
drawable-xlarge-mdpi - Size means at least 720dp. mdpi multiplier
is 1X, so we use a 720px wide image.
Now lets look at some devices to see how they fall in with those buckets:
Nexus one - normal hdpi. Actual px width: 480px. The image fits
perfectly.
Galaxy nexus - normal xhdpi. We could fit a 720px image, so the
640px image we use isn't optimal - but it's very close.
Nexus 4 is just like the Gnex.
Nexus 10.1 (1280X800) - xlarge mdpi. We could fit 800px, our image
is 720px. Again not ideal but close enough.
Worst case scenario: image used could have 5-10% better quality.
Best case: perfect fit.
The main down side of this method is that you need to provide a lot of resources and folders to account for all the permutations of sizes and densities (even worse if you need to combine that with more qualifiers for locale, orientation and so on). However, as far as my Android understanding goes I don't think you can achieve something better than this without coding.
A remark regarding smallestWidth: Your example for the weird behavior is actually the expected behavior.
hdpi multiplier is 1.5 - So a 480px wide hdpi display is exactly 320dp wide. This makes the drawable-sw320dp the right choice, as documented. I'm not sure if you can combine the smallestWidth qualifier with the dpi qualifier. If it's possible you might get more accurate results than just size modifiers. But this would mean a lot more permutations for a 5% increase in image quality. Probably not worth it.
Actually, your method is not how it is supposed to be. I will suggest 2 ways for you, one is easy but doing programmatically, other one is using a custom view.
Method 1 - Programmatically
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int screenWidth = displaymetrics.widthPixels;
// this will determine "scale ratio" so using which image height and width won't matter
int imageOriginalHeight = 101; // your original image height
int imageOriginalWidth = 720; // your original image width
int imageScaleHeight = (screenWidth*imageOriginalHeight) / imageOriginalWidth;
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(screenWidth, imageScaleHeight);
imageView.setLayoutParams(params);
imageView.setImageResource(R.drawable.file);
Method 2 - Custom View
You can use a custom view called ScaleImageView which is written by Maurycy Wojtowicz.
Class is defined like below:
This view will auto determine the width or height by determining if
the height or width is set(exact size or match_parent) and scale the
other dimension depending on the images dimension This view also
contains an ImageChangeListener which calls changed(boolean isEmpty)
once a change has been made to the ImageView
Here is how you are going to implement it.
Create a class named ScaleImageView.java and copy contents of the link above.
In your xml file, create a ScaleImageView, just same like ImageView (the example I am writing below is for filling screenwidth, and scaling height according to that so there will be no empty spaces on right/left)
<com.project.customview.ScaleImageView
android:id="#+id/scaleImageView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
android:src="#drawable/file" />
If you need to declare and set programmatically in your Activity, it is also the same as using ImageView:
imageView = (ScaleImageView)findViewById(R.id.scaleImageView);
imageView.setImageResource(R.drawable.file);
In android you have the option hdpi, mdpi, xdpi,etc..
folders for that , you have to create different images according your device resolution and put your images at there after confirming your device resolution and density category.
for the more reference why it'll happen you can see here
here i explain some chart may be helpful to you.
Low density Small screens QVGA 240x320 (120dpi):
drawable-small-ldpi (240x320)
drawable-small-land-ldpi (320x240)
Low density Normal screens WVGA400 240x400 (x432) (120dpi):
drawable-ldpi (240 x 400 )
drawable-land-ldpi (400 x 240 )
Medium density Normal screens HVGA 320x480 (160dpi):
drawable-mdpi (320 x 480 )
drawable-land-mdpi (480 x 320 )
Medium density Large screens HVGA 320x480 (160dpi):
drawable-large-mdpi (320 x 480 )
drawable-large-land-mdpi (480 x 320)
Galaxy Tab ( 240 dpi ):
drawable-large (600 x 1024)
drawable-large-land (1024 x 600)
High density Normal screens WVGA800 480x800 (x854) (240 dpi):
drawable-hdpi (480 x 800)
drawable-land-hdpi (800 x 480)
Xoom (medium density large but 1280x800 res) (160 dpi):
drawable-xlarge (800 x 1280)
drawable-xlarge-land (1280 x 800)
I have got a Samsung Galaxy S4 Active
When I execute on Android, the following code:
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width=dm.widthPixels;
int height=dm.heightPixels;
int dens=dm.densityDpi;
double wi=(double)width/(double)dens;
double hi=(double)height/(double)dens;
double x = Math.pow(wi,2);
double y = Math.pow(hi,2);
double screenInches = Math.sqrt(x+y);
I obtain
width = 1080
heigh = 1920
dens = 480
So, is the used formula is correct, screenInches is 4.589. But the specification said that the screenInches size is 5.
I have also tried with my Samsung Galaxy SII that gives me:
width = 480
heigh = 800
dens = 240
that corresponds to a screenInches of 3.887 instead of 4.3 (as said in the specs)
Why there is this difference between the declared size in inches and the size I obtain?
EDIT: It seems that densityDpi returns one of these values: (120, 160, 213, 240, 320, 480 or 640 dpi).
I don't think that you can get the real screen density from DisplayMetrics.densityDpi. It can return only one of particular constant value.
DisplayMetrics documentation says that
The screen density expressed as dots-per-inch. May be either DENSITY_LOW, DENSITY_MEDIUM, or DENSITY_HIGH.
But Android Compatibility Definition Document (CDD). provides a more comprehensive explanation
Screen Density
The Android UI framework defines a set of standard
logical densities to help application developers target application
resources.
Device implementations MUST report one of the following
logical Android framework densities through the
android.util.DisplayMetrics APIs, and MUST execute applications at
this standard density.
> 120 dpi, known as 'ldpi'
> 160 dpi, known as 'mdpi'
> 213 dpi, known as 'tvdpi'
> 240 dpi, known as 'hdpi'
> 320 dpi, known as 'xhdpi'
> 400 dpi, known as '400dpi'
> 480 dpi, known as 'xxhdpi'
> 640 dpi, known as 'xxxhdpi'
Device implementations SHOULD define the
standard Android framework density that is numerically closest to the
physical density of the screen, unless that logical density pushes the
reported screen size below the minimum supported. If the standard
Android framework density that is numerically closest to the physical
density results in a screen size that is smaller than the smallest
supported compatible screen size (320 dp width), device
implementations SHOULD report the next lowest standard Android
framework density.
You can get the exact physical pixels per inch of the screen in the X and Y dimension from DespalyMetrics.xdpi and DespalyMetrics.ydpi
densityDpi returns the screen density expressed as dots-per-inch, but this value is approximated.
densityDpi is calculated from density which is a logical density of display.This is a scaling factor for the Density Independent Pixel unit, where one DIP is one pixel on an approximately 160 dpi screen (for example a 240x320, 1.5"x2" screen), providing the baseline of the system's display. Thus on a 160dpi screen this density value will be 1; on a 120 dpi screen it would be .75; etc.
As said in the documentation:
This value does not exactly follow the real screen size (as given by xdpi and ydpi) , but rather is used to scale the size of the overall UI in steps based on gross changes in the display dpi.
So in order to obtain the correct DPI I can use xdpi and ydpi.
Applying these to my Samsung Galaxy S4 Active, I obtain:
xdpi = 442.451
ydpi = 439.351
My phone spec said ~441 ppi pixel density, so I think these value are correct.
So, getting the average value 440.901 DPI the result is:
screenInches = 4.9963
I did my analysis and found that most of the users of my application will be of samsung galaxy S2, and samsung galaxy note . Doing research on their technical spec, I found:
Galaxy S2:
480 x 800 pixels, 4.3' display
Galaxy Note:
1280 x 800, 5.3”screen
How can I categorize these into Layout size and desnity level? I think I can figure out the layout size as mentioned in the android development page
However I am unable to know the density level (low, medium high xhigh). They say that I need the dpi but in functional spec, I rarely see the dpi, I just see the above resolution
Thank you
Calculate the density first Density=Square root((wp*wp)+(hp*hp))/di
where wp is width resolution in pixels, hp is height resolution in pixels and di is diagonal size in inches. It would come ~208 for S2.
ldpi ~120dpi,mdpi ~160dpi, hdpi~240dpi, xhdpi ~320dpi. Compare from these values of dpi
Not sure if I understand you correctly but you can request the density level and the dpi from the DisplayMetrics class. Using resolution and density you can approximate the display's size.
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
I'm confused regarding the densities. I see that with medium density, the screen resolution could be either 320x480, 480x800, or 480x854. So if I have an image thats 300px wide in the mdpi folder, how is it going to look the same size on all 3 different screen sizes (mainly 320x480 vs the other 2)?
And by look the same size, I mean scale to be bigger or smaller depending upon the screen size. Thanks.
There are three distinct but linked concepts to understand here: screen density (pixels per inch/centimeter, or commonly known as DPI from dots per inch in printers), physical screen size (in inches or centimeters) and number of pixels (also known as resolution, in pixels).
These terms are not interchangeable, and you need to understand how they interlink to not be confused with the issue. Generally, you can ignore physical screen size since that's already accounted for in the density. For example a screen 3 inches wide and 300 pixels across will have a DPI of 100. Furthermore phones screens tend to have about the same physical size, even if the number of pixels is very different.
So, let's consider the screen of a G1 or Hero which has a resolution 480x320 and a density of approx 160dpi. An image 300 pixels wide will be 1.875 inches across. This is calculated by pixel size (300) / density (160). Now if you compare this to the screen of the Nexus One, Droid or similar, these models have a higher resolution screen of approx 800x480 with a high density of approx 240dpi. If you display the same 300px wide image, it will now only physically be displayed at about one and a quarter inches across. In other words, it will be much smaller. This can be a problem because if the image contains text, then the text might not be readable anymore.
Android can be told to automatically scale images to fit these different screens so that it still looks to be the same size. This is done by setting sizes in Density Independent pixels. If something is 100dp wide, it will be 100px wide on a medium density screen. On a high density screen, it will be 150px wide, but they will both look about the same size on the actual screen. However, if you do this, your image can go a bit blurry. It's the same as when you zoom into a photo too closely in a picture viewing program; the edges go blurry since it 'stretches' them while you zoom.
The way to solve this is to use the mdpi, hdpi and so forth folders. You're giving Android an image that has already been scaled, so that it doesn't have to do it itself. Obviously if you just stretch the image yourself in Photoshop, then it won't look any better. But normally one is resizing very large images down to make them fit the mobile screen. In that case, you just resize them three different times, each into a different resolution.
So to finally answer your specific question: if you have an image placed in your mdpi folder, it will be exactly the same size regardless of the screen resolution, as long as they are all the same density. What will change is how much space around them, e.g. a 320x320px wide image would fill most of a 320x480 screen, but only about a third of a 480x800 screen. However, as noted above, generally the higher resolution phones also have a more dense screen. In that case, Android won't look in your mdpi folder for the image - it will go to the hdpi folder, and if it can't find it there, it will take the default "drawable" folder. Then if you've used DP it will automatically scale it, or if you've used PX, it will leave it as is, and it will just look smaller.
There! A very long answer for you. I hope it makes sense.
For completeness, also check these option for controlling layout:
Directory qualifiers:
Size: small, normal, large
Density: ldpi, mdpi, hdpi, nodpi(no auto-scale)
Aspect ratio: long, notlong
Orientation: land
Usage:
res/layout/my_layout.xml
res/layout-small/my_layout.xml
res/layout-large/my_layout.xml
res/layout-large-long/my_layout.xml
res/layout-large-land/my_layout.xml
res/drawable-ldpi/my_icon.png
res/drawable-mdpi/dpi/my_icon.png
res/drawable-hdpi/my_icon.png
res/drawable-nodpi/composite.xml
Restricting your app to specific screen sizes(via the AndroidManifest):
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
...
<supports-screens
android:largeScreens="true"
android:normalScreens="true"
android:smallScreens="true"
android:anyDensity="true" />
...
</manifest>
And for code level tweeking:
float scale = getContext().getResources().getDisplayMetrics().density;
And don't forget:
dpi = 160; //At 160dpi
pixels = dips * (density / dpi)
It's all in this doc:
developer.android.com:Supporting Multiple Screens
So if I have an image thats 300px wide
in the mdpi folder, how is it going to
look the same size on all 3 different
screen sizes (mainly 320x480 vs the
other 2)?
How the image looks, physically, is driven by screen density, not screen size. Your -mdpi folder is not tied to screen size -- it is tied to screen density.
This is what Device Independent Pixels (DIPs) are for. Instead of 320px write 320dip.
http://developer.android.com/guide/practices/screens_support.html
Could you please confirm the formula for calculating the screen density?
As I have read, the following is the formula:
Density = SQRT (wp^2 + hp^2)/screen size
wp -> width of the screen (in px)
hp -> height of the screen (in px)
screen size -> Physical screen size (diagonal inches)
screen size (320x480) = SQRT(102400 + 230400) /160 = 3.6 inches
screen size (480x800) = SQRT(640000 + 230400) /160 = 5.8 inches
screen size (480x854) = SQRT(729316 + 230400) /160 = 6.12 inches
So, the layouts (UI screens) are driven by screen sizes (small: <3", normal <4",
large >5") and drawable resources (images) are driven by screen densities.
And, the size of the image (in pixels) does not change if the density of the screens
(320x480, 480x800, or 480x854) are the same.
Could you please confirm?
Thanks,
Ram
Actually the code to calculate physical screen size for devices is the following one:
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
double x = Math.pow(dm.widthPixels/dm.xdpi,2);
double y = Math.pow(dm.heightPixels/dm.ydpi,2);
double screenInches = Math.sqrt(x+y);