Issues with font sizes on different dpis - android

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.

Related

Why is there a layout preview difference between Pixel and Pixel 2 in Android Studio even though they have same screen size and pixel density

I have been trying to understand why these two devices even though having same screen size and pixel density behave differently.
Please Note: The Font size and Display size are set to "Default" on both the devices.
EDIT 1: I have created the following folders in the layout folder.
layout-hdpi
layout-xhdpi
layout-xxhdpi
layout-xxxhdpi
Each folder has the activity's layout with the dimension and image size fixed. My point is, if the two mobiles are using xxhdpi layouts or the Google Pixel is using xxxhdpi and Google Pixel 2 is using xxhdpi?
I think it depends on DPI of your phone. Two phones may have different dpi. You can change dpi if phone is rooted. I dont know if stock supports.
Please Note: The Font size and Display size are set to "Default" on both the devices.
No , You have to using dimens.xml for different devices of android for Example see Below structure :
res/values/dimens.xml
res/values-small/dimens.xml
res/values-normal/dimens.xml
res/values-large/dimens.xml
res/values-xlarge/dimens.xml
dimens.xml file contains
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="text_size">18sp</dimen>
</resources>
In Textview add this
android:textSize="#dimen/text_size"

Should we use different dimen files for different screen sizes

Currently, I'm developing Android app (phone only) and using only one size for different screen sizes, ie:
dimens.xml:
<dimen name="button_size">48dp</dimen>
<dimen name="text_size">16sp</dimen>
so in different screen sizes, we have only one size for components. And we go to this result: in small device, a textview can contain 10 chars but in larger device, a textview can contain 20 chars
And some developers use a gradle script to generate multiple dimens files in different folders based on the main dimens file like this:
values-sw320dp
dimens.xml:
<dimen name="button_size">48dp</dimen>
<dimen name="text_size">16sp</dimen>
values-sw480dp
dimens.xml:
<dimen name="button_size">52dp</dimen>
<dimen name="text_size">20sp</dimen>
...
so the system will use the dimens based on device size. And we go to this result: in small device and larger device, a textview can contain the same char, ie: 12 chars.
My question is: which one is better for UI, UX? (using Google Material Design)
You can use this library to support multiple screen dimen here
You can try this below, this will set automatically based on device.
?android:attr/textAppearanceMedium - For Medium font
?android:attr/textAppearanceSmall - For Small font
?android:attr/textAppearanceLarge - For Large font
Please check Material guidelines, To ensure usability for people with disabilities, give buttons a height of 36dp and give touchable targets a minimum height of 48dp.
Best practice is to use different dimens file for different devices. This will help you application view to be same across devices. If you keep same dimens for different devices then layout problem can also come. In some devices your layout will look perfectly fine but in another it will look very bad.
Android developer site also recommend to use different layout for supporting different devices.

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.

How to make font size bigger in tablet PC?

tv.setTextSize(14);
I set TextView font size to 14sp.
When I run my app in Nexus One, font size is ok.
But, when I run it in Nexus 7, font size is so small.
This is because Android scales font size according to density.
Nexus 7 density is lower than Nexus One, Android scaled font size down.
How can I adjust font size proportional to screen size?
Use an XML resource. In an XML resource file (I usually call mine 'dimens.xml'), define a dimension resource called something like font_size. Create a 'dimens.xml' file in the folders 'values-normal' and 'values-large'. The font size in 'values' will be used by default, but a large screen (like the Nexus 7) will use the size in 'values-large'. You could also define it in 'values-xlarge' if you wanted to explicitly support 10" tablets.
Settings the size can be done in XML with textSize="#dimen/font_size" and from code with tv.setTextSize(getResources().getDimension(R.dimen.font_size)).
Your dimens.xml file should look like this:
<resources>
<dimen name="font_size">14sp</dimen>
</resources>
Will be better if you use the dimens.xml approach, and generate different versions for phone and tablet in folder:
res/values/dimens.xml -> For default sizes
res/values-normal/dimens.xml -> For phone sizes
res/values-large/dimens.xml -> For tablet sizes (mind setting bigger values for font-size, may paddings, and so on...
You can set it from your dimes.xml
Define a dimension in the dimens.xml. Eg:
<dimen name="my_text_size">14sp</dimen>
And then use that in your code:
tv.setTextSize(getResources().getDimension(R.dimen.my_text_size));

How to use different font sizes on 480x800 and 1280x760 screen?

I have read Different font sizes for different screen sizes, but I found that is not working well for me.
I created a values-hdpi to define styles, but I found both 480x800 and 1280x760 use it -- the screen size is so different!
Is there any way to let 480x800 screen use one font size and 1280x760 use another one?
Yes.
Create screen-specific dimensions files, like this,
values-sw720dp/dimens.xml
values-sw600dp/dimens.xml
values/dimens.xml
The first is for 10" tablets, the second is for 7" tablet, and the last is for phones. If that doesn't give you the gradations you want, read this page to find out other ways of targeting different screens.
In those files, define a dimension,
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen
name="my_size">40sp</dimen>
</resources>
adjust the dimension value in each of the dimens.xml files to your liking.
Now in your layout,
<TextView ...
android:textSize="#dimen/my_size"/>

Categories

Resources