How to Supporting Multiple Screens dimension - android

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.

Related

TextSize for different device with different size?

How can support text size for different devices with different screen size ? I have try to provide different dimension xml for different screen but it's not working efficiently.
values
values-large
values-small
values-sw600dp
values-sw720dp
values-sw800dp
Try this library, it may help you. CommonTextSize
In your layout add like this to mention size android:textSize="#dimen/_15sdp"
Your approach is accurate when you have values values-large values-small values-sw600dp values-sw720dp values-sw800dp etc folders. However i think you are missing to change textsize dimensions in values/dimens.xml values-large/dimens.xml values-small/dimens.xml values-sw600dp/dimens.xml values-sw720dp/dimens.xml values-sw800dp/dimens.xml.
For example you have used something like below dimens.xml in your values/dimens.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="text_size">18sp</dimen>
</resources>
In other values folder you need to change values for your text size.Also use sp for textsize not px or dp.
Please note that values values-large values-small are depricated after honeycomb.
You can also use the library as suggested in the other answer.However if you don't want to use an extra library then try this.
Programmatically
Here's a method which converts size of dp to the size according to screen size.
public float convertFromDp(int input) {
final float scale = getResources().getDisplayMetrics().density;
return ((input - 0.5f) / scale);
}
Now simply give the value to the textsize programically.
tvTextView1.setTextSize(convertFromDp(24));

Best practice for text size dimen declarations

I'd like to declare some dimensions for naming some common text sizes in my app. Are there any agreed upon standards or recommended best practices for the various res categories? Or, have there been any clever solutions for basing common text sizes dynamically at runtime on the current system default? For example, default text size is considered normal text. A smaller size is declared for minor text/descriptions, a slightly larger size for minor headlines, a much larger size for major headlines, etc.
In our code, We often set 3 or 4 steps for text size to keep it looks uniform, such as
<dimen name="textview_small_size">11sp</dimen>
<dimen name="textview_middle_size">13sp</dimen>
<dimen name="textview_big_0_size">21sp</dimen>
<dimen name="textview_big_1_size">30sp</dimen>
And for perfect UI appearance, You should define separate dimens in different DPIs and screen size like values-sw800dp-large-port.
So, just set the text size to make it looks perfect and keep a uniform rule.
There is no difference between set size in xml and in runtime, there're basically the same.
This might be a late answer but I feel it would still be helpful.
For different screen sizes, it is recommended to use dedicated resource files as mentioned here.
However, structuring content in a given resource file also proves useful (The same conventions can be used for text and dimension resources). Here, I use conventions similar to material colors
<resources>
<dimen name="normal_100">16dp</dimen>
<dimen name="normal_125">20dp</dimen>
<dimen name="normal_150">24dp</dimen>
<dimen name="normal_175">28dp</dimen>
<dimen name="normal_200">32dp</dimen>
<dimen name="normal_225">36dp</dimen>
<dimen name="normal_250">40dp</dimen>
<dimen name="normal_275">44dp</dimen>
</resources>
This greatly increases reusability and avoids declaring write-once-use-once dimension literals like
<dimen name="home_screen_banner_textLeftPadding">16dp</dimen>
For a complete descriptive article, see this.
Hope this helps! :)
You need to maintain different values folder for different device size and you can mention your text size in those values folder. Android will take appropriate text size as per device. For reference you can check pdf provided by GOOGLE IO. You will find how they are suggesting for using dimens.xml for different devices of android. For Example see the structure given below:
res/values/dimens.xml
res/values-small/dimens.xml
res/values-normal/dimens.xml
res/values-xlarge/dimens.xml
For Example
In values/dimens.xml
<dimen name="text_size">10dp</dimen>
In values-xlarge/dimens.xml
<dimen name="text_size">20dp</dimen>

Issues with font sizes on different dpis

I specify all font sizes in sp. The text looks good on several devices including Nexus 7, Galaxy S4 mini etc. However, on Galaxy S 3, the font is too large. Is this because the DPI of the S3 is so much more? If so, how do I adjust for this so that fonts display at relatively similar sizes?
DP/SP seems to be working properly. The larger font sizes I got was because of setting text sizes programmatically, using resource values which were already returned scaled - so essentially fonts were being double scaled. This could have been prevented by proper documentation by Android.
On my project i use different "dimens.xml" file for every type of density folder, for example:
values-mdpi
values-hdpi
values-xhdpi
dimens.xml has:
<!-- Text dimension for activity album preview -->
<dimen name="text_album_title">20sp</dimen>
<dimen name="text_album_author">18sp</dimen>
<dimen name="text_album_info">13sp</dimen>
<!-- Text dimension for row -->
<dimen name="text_track_title">16sp</dimen>
<dimen name="text_track_length">14sp</dimen>
And this is different for every file in folder. Is up to you to decide the size, you will see the difference between each dimension by using a layout with a TextView. Give it a try.
EDIT
I was totally wrong on this subject, thanks to Runloop and 323go.

Android Scaling Density

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.

android dimens value for different resolution devices

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

Categories

Resources