I did find a lot of code to get current screen density in dpi and scale ratio and etc.
However what I am looking for a way to get a list of all densities that are shown inside the screen zoom view in device settings of a phone -
How do you get all available device densities via android studio?
please see a screenshot for more details on the question:
Thanks in advance!
You are mixing different things here. Density it's something tight to the device, in order to support dp and sp locally (based on screen resolution). Which means just factor to multiply pixels on this device.
You question "To get a list of all densities that are shown inside the screen zoom view in device settings of a phone" makes no sense. Since you are asking about some magic list of factors.
Now, to answer question, how to make something similar shown in the screen is pretty easy. You implementation starts with default and correct density supported by this device (as shown on image, we have centered slider), and just applying new factor changes of this density, based on user input from slider. So all your views should be aligned from encapsulated logic with using this changed scale factor (based on initial density of the device).
Related
tl;dr How do I convert pixel measurements on one device to pixel measurements on another?
I've designed an Android app with all the screens measuring 720x1280 px. I've assumed this is at 2.0 xhdpi meaning digital-pixel dimensions of 360x640 dp.
Now in QA I've found a few inconsistencies on my Nexus 5x, taken screenshots, and pulled them into Photoshop to confirm/measure the errors. The Nexus 5x is 1080x1920 px, but is at 2.6 xxhdpi which converts to 411x731 dp.
Won't just a simple scale fail?
It's not as simple as pulling the Nexus (1080 width) screenshots into Photoshop and scaling them to width 720, right? That doesn't account for pixel densities.
Said another way. If a square is 100px (50dp) on my #2x design, how large should I expect it to be on the Nexus screenshot?
Helpful links
Android Docs, especially the part about Supporting Multiple
Screens
Google's Device Metrics
There are multiple questions/thoughts in your post.
To start:
How do I convert pixel measurements on one device to pixel
measurements on another?
And:
I've designed an Android app with all the screens measuring 720x1280
px.
I'm going to assume you mean that you made some digital images/ux documents at 720x1280 and now are building out the code/loading images for your app and have run into problems because different devices are behaving in an unexpected way? Correct me if this is inaccurate.
Designing for Android, and mobile in general, is at least to some extent about asking yourself what you want the user to experience, and how to accomplish that within the framework's rules for building an interface.
Google states that when dealing with multiple devices you should aim for "density independence":
Your application achieves "density independence" when it preserves the
physical size (from the user's point of view) of user interface
elements when displayed on screens with different densities.
The image below represents "bad" design (doesn't take into account dp)
The image below represents good "density independent" design:
http://developer.android.com/guide/practices/screens_support.html#density-independence
In other words, if taking into account density independence when designing and building your app, the user should see images and/or views in a consistent way across devices. Verifying images and views on different devices should not be done in Photoshop or similar, it should be accomplished in the emulator provided in Android Studio and/or on multiple physical devices with various OS versions and/or screen sizes (as they are available to you).
Another thing to keep in mind:
You asked:
Said another way. If a square is 100px (50dp) on my #2x design, how
large should I expect it to be on the Nexus screenshot?
Go to the Google Device Metrics page:
Click on Nexus 5x, and look at the device info from the slide-in panel:
According to this info, the Google Nexus 5x is 424px/in i.e. 424dpi
Use the equation px = 50dp * (dpi / 160)
px = 132.5
It is valuable to also give a real-life example of how to calculate pixel and dp sizes when using a specific device, and hope that this helps you get to the next step and answer the question above in the future.
Let’s say we have some images we want to fit "perfectly" across a device when the device is in portrait orientation. Like this:
And let's say our device is the Nexus 5. As you can see in the above example, there are 7 identical images side by side, from the left to the right side of the screen. With the following example, you should probably be able to figure out "how large [you] should expect [images] to be" for various circumstances:
We know the Nexus 5 is an xxhdpi device because we looked it up in Google’s Device Metrics:
Let’s start doing the math by accounting for padding. I assume you have padding by default - check it out in your values/dimens.xml to make sure:
That’s 32dp we need to account for.
3. Next, we know the width of the Nexus 5 is 360dp or 1080px, so let’s figure out the width of one of our seven images:
Let’s make the decision that we’re going to import our image with Input Density of xxdpi in mind.
360dp (full) - 32dp (padding) = 328dp (remaining for 7 images)
328dp / 7 = 46.9dp (approx)
Let’s go to androidpixels, highly recommended site for conversions, and look up what that means in pixels - in this case, it equals 140.7px.
Create a png version of our image that’s 140.7px and import it as xxhdpi (we'll import it in the same dp signature as the device).
Since Adobe Illustrator and Adobe Photoshop won’t allow decimal sizes, you can go with 140, and make up the difference with spacing in Android Studio. The math suggests that if there are 6 gaps between these 7 images, and we lost 0.7px per image, that’s 6 * 0.7px = 4.2px which we may have to account for somewhere.
After you import the images in Final Android Resizer, highly recommended for resizing images - or just import it into the proper drawable folder - there’s one final step: update the content_main.xml with the correct image, and make sure to do it for all seven ImageView elements:
This is what it should finally look like:
Do these steps again with the size of the device in question.
This is also not the only way to do this. You can use layout directories to pinpoint specific device dimensions and affect views within them. See another post I made about this:
Differences between scaling images with dpi as opposed to dp
I am making an app for story books, i have my curlview animation for this app.
My problem is that i dont know what size of image and what resolution should i use for 3.5 inches to 5.0 inches screen.
I always reading on supporting multiple screen size but the problem is that i cannot get that.
All i want is the exact image size in pixel and what is the resolution should i use.
i always use 72 as resolution but i dont know what is the dimension should i use for 3.5 to 5.0 inches screen size.
did anyone knows what should i use? i dont want to read supporting multiple screen size because i already read it and i did not get it.
Thank you to someone who can give me the size i should use that will not get crash in my app.
Thanks again!
As it said in the Android style guide icon size scale for different screens should follow the 2:3:4:6 scaling ratio. Maybe you can use this ratio for other, not only icon, images too.
As you write you read the documentation, I assume you already read the Android Best Practices Chapter about Density Independence. If I got your question right, you're looking for the following part of it:
The system scales dp units as appropriate for the current screen density.
The system scales drawable resources to the appropriate size, based on the current screen density, if necessary
[...]
By default, Android scales your bitmap drawables (.png, .jpg, and .gif files) [...] so that they render at the appropriate physical size on each device.
So you basically need to provide an screen-size independent layout, and Android takes care of the rest. However it is encouraged to deliver multiple images for a better scaling quality.
If you can only afford a single size for the images, I'd decide on a target audience (e.g. how likely are Tablet users, what is the average device's screen size in my audience -> how many pixels will be available in the area you want to display the image) and then calculate the resolution of the images based on that. If a user needs a smaller or larger version, Android will scale the image up or down, given your layout is correct.
From what you write, it seems to me like you're targeting normal screen sizes, so you can have something between 470dp x 320dp and 640dp x 480dp. To get regular pixels, you must think about the screen density of your users, refer to a Question about dp/px conversion for details.
I would like my app to display correctly on any Android device, regardless of screen size, screen density or ratio of screen height to width. I want to support Android devices as old as API 8. I can create separate layouts for portrait, landscape and perhaps xlarge screens, but beyond that I don’t want to create different layouts for different densities or screen sizes.
I have read the Android guide on supporting multiple screens. I try to follow those guidelines, for example, for widgets I set their height and width to wrap_content or fill_parent, and I avoid using specific pixel measurements. For images, I provide alternative bitmap resources for different densities, or sometimes I use a specific size with density-independent pixels (dip), so the system will size it correctly on screen with different densities.
Over the past 2 years I’ve probably read that guide dozens of times.
I understand that I could define specific resources (e.g. layouts) for specific screen sizes and densities, but there are dozens or hundreds of phones I’d like to support, so that seems futile. I see table 3 in that guide shows some common dimensions for screens, but I want to make sure my app runs on nearly every phone.
It seems to me that what I need is to know is the narrowest screen (in dip) and the shortest screen. That is, I want to know the ratio of height versus width.
For example, in portrait orientation, perhaps I have a list of buttons or other widgets. I would really like to know which phone is shortest: has the largest ratio of width to height, so I can see if my layout is cut off at the bottom. Perhaps I also have 3 tabs: I would like to know the narrowest screen so I can make sure the tabs are not cut off on the right.
If I knew the minimum and max of h/w ratio for all the current Android phones, for portrait I could test on merely two Android Virtual Devices (or even just a custom screen in Eclipse).
I have taken to examining Wikipedia’s list of Android devices, but someone must have a better way.
Unfortunatly I still haven't found that. I have this link: List of displays by pixel density but not all are Android devices. I know there are a couple of weird devices with square screen ratios (motorolas). But I would simply stick to Google's screen sizes list and you should be safe for 99.9% of you users.
Edit1: stats about screen sizes and densities
Edit2: From my experience, I usually make 5 quick checks:
App on a HDPI phone with 16:9 ratio
App on a MDPI phone with 4:3 ratio
App on a XHDPI phone
App on a LDPI phone
App on a large screen tablet (usually MDPI)
I feel with those 5 cases I cover all screen densities (to check for image sizes in particular), most common screen ratios (4:3 & 16:9, to check all layout elements are accessible), and two most common screen sizes (phone & tablet).
I am currently working on an android app that is causing me some trouble. My app is heavily dependent on images that are fitted correctly to the screen they are being displayed on. Unfortunately things like the FWVGA/WVGA resolution difference 854 vs 800 is causing me some real problems, and that's aside from the other sizes/densieites and resolutions.
I don't know if I'm just missing the point here but how do I go about having my app display correctly on both the resolutions without either "squashing" my images or cutting bits off of giving me black bars etc?
There doesn't seem an immediately present solution where I can just have it display a different image automatically from the drawable folder for the right screen.
I have tried using displaymetrics but unless i have my manifest claim that it supports all screen sizes, the android OS will in fact "lie" to my app about hte current resolution.
Someone please tell me there is an easy solution.
how do I go about having my app display correctly on both the resolutions without either "squashing" my images or cutting bits off of giving me black bars etc?
IMHO, you start by revisiting your GUI design:
My app is heavily dependent on images that are fitted correctly to the screen they are being displayed on.
That does not work particularly well anywhere, IMHO. Windows (including browsers) can be sized to any desired size by the user, for example.
There doesn't seem an immediately present solution where I can just have it display a different image automatically from the drawable folder for the right screen.
Because Android devices can have any number of screen dimensions, just like windows and Web browsers can have any number of screen dimensions. Android does let you choose images based on density (e.g., -hdpi) or screen size (e.g., -large), but these do not map to resolutions, nor should they.
As CommonsWare pointed out, your best bet for this situation really is to rethink your layout structure. It's probably possible to accomplish similar, if not identical results to what you currently have, using RelativeLayouts and widgets. If you're willing to post a sample, we could advise you on ways to do so.
That said, an answer to your question would be to query the device at runtime for its screen resolution (keeping in mind space taken up by the notification bar, as well as the title bar - unless you theme it without a title bar). You would need a different image for every possible screen resolution, and as more devices (notably tablets) begin to release, you're going to have to continue to provide more images and customized code for each different image as they release. You can see why this is not an ideal situation.
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int screenWidth = metrics.widthPixels();
int screenHeight = metrics.heightPixels();
Then you can place that into an if-then-else block, and determine the resolution, and set the image accordingly. I don't recommend this.
When it comes to displays and resolutions, it is not possible to have your images resized to any aspect ratio without either squishing / stretching them, or using letterboxes. I know this doesn't really answer you question, but if you can't get reliable screen size information from the OS, then try picking images for the largest resolution and cropping them for the smaller resolutions. Just make sure there is nothing interesting around the borders.
I've determined an estimate of the size of my Android emulator's screen in pixels, although I think its resolution can be changed to other numbers. Quite frankly though that doesn't eliminate the general problem of not knowing how many pixels on each axis I have to work with on my Android applications in general.
The main problem I'm trying to solve is this: How do I make sure I don't use a faulty resolution on Android applications if I want to keep things' sizes constant (so that if the application screen shrinks, for instances, objects will still show up just as big - there just won't be as many of them being shown) if I wish to do this with a single universal resolution for each program? Failing that, how do I make sure everything's alright if I try to do everything the same way with maybe a few different pre-set resolutions?
Mainly it seems like a relevant question that must be answered before I can come across a complete answer for the general problem is how big can I always make my application in pixels, NOT regarding if and when a user resizes the application's screen to something smaller than the maximum size permitted by the phone and its operating system.
I really want to try to keep this simple. If I were doing this for a modern desktop, for instance, I know that if I design the application with a 800x600 canvas, the user can still shrink the application to the point they're not doing themselves any favors, but at least I can basically count on it working right and not being too big for the monitor or something. Is there such a magic resolution for Android, assuming that I'm designing for API levels 3+ (Android 1.5+)?
Thanks
Hmm, I suggest you read this: Supporting Multiple Screens. That gives a wealth of information on how to handle different screen sizes. In particular, read about its concept of density independent pixels versus absolute pixels. Density independent pixels are essentially pixel sizes that the system automatically scales for you depending on the screen size you're working on. It considers the 'standard' screen as that of the G1, Hero or similar devices, i.e. 480x320 pixels with a density of 160dpi. If the screen you're working with is bigger than that, i.e. the Droid or Nexus One, it adjusts all density independent pixel sizes by 50%.
Alternatively, if you're really talking about canvases, as in, Canvas for animations etc, you can just call canvas.getWidth() and canvas.getHeight().