I am having an issue with scaling Android across multiple screens. According to the documentation Android OS will assume your dimens.xml is created for mdpi and scale accordingly for hdpi, xhdpi, etc...
However, I am not able to see this work. If I have a folder for values-hdpi my SGS4 looks fine with the 1.5 scale ratio applied from the dimens.xml file located in the values-hdpi. However, whenever I remove the values-hdpi folder from the project to allow Android OS to scale it up to hdpi, the SGS4 just runs mdpi values. I'm not sure what I'm doing wrong.
Android Manifest:
<!-- Screen Support -->
<supports-screens android:resizeable="true"
android:smallScreens="false"
android:normalScreens="true"
android:largeScreens="true"
android:anyDensity="true"/>
<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="15"/>
Folder structure includes
values
values-es
values-fr
values-sw600dp
values has the mdpi values stored in the dimens.xml file all done in dp and sp.
dimens.xml:
(NOTE*) Whenever I tried to add the xml snippet from the dimens.xml it kept formatting it funny and removing all the dimen name= stuff and just lists out the dp and sp used in it. Not sure why.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="center_text_margin_right">20dp</dimen>
<dimen name="answer_row_top_padding">7dp</dimen>
<dimen name="answer_row_bottom_padding">7dp</dimen>
<dimen name="answer_number_text_size">12sp</dimen>
<dimen name="answer_left_margin_from_answer_number">18dp</dimen>
<dimen name="answer_text_size">12sp</dimen>
<dimen name="answer_points_right_margin">20dp</dimen>
<dimen name="answer_points_left_margin">18dp</dimen>
<dimen name="answer_number_left_margin">60dp</dimen>
<dimen name="center_text_size">12sp</dimen>
<dimen name="center_text_margin_left">20dp</dimen>
<dimen name="center_text_margin_right">20dp</dimen>
<dimen name="answer_title_height">28dp</dimen>
<dimen name="round_circle_margin_left">6dp</dimen>
<dimen name="round_circle_height">6dp</dimen>
<dimen name="round_circle_width">6dp</dimen>
<dimen name="total_score_right_margin">26dp</dimen>
<dimen name="total_score_left_margin">5dp</dimen>
<dimen name="score_text_size">12sp</dimen>
<dimen name="trivia_logo_height">25dp</dimen>
<dimen name="trivia_logo_width">84dp</dimen>
<dimen name="trivia_logo_margin_left">8dp</dimen>
<dimen name="question_margin_left">26dp</dimen>
<dimen name="question_margin_right">26dp</dimen>
<dimen name="question_top_margin">15dp</dimen>
<dimen name="question_margin_bottom">20dp</dimen>
<dimen name="question_text_size">12sp</dimen>
<dimen name="fun_fact_margin_bottom">100dp</dimen>
<dimen name="fun_fact_text_size">12dp</dimen>
<dimen name="answer_row_height">25dp</dimen>
</resources>
If I create a values-hdpi folder with this file
dimens.xml
30.00dp
10.50dp
10.50dp
18.00dp
27.00dp
18.00dp
30.00dp
27.00dp
90.00dp
18.00dp
30.00dp
30.00dp
42.00dp
9.00dp
9.00dp
9.00dp
39.00dp
7.50dp
18.00dp
37.50dp
126.00dp
8dp
39.00dp
39.00dp
22.50dp
30.00dp
18.00dp
150.00dp
18.00dp
37.50dp
everything will work fine
The values-es and values-fr are just folders containing strings.xml NO dimens in there. I reference all throughout my layout xml files with #dimen/answer_row_height for example.
As I stated before it works perfectly if I drop in the values-hdpi with a scaled dimens.xml file in it, but if I remove it Android does not scale it. Has anyone hit this before or know what i'm screwing up?
Values will not be automatically scaled, only drawables will be.
It's up to you to define all the alternative resources (values included) that your app will need, the framework cannot and will not make those decisions for you.
According to the documentation Android OS will assume your dimens.xml
is created for mdpi and scale accordingly for hdpi, xhdpi, etc...
That is incorrect. Whatever you put in dimens.xml is going to be used as is, if the dimensions are in pixels (px). You'll need to put them in density-independent-pixels (dp) to make them scale. dimens.xml should not be based on resolution but on screen size. You might have a dimens.xml in values and in values-large, specifying different spacing, or even different font-sizes for larger screens.
You should not have a dimens.xml in values and values-hdpi, because the dp unit already takes care of density-scaling.
Based on the comments you left in various places, it appears that you are not accessing the dimensions resources correctly. You should be using something like:
getResources().getDimensionPixelSize( R.dimen. round_circle_height );
if retrieving values from code, or
#dimen/round_circle_height
if referencing from xml.
Related
I know this question might sound a bit odd. I am a newbie in Flutter Development. While making my app responsive I always face an issue while scaling the text. If I keep the font size constant (say 10 or 20) it sometimes looks too small on devices with large resolution or too big on small phones. Then I tried scaling it with respect to screen dimensions (like 4% or screen width etc) but then the question remains to scale it with respect to screen height or width ? Hence I want to know what is the ideal way for scaling the text in general.
Thankyou in advance for answering.
You can create dimension files and the sizes defined in these files will be used according to the device's dpi.
You will have to create:
res/values-mdpi/dimens.xml
res/values-hdpi/dimens.xml
res/values-xhdpi/dimens.xml
res/values-xxhdpi/dimens.xml
res/values-xxxhdpi/dimens.xml
Following are the ratios you would want to use for different screen size:
3:4:6:8:12 (m:h:xh:xxh:xxxh)
Let's say you have a device(hdpi), you want to set the text size to 12sp.
So you will set text sizes as:
mdpi - 9sp
hdpi - 12sp
xhdpi - 18sp
xxhdpi - 24sp
xxxhdpi - 36sp
Make sure the name should be same in all 'dimens.xml'
res/values-mdpi/dimens.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="title">12sp</dimen>
<dimen name="paragraph">9sp</dimen>
</resources>
res/values-hdpi/dimens.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="title">16sp</dimen>
<dimen name="paragraph">12sp</dimen>
</resources>
res/values-xhdpi/dimens.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="title">24sp</dimen>
<dimen name="paragraph">18sp</dimen>
</resources>
res/values-xxhdpi/dimens.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="title">32sp</dimen>
<dimen name="paragraph">24sp</dimen>
</resources>
res/values-xxxhdpi/dimens.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="title">48sp</dimen>
<dimen name="paragraph">36sp</dimen>
</resources>
I am using multi support value for dimension , I have given value like this
I have attached screen shot dimen folder please check this
I have make the value folder like
value
dimen.xml
<dimen name="padding_1">1dp</dimen>
and
values-sw320dp-hdpi
dminen.xml
<dimen name="padding_1">what padding should I give</dimen>
values-sw320dp-xhdpi
dminen.xml
<dimen name="padding_1">what padding should I give</dimen>
values-sw320dp-xxhdpi
dminen.xml
<dimen name="padding_1">what padding should I give</dimen>
values-sw360dp
dminen.xml
<dimen name="padding_1">what padding should I give</dimen>
values-sw480dp
dminen.xml
<dimen name="padding_1">what padding should I give</dimen>
values-sw600dp
dminen.xml
<dimen name="padding_1">what padding should I give</dimen>
values-sw720dp
dminen.xml
<dimen name="padding_1">what padding should I give</dimen>
My question is that what padding dimension should I give so I can support to different different device, like how to find the 1dp for every value folder.
I have go through with this link
https://developer.android.com/training/basics/supporting-devices/screens.html
but not understand clearly. I have gone through the Dimenify plugin as well but not getting clear solution.
Edit
I wanted to know is there any way to put exact dimension in all value bucket or
any dp converter which convert exact dimension for all value bucket in android
values folders to create
values-sw320dp,
values-sw480dp,
values-sw600dp,
values-sw720dp. You should create dimens.xml in all the values folders.
values-sw360dp
dimens.xml
<dimen name="padding_1">5dp</dimen>
values-sw480dp
dimens.xml
<dimen name="padding_1">8dp</dimen>
values-sw600dp
dimens.xml
<dimen name="padding_1">10dp</dimen>
values-sw720dp
dminen.xml
<dimen name="padding_1">13dp</dimen>
Based on your requirement you can give padding or any dimen values. As you
given, you should give same name for dimen in all the folders. Android will
automatically pick the dimen value based on your screen size.
A few tips to help you go with your answer.
The dp metric lets you define consistent sizes across device densities.
This would typically mean they look the exact same size on a majority of devices.
While giving a fixed width works for a few of the cases, visual appeal truly lies in the fact that you can scale down the size of your UI on smaller devices and scale it up on higher devices. You need to start using android's bucketing feature that pulls out different resources based on the device config with the same qualifying name.
Lets say you may choose for eg. to have margins
a margin_normal maybe visually perfect at 16dp for xxhdpi, but you see that 18dp works for xxxhdpi whereas 14dp works for xhdpi device.
These are subjective values but rest assured scale factors are a good approximation to this rule.
Dimenify deals with this conversion automatically but you need to provide the scale factors by trial and error and as a one time effort.
I'm trying to decrease the text size of a textview depending on the device the app runs. However I can't make it work.
I created the folder values-sw320dp (also tried values-w320dp) and put inside the dimens.xml file
<resources>
<dimen name="text_size">25sp</dimen>
</resources>
and my values/dimens.xml file contains
<dimen name="text_size">18sp</dimen>
What I expect is to load the app in Nexus One (emulator), which is 482 x 320 dp, and get the text size 18sp. However I get text size 25sp.
Does anyone know what is happening?
Thank you
Create folder as values-hdpi
dimens.xml for values-hdpi
<resources>
<dimen name="text_size">18sp</dimen>
</resources>
And Create folder as values-xhdpi
dimens.xml for values-xhdpi
<resources>
<dimen name="text_size">25sp</dimen>
</resources>
And use like this-
android:textSize="#dimen/text_size"
I use values-hdpi, values-mdpi, values-ldpi, values-xlarge
Create folder as values-sw600dp and values-sw720dp in res folder.
dimens.xml for values-sw600dp
<resources>
<dimen name="text_size">4sp</dimen>
</resources>
dimens.xml for values-sw720dp
<resources>
<dimen name="text_size">6sp</dimen>
</resources>
Nexus One has dimension 480 x 800, so try
values-sw360dp
I want setup different dimen-values for different resolution(dp) devices, how ?
just in my dimens.xml
<resources>
<!-- for 1280x720 dp ,Default screen margins, per the Android Design guidelines. -->
<dimen name="image_thumbnail_size">200dp</dimen>
<dimen name="image_thumbnail_spacing">2dp</dimen>
<dimen name="image_detail_pager_margin">160dp</dimen>
<dimen name="activity_horizontal_margin">80dp</dimen>
<dimen name="activity_vertical_margin">100dp</dimen>
<!-- for 640x360 dp -->
<!--
<dimen name="image_thumbnail_size">100dp</dimen>
<dimen name="image_thumbnail_spacing">2dp</dimen>
<dimen name="image_detail_pager_margin">80dp</dimen>
<dimen name="activity_horizontal_margin">40dp</dimen>
<dimen name="activity_vertical_margin">30dp</dimen>
-->
add:
I have two devices, the resolution is 1280x720 ,but my phone dp is 640x360 , and another is 1280x720
Create seperate values folders, each with a different suffex. Some examples:
values-normal (for phone versions)
values-large (for 7" tablets)
values-xlarge (for large tablets)
Place a dimensions.xml file in each of these folders, in each one, specify different values for each variable.
Hope this helps.
Sorry to get this to you late but you can use the same folder combinations that you use for drawables with the values folder. Here is an example
values-ldpi/dimens.xml
values-hdpi/dimens.xml
values-xhdpi/dimens.xml
I am creating an app that will be used for all screen sizes.. I have different layouts for small, normal, large, and x-large.. However, I also want my webview to adjust its sizes (text/images) depending on the size of the screen. I tried researching for viewport and values/dimens.xml but had no luck..Here is dimens.xml but I do not know how to apply it:
(This is for xlarge)
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="body_padding_large"> 20dp </dimen>
<dimen name="text_size_xlarge"> 32sp </dimen>
<dimen name="speaker_image_size"> 64dp </dimen>
</resources>