TextSize for different device with different size? - android

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));

Related

How to calculate dimen for different values folder

I have a dimen.xml defined in values folder
<dimen name="dimen_button_border_width">2dp</dimen>
<dimen name="version_txt_size">14sp</dimen>
How if I need to add dimen for different values folder like mdpi,hdpi,xxhdpi . What happens to the value of 2dp& 14sp in corresponding folders. Is there any mathematical formula ?
check out what these units are in here
in short: 1dp (density pixel) means 1*density_factor in px, no matter of folder
so 1dp on mdpi device is 1px, on xxhdpi device is 3*1px and so on
you don't need to put same integer in different qualifying folders, it will be calculated same way. but you may when you want e.g. bigger space on low-res devices (different value)
for example - you have thick line separator, very thin, height is 0.5dp. on xxxhdpi device it will be 0.5 x 4 = 2px. but same value for mdpi device will be 0.5 x 1 = 0px (because of int rounding) and your thick separator is gone... in this case you may put in values-mdpi and values-hdpi (1.5 factor) folders different, fixed value in px already instead of dp (in above example it should be 1px overriding 0.5dp -> 0px)
How if I need to add dimen for different values folder like mdpi,hdpi,xxhdpi
You do not need to add dimension resources for different density-based resource sets.
What happens to the value of 2dp& 14sp in corresponding folders. Is there any mathematical formula ?
14sp is the same in all densities. 2dp is the same in all densities. So, the mathematical formula is equality.

How to Supporting Multiple Screens dimension

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.

Size of textview in different screens Android

I'm getting sick to find the best away to put the same scale for textView's in different screens density and resolution.
I tried:
Create different folders, for different density and put "dimensions". Example, folder values-xhdpi and dimensions.xml, values-ldpi and dimensions.xml too. On my code, programmatically i get textView.setTextSize(getResources().getDimensions(R.id.myDimension);
In xml, 20sp
like example above, but put this textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimensions(R.id.myDimension);
Add textAppearence , like textView.setTextAppearance(this,android.R.style.TextAppearance_Large);
But all examples, i got what i wasnt expected, because got texts biggers in biggers density. like in image below
this happen except if i change my dimension in appropriate xml. But, what structure should i follow?
For an example, in drawbles, people reccomend to use scale like 3:4:6:8, but here i didnt follow a structre.
What are the best pratices ? What people normally do..?
What i want? this..
You have to use a pixel independent unit for your text size. Make dimensions with sp unit and load them programmatically.
For supporting multiple screens take a look in android documentation
For the sp unit says :
Similarly, you should prefer the sp (scale-independent pixel) to define text sizes.
The sp scale factor depends on a user setting and the system scales the size
the same as it does for dp.
In other words you have two
You have to declare in values file dimension elements. If you don't like the result and you want to declare for each screen category specific text size you have to do the following :
You can create a values folder for each dpi. For instance , the folder name can be : values-hdpi , values-xhdpi etc.
Inside the values file you can place a dimens.xml file in which you will declare as
<resources>
<dimen name="small_font_size"=10sp></dimen>
<dimen name="large_font_size"=20sp></dimen>
</resources>
It's up to you. You can create different dimensions files for different screen sizes. For example:
values-sw720dp 10.1” tablet 1280x800 mdpi and bigger
values-sw600dp 7.0” tablet 1024x600 mdpi and bigger
values-sw480dp 5.4” 480x854 mdpi and bigger
And take the value in sp for your TextView from your dimensions files.

What should be the text size ratio in android for multiple density device?

I am making an Android application for multiple screen device.
I want to know what should be the text size scale ratio for different density device?
example : if my text size is 28sp on mdpi device then what should be on hdpi device?
I made values-ldpi, values-mdpi, values-hdpi etc. and define text size in dimens of each respective values folder, but it did not look similar on all device.
You dont need to know the ratios, and you shouldnt directly use them since the amount of densities keep changin.
If you use it in the resources, using the sp unit will do it work fine in every density, without diferent value folders.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="type1">28sp</dimen>
</resources>
If you do it from code, probably it will look diferent depending on howyou asign the values. You have to send the correct values to the setTextSizeFunction, for example:
setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimensionPixelSize(R.dimen.type1));
//he documentation method that COMPLEX_UNIT_PX returns a Resource dimension value multiplied by the appropriate metric.
or with hardcoded numbers:
setTextSize(TypedValue.COMPLEX_UNIT_SP, 9);
Use below formula for declaration in dimens.xml
base mdpi= 1X
HDPI= 1.5X
XHDPI=2.0X
XXHDP=2.75X
above formula for relation b/W all Resolution

How to set text size of textview dynamically for different screens [duplicate]

This question already has answers here:
TextView.setTextSize behaves abnormally - How to set text size of textview dynamically for different screens
(8 answers)
Closed 6 years ago.
I am creating a textview and adding to the layout dynamically. I am using textView.setTextSize(18) method to set the text size.I tested it on samsung tablet and found that the font size is too small for this screen then I changed the textsize to 25 but it is too big for an emulator(480*800). My problem is to set text size dynamically so that it fits for all the screens.
EDIT:
And As I Search on StackOverflow now I found This Question is Duplicate of : This and This
You need to use another function setTextSize(unit, size) with unit SP like this,
tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18f);
Please read more for TypedValue constants.
You should use the resource folders such as
values-ldpi
values-mdpi
values-hdpi
And write the text size in 'dimensions.xml' file for each range.
And in the java code you can set the text size with
textView.setTextSize(getResources().getDimension(R.dimen.textsize));
Sample dimensions.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="textsize">15sp</dimen>
</resources>
There is probably no need to use the ldpi , mdpi or hdpi qualifiers in this case.
When you define a dimension in a resource file you include the measurement unit. If you use sp units they are scaled according to the screen density so text at 15sp should appear roughly the same size on screens of differing density.(The real screen density of the device isn't going to exactly match as Android generalises screen density into 120, 160, 240, 320, 480 and 640 dpi groups.)
When calling getResources().getDimension(R.dimen.textsize) it will return the size in pixels. If using sp it will scaled by the screen density,
Calling setText(float) sets the size in sp units. This is where the issue is,i.e you have pixels measurements on one hand and sp unit on the other to fix do this:
textView.setTextSize(TypedValue.COMPLEX_UNIT_PX,
getResources().getDimension(R.dimen.textsize));
Note you can also use
getResources().getDimensionPixelSize(R.dimen.textSize);
instead of getDimension() and it will round and convert to an non fractional value.
After long time stuck this issue, finally solved like this
textView.setTextSize(TypedValue.COMPLEX_UNIT_PX,
getResources().getDimension(R.dimen.textsize));
create folder like this res/values/dimens.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="textsize">8sp</dimen>
</resources>
In Style.xml pre-define the style:
<style name="largeText">
<item name="android:textAppearance">#android:style/TextAppearance.Large.Inverse</item>
<item name="android:textStyle">bold</item>
</style>
in code:
text.setTextAppearance(context, R.style.largeText);
I think You should use the textView.setTextSize(float size) method to set the size of text. textView.setText(arg) used to set the text in the Text View.
float currentSize = textEdit.getTextSize(); // default size
float newSize = currentSize * 2.0F; // new size is twice bigger than default one
textEdit.setTextSize(newSize);

Categories

Resources