TextView size differs in real device and emulator - android

None of the previously asked questions and solutions worked for me.
For the methods setTextSize(), when I give hard coded value as below:
textView.setTextSize(16)
the text appears to be of same size on emulator and device.
However, when I give the size in dimensions file:
<resources>
<dimen name="default_text_size">5sp</dimen>
</resources>
and I have tried 2 approaches programatically for setting this size:
defaultTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, getContext().getResources().getDimension(R.dimen.default_text_size));
and
defaultTextView.setTextSize(getContext().getResources().getDimension(R.dimen.default_text_size));
Both of them show different size on real device and on emulator.
Please see attached screenshots.
Any idea how to fix this issue? I know that it will change based upon the screen resolution, but this is drastic change. The whole text has shrunk in the upper half of the ListView row in case of emulator. Which is not what I want.

Scale-independent Pixels (sp) - this is like the dp unit, but it is
also scaled by the user's font size preference. It is recommended you
use this unit when specifying font sizes, so they will be adjusted for
both the screen density and user's preference.
When you set the size unit of text (in TextView) as sp it becomes also dependent on the user/device font size preference. For this reason, on different devices your text may appear different.
If you want your text to appear everywhere same (regardless of the user's font size preference), use dp instead:
<resources>
<dimen name="default_text_size">5dp</dimen>
</resources>

Related

Text sizes, different density devices and material design

I'm trying my best to follow the material design guidelines when it comes to fonts as I can, but I'm struggling to fix an issue I'm having without using dimens dependant on density. My issue is;
I'm using styles for my fonts so an example of this;
<style name="Text.MyApp.Heading3" parent="TextAppearance.MaterialComponents.Headline3">
<item name="android:fontFamily">?attr/RegularFont</item>
<item name="fontFamily">?attr/RegularFont</item>
<item name="android:textSize">?attr/TextSizeHeadingThree</item>
</style>
And applying this as the style of the text, my font size comes from the material guidelines so for Heading 4 I have
<dimen name="text_size_heading_three">48sp</dimen>
My problem is, I've got a view where there's two numbers using Heading3 in a row on the screen, This may look like +£100.00 | -£200.00, these are constrained to a separator view in the middle.
Where my issue lies is on a Pixel 4 XL (xx-hdpi) this looks fine, but bringing this down onto a Nexus S (hdpi) the text squashes together, overlaps the separator in the centre and comes off the end of the screen slightly.
It was to my understanding of the use of SP is that it would scale this text size which doesn't appear to be happening, a worry also arises that SP takes from the users text size preferences so increasing the text size of the device will likely also break this.
Just short of changing the font size for hdpi to a smaller text size for this text view, is there a way I can make this style scale with the screen?
sp actually scales in regard to the screen pixel density and the selected font size of the system.
The issue you are facing is related to the screen's actual width instead.
To achieve what you want, you can try either of the following two:
Have different sp values for your dimen for each screen width. You can do this by creating additional resource files that would contain your dimen values and will be screen width dependant. You can read more about this here:
https://developer.android.com/training/multiscreen/screensizes#TaskUseSWQuali
Use an autosizing TextView. You can find more information here:
https://developer.android.com/guide/topics/ui/look-and-feel/autosizing-textview
sp units are literally about the user's choice of text size:
Scale-independent Pixels - This is like the dp unit, but it is also scaled by the user's font size preference. It is recommend you use this unit when specifying font sizes, so they will be adjusted for both the screen density and the user's preference.
Basically users can choose the general size of text on their device, and that multiplies dp values by a specific factor (Normal is * 1.0, I think Huge is * 2.0) and that's what the sp value is. When you specify sizes in sp, you're allowing the system to scale that text up so it's readable for everyone.
So you need to be mindful of that when designing - it's always worth having an emulator with a small screen (limited space) and the text size set to the largest setting, just to make sure everything still fits in those extreme cases.
You have four options I think:
reduce your font's sp size in the layout, so it always fits
make different layouts for different configs, and tweak that (more work for you)
use autosizing TextViews like #kandrit mentions - set the space you want to fill, and it will scale the text size to fit it (this sounds like what you were expecting, and you can tweak things like having a fixed set of possible sizes)
don't use sp, use dp instead (won't scale according to the user's preference - but above a certain size, it doesn't need to because it will be readable)
You'll get a warning about the last one, but in my opinion it's completely fine and the right thing to do in some cases. Think about an app with a clock in the middle of the screen - it's a fixed size, designed and laid out a certain way, and it's large enough to be easily read. It's meant to look a specific way, take up a specific area of the screen, and there's no need for it to change size - so it makes sense to define it in dp like any other visual element. Maybe that works for what you're doing!
Problem is not with your text size, it is with your layout design
I also had the similar problem I solved it by giving a margin to views which was placed inside constraint layout so in a case of large screen it will use constraints and ignore the value of margin and in case of small screen it use the value of margin to keep views separate
If you are using a Linear Layout then set layout_weight to 1 and give margins to views then it will work same as with constraint layout

Android Text Size support multiple screens

Is it best practice to vary your text size based on screen size or based on screen density, or both?
I already know to use sp. I have static title text that fits on one line in some screens, and wraps around to the next line on others. I need to avoid the word wrap.
Its good practice to do so. On an 10" Tablet you are better using bigger Text sizes.
You can for example define your Custom values for each type of device using the values-sw720dp
folder an place it there:
<dimen name="HeadlineSize">28sp</dimen>
Also the already System Values like android:style/TextAppearance.Large scale. Means on an Phone will this be smaller(eg. 18sp) than on an Tablet (24dp)

Recommended text size ratio between mobile and tablet devices?

What is recommended text size ratio between mobile and tablet device?
There is an app for a mobile device and I need to make the same for a tablet device. If font size in mobile app is 16dp, what should be in tablet app?
There must have been some multiplication ration which I can use to best decide on text size for a tablet app. Android design web site does not say much about this.
PS. I know that I can tweak each text by testing it on a device, but I am looking for a recommended ratio.
EDIT
not 16dp, but 16sp. Sorry!
Actually we developers need not worry about that ratio. I simply externalize font size values into res/values/dimensions.xml and use sp as a unit.
<resources>
<dimen name="my_font_size">16sp</dimen>
...
Scale-independent Pixels - this is like the dp unit, but it is also
scaled by the user's font size preference. It is recommend you use
this unit when specifying font sizes, so they will be adjusted for
both the screen density and user's preference.
Just saw your edit: Since sp already takes user prefs and dpi into account all you can do is to assign different font sizes via the resource system. And to my knowledge there is no single magic conversion number for tablets to phones. Just think about the multitude of different screen sizes out there.
You could however use a custom style which uses one of the theme defined values for text size.
Id like to hope that device manufacturers know which font size has good readability on their devices.
<style
name="MyTextStyle"
parent="#android:style/TextAppearance.Medium">
</style>
If you use dip's instead of pixels, it should automatically look right for both phones and tablets. Tablets are typically mdpi devices (scale factor 1).
if you're interested, you can find more information on the scaling factors for screens with different densities here: http://developer.android.com/guide/practices/screens_support.html

Dynamic Text size change according to tablet

With all effort,I finally reached to the end of my first app in android. And thanks to all. But after coming to end, I realized one thing that my app text size is common in all tablet sizes. So I will re frame my question.
Problem: I used my own custom text size in entire app. and some what satisfied with 7 inch tablet. But when I am looking the same thing in 8.9 inches and 10.1 inch tablet its containing lots of white spaces and text size are also relatively small. So is there some way that I can change the text size according to my tablet size??? It may look novice question but I am struggling with this because the same app which look wonderful in 7 inches, is loosing its essence in 8.9 and 10.1 inches. Or there is something else which I could do with my app for the same.
Probable Solution:- As my topic indicates is there some way to change the text size as tablet size changes. Either dynamically or any other way.
EDITED:: As per Cheeta answer, approach is correct but I can't do it for each layout buttons and textfields and other field. There are hundreds of field like this in single app. Can I do it in some one place and call it required attribute. Is custom tag is approach which I am looking for.Is it possible????
Any help will be appreciated.
Thanks in advance.
NOTE I have not reached to a answer but cheetah answer is somewhat leading to correct way. I am marking his answer as correct although there are few alignment issue with his answer. Any other answer is always welcome. Thanks
After checking some resources I finally came up with the following solution:
Within the layout-xml I define the size of the button-text with
a dimension declaration:
android:textSize="#dimen/campaign_textfontsize"
I created a dimens.xml in the "/values"-subdirectory with the corresponding value
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="campaign_textfontsize">18dp</dimen>
</resources>
then I created a second values-folder "/values-sw720dp" and copied the old dimens.xml into it. In this dimens.xml I adapted the fontsize value:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="campaign_textfontsize">24dp</dimen>
</resources>
Based on the current screensize android selects the proper dimens-value.
So without any code, font is adapted to the display- (and display related button-) size.
Hope this helps...
I was Having Same Problem but what i realized sp/db are not efficient for dealing with this kind of problem, I handled my alignment and font size related stuff programtically.
here are the steps, how i handled this problem.
Calculate Screen Width and Height of Your device
Use Set TextView.setTextSize(unit, size), where the unit parameter is TypedValue.COMPLEX_UNIT_PX and the size parameter is percent of the total width of your screen. Normally, for me, .5% of total width for font is suitable. You can experiment by trying other total width percentages, such as 0.4% or 0.3%.
This way your font size will be always suitable for each and every text/screen size.
no need to change textsize dynamically use sp for textsize it will automatically arrange the text size depending on the screen resolutions..like in phone.
android:textSize="10sp"
It's quite an old question,
but another modern solution can be really handy.
With the Support library, 26+ new text auto sizing is available.
In simple words, you define TextView size and text size gets automatically increased/decreased to match it's bounds:
<TextView
android:layout_width="match_parent"
android:layout_height="200dp"
app:autoSizeTextType="uniform"
app:autoSizeMinTextSize="12sp"
app:autoSizeMaxTextSize="100sp"
app:autoSizeStepGranularity="2sp" />
More on Autosizing TextViews
Add here the ability to set TextView size in a percentage of the screen with the ConstraintLayout, and you can create layouts which look almost identically on any device.

Android - text size on particular resolution - setting programatically

I am programming application, which will be used on various kinds of resolutions, but let's say, we would keep to the density. For medium, high and low I am setting it's own drawables, but it still doesn's solve my problem, so I am wondering, how to set values dependently on actual pixel screen density in Java code?
In this time I have to in XML set smaller letters and smaller textviews to fit it into low resolution screen, on the other hand, high density screen is used from about 60% and text fields are too small. I am wondering if there is some way how to, during the start of the program, in some If loop find out, what the resolution is and then set layout_height and textSize for items I want.
Thanks
edit: Solved myself - http://developer.android.com/reference/android/util/DisplayMetrics.html
well before moving ahead to set the text size programatically, I recommend you to set the Textsize property's value in dip instead of px. it seems that you have marked the textsize value in px.

Categories

Resources