I have made several values folder and dimen.xml files for configuring my app for different screen sizes. The preview in Android Studio shows them perfectly however, when I run the app on the phone, those dimensions don't take effect. And the layout shown in the Android Studio layout and the layout shown on the phone are different.
So for example on my nexus 5 the app should read dimensions from values-w640dp/dimen.xml however, it reads them from values/dimen.xml for some odd reason.
What can I do to resolve it?
Here are the screenshots.
Android Studio Preview Snapshot (Nexus 5)
Nexus 5 Phone Screenshot:
So dp are calculated using the following formula:
pixels / dp = dpi / 160dpi or pixels / dp = density
Looking at device metrics, the Nexus 5 has a density of 3 so:
dp = pixels / density
dp = 1920px / 3 = 640dp
dp = 1080px / 3 = 360dp
Using values-sw600dp would not fix it because that is not the actual smallest width of the screen since it can also be 360. So you should use values-sw300dp. Be warned though that this covers a very wide variety of devices. values-sw600dp usually targets tablets.
I have also run into issues where the true resolution of the device does not match what the emulator created, with the Nexus 5 too, so make sure you check the AVD screen and ensure that the resolution matches the real resolution of the device. Example, the Nexus 5 should be 1920x1080 and it is. If you ever encounter this issue, recreate the emulator or try a different one entirely.
Related
This is how I am getting the screen density using Android Studios
float density = getResources().getDisplayMetrics().density;
According to device specification, S8+ has 4.0 Density value and falls under xxxhdpi category
Refer this site https://material.io/devices/,
Have Attached a screenshot for reference
here
But the value returned by the above code is 2.9, which seems very wrong
check here
Tried using densityDpi also, but it also returns xxhdpi as opposed to xxxhdpi
int densityDpi = context.getResources().getDisplayMetrics().densityDpi;
The above code works well on other devices, tested on OnePlus 5T, Nexus 6P, Redmi Note 4, Moto g4+ and others...facing issues with the Galaxy s8+, haven't tested for Galaxy s8 but i guess the result will be same.
Is this a know Bug, or am I doing something wrong?
Its getting difficult to manage layout for S8+ devices without the correct pixel density info for s8+ devices.
Just like Surfman said. Your phone isn't at the maximum possible resolution. If you take the resolution reported within your screenshot and divide it by the reported 2.9 and multiply it by 4 you end up with roughly the maximum resolution of 2960x1440
You need to initialize display metrics object
You can try this:
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
flat density=metrics.density;
flat densityDpi=metrics.densityDpi;
I am creating different layouts for different screensizes using the layout-sw<>dp qualifier, but for some reason, it is not working. I have the following layouts:
I made a special sw320dp directory so that it looks nice on one of my test devices which was 569 dp x 320 dp. Then, for my LG-G3, which is 480 dp x 853 dp, I made the layout-sw480dp folder.
However, the 320dp layout is showing on both phones, including my LG G3.
Am I doing something wrong? Shouldn't the 480dp layout show on my LG G3? Please let me know.
Ruchir
This link tells to consider actual sizes that are available to your activity's window when choosing qualifiers
The smallestWidth of a device takes into account screen decorations
and system UI. For example, if the device has some persistent UI
elements on the screen that account for space along the axis of the
smallestWidth, the system declares the smallestWidth to be smaller
than the actual screen size, because those are screen pixels not
available for your UI.
so ensure you have at least 480dp available to your screen and it is not partially taken up by any system UI/ screen decoration.
I have Tablet with 7" screen (600×1024) with hdpi (240 dpi classification).
I have created folder layout-sw600dp. But it's not working in this resolution tablet.
Its working fine with 7" screen (600×1024) with mdpi (160 dpi classification).
Which folder should I create for 7" (600×1024) tablet which has hdpi (240 dpi classification)?
It depends from the Android API version you're building against, like mentioned here:
... However, this won't work well on pre-3.2 devices, because they
don't recognize sw600dp as a size qualifier, so you still have to use
the large qualifier as well. So, you should have a file named
res/layout-large/main.xml which is identical to
res/layout-sw600dp/main.xml. In the next section you'll see a
technique that allows you to avoid duplicating the layout files this
way.
You should also take a look here:
Preparing for Handsets
and
New Tools For Managing Screen Sizes
Make Your Layout like this:
res/layout/main_activity.xml # For handsets (smaller than 600dp available width)
res/layout-sw600dp/main_activity.xml # For 7” tablets (600dp wide and bigger)
res/layout-sw720dp/main_activity.xml # For 10” tablets (720dp wide and bigger)
For TAB:
For example, if your application is only for tablet-style devices with a 600dp smallest available width:
<supports-screens
android:requiresSmallestWidthDp="600" />
I have Tablet with 7" screen (600×1024) with hdpi (240 dpi classification) which is comes under the Normal Screen see my screen shot.Its working fine with 7" screen (600×1024) with mdpi (160 dpi classification) which is comes under the large screen.
For Tablet .
MULTIPLE SCREENS:
For example, the following is a list of resource directories in an application that provides different layout designs for different screen sizes and different bitmap drawables for medium, high, and extra high density screens.
res/layout/my_layout.xml // layout for normal screen size ("default")
res/layout-small/my_layout.xml // layout for small screen size
res/layout-large/my_layout.xml // layout for large screen size
res/layout-xlarge/my_layout.xml // layout for extra large screen size
res/layout-xlarge-land/my_layout.xml // layout for extra large in landscape orientation
res/drawable-mdpi/my_icon.png // bitmap for medium density
res/drawable-hdpi/my_icon.png // bitmap for high density
res/drawable-xhdpi/my_icon.png // bitmap for extra high density
Hope this will help you.
In android, we use resolution in dp to measure the screen size, not resolution in px.
Both of your two tablets have the same resolution in px, but their resolution in dp are quite different.
600 X 1024px with mdpi = 600 * 1024 dp
600 X 1024px with hdpi = 400 * 682 dp
You use sw600dp as the qualifier for tablet, which will effect the first device but not the second one.
In fact, the second device(400 * 682dp), is much more like a handset rather than a tablet, it should not use the layout for tablet.
i have a problem in android development that bored me. my problem is screen size and dealing with that. specially i have some problems with images. for example i want to create a background image for my activity that i created in photoshop and my background image contains a "HELLO" word on it. but when i put it on drawable-xhdpi folder, it seems blurry and its not sharp!! my phone is a nexus 4 and according to Google documentation i create background image in 640 x 480 size.
when i create background image in 960 x 720 size it seems better but not perfect. in this case my image file size is very high!
but what is the standard way for this? please help me to solve this problem for ever. i read google documentation but its not solve my problem!
http://developer.android.com/guide/practices/screens_support.html
You should usually avoid creating images for certain screen sizes to make them background, because there are thousands of different devices and you would have to create dozens of such images.
The first thing you need to be aware of is screen density.
Generally you create 3 to 5 images when not even looking at screen size: low (120 dpi), medium (160 dpi), high (240 dpi), extra high (320 dpi) and 2*extra high (480 dpi). These go into drawable-Xdpi folders, where X is one of l, m, h, xh, xxh.
Next thing when you want to have bigger images on bigger screens (bigger phones, small and big tablets), you may want to put images to folders like drawable-sw600dp-Xdpi. This is not a case for your phone.
Nexus 4 is a xhdpi 640x384 dp device, but you should not treat it differently than Samsung Galaxy S2 (hdpi 533x320 dp).
Create an image of smaller size for both phones and center it horizontally. E.g. 320x100 px for mdpi, 480x150 px for hdpi and 640x200 px for xhdpi (your phone).
the screen resolution for Nexus is 1280x768 (http://www.google.com/nexus/4/specs/), resize the image to this resolution. In especial consideration some images can't handle the resolution and the image became disproportionately.
for interesting
resolution calculator:
http://members.ping.de/~sven/dpi.html
This is problem of Android Fragmentation and you just cannot deal with it perfectly as there is a several hundreds different devices. As colleague above wrote Nexus 4 has resolution -1280 x 768 so for sure res of image as equal as 960 x 720 is good choice. I'm even surprised that google suggest 640 x 480 for xhdpi, it's definitely too less.
So as I said you are not able to make perfect looking graphics for all existing devices. You should choose the most popular devices from every screen category(xhdpi,mdpi,ldpi ... etc) to cover the most important market share.
With 1600+ android models even after they are categorized in few Screen size and a few DPI's its very difficult to manage layouts.. i suggest that you just concentrate on designing layouts w.r.t to screen size and then create views as Resizeable Views to neglect density effects.
Once you have created your layouts Resize the Views .. You can create a Custom View or resize on its onMeasure();
am trying to build android app with a dynamic layout to support multiple screens
am thinking about build all my app for a certain device with a fixed dimensions like Motorola Droid , say that the button will be 50 width 50 height
and after i finished all the app , i will reedit the sizes of elements to be a ratio betweetn the right place of them and the dimensions of the new device
also Drawables will be Scalable Drawables and i will use only the Drawables folder and remove others :
- drawable-hdpi
- drawable-ldpi
- drawable-mdpi
so i will have only one Drawables folder and only one layout xml file for every activity
and most of my layouts will be hard coded using java
so the question is : is it a true method for development ?!
may be you will ask , why ?
so my answer is , as i think my method is easer than using :
res/layout/my_layout.xml // layout for normal screen size ("default")
res/layout-small/my_layout.xml // layout for small screen size
res/layout-large/my_layout.xml // layout for large screen size
res/layout-xlarge/my_layout.xml // layout for extra large screen size
res/layout-xlarge-land/my_layout.xml // layout for extra large in landscape orientation
res/drawable-mdpi/my_icon.png // bitmap for medium density
res/drawable-hdpi/my_icon.png // bitmap for high density
res/drawable-xhdpi/my_icon.png // bitmap for extra high density
The simplest (and probably the best) way to create layouts that support all screen sizes is to use a RelativeLayout.
AFAIK you can manage resoution variation of different devices by keeping the images in drawable-hdpi,drawable-mdpi,drawable-ldpi folder.
then screen sizes by providing different layout for different screen size categories by specifying layout-small,layout-large,layout-xlarge.
also you can increase number of devices supported by using nine patch and relative layout check this google official documentation regarding this topic.
hope this help.
thanks.