While testing for an application I found an unknown behavior. I have defined some certain font size for TextView I am using. Now if I go in Device Setting -> and Change Display-?>Font size then it reflect in application as well. Font get tiny/large as what set in settings.
Any suggestion how can prevent from this.
You might be using sp while defining font size in xml. To prevent this behavior you can use dp instead of sp.
An sp is the same base unit, but is scaled by the user's preferred
text size (it’s a scale-independent pixel), so you should use this
measurement unit when defining text size (but never for layout sizes).
Hope this helps :)
Related
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>
While defining a view in android, we use dp for margins, sizes, etc but for text sizes we use sp. sp is also same as dp but for texts. Why can't we use sp for everything then?
I know the difference in between the two. I am asking if sp is a superset of dp, why use dp at all? why not use sp to specifiy all sizes in views?
The reason we can't use sp for everything is that when we increase the font size from settings,we only want the text to resize and not the buttons and other views as well. So we use dp for the rest and just sp for the text.
The sp unit of measurement is used for fonts and is pixel
density dependent in the exact same way that dp is. The extra calculation that an
Android device will take into account when deciding how big your font will be,
based on the value of sp you use, is the user's own font size settings. So, if you test
your app on devices and emulators with normal size fonts, then a user who has a
sight impairment (or just likes big fonts) and has the font setting on large, will see
something different to what you saw during testing.
When I use
android:textSizes="20dp"
in my XML for a textView, I got a warning "Should use "sp" instead of "dp" for text sizes."
Why should "dp" not be used? What is the correct approach? How can I achieve same textsizes on different displays?
You should always use SP for fonts as it respects the user preferences. Here is an example
Lets understand it with the help of an example -
Text with SP and DP
Change the device text setting (Settings -> Display -> Font Size)
Now reopen the app and relook at the texts, You will see that the text which was using SP has different height than DP.
You can use sp and dp. As you know in Android settings you can change text size (Settings -> My device -> Display -> Font size). All your textView in sp would change after changing font size in settings, dp - would not change
As #GiruBhai shared,it is more convenient to use sp instead of dp for the text size since it can be changed -unlike dp - according to the user's preferences.Which may be fulfilling your users needs better.
More info. : Dimensions in Android
Source : developer.android.com
Hello I am making an android application where I am giving text size is in sp as recommended.
I am setting my font size to 25sp to the TextView. Suppose here if user pick "Large" font of the device from the settings of the device and later update it to "Normal" then my application font also vary according to that.
But Is there any calculation such that I can calculate what would be size of font if it is "Large" and "Normal" ?
Screenshot of the device font settings
Sure, you can get view dimensions, but you do not really need to care in most cases. If you set size in sp then it means you are aware what that unit means and you expect this behaviour. If you do not like fonts size being changed, simply use dp
I have an Android app that looks absolutely horrible if the user sets their font size to large or extra large (via Settings -> Display -> Font size in Ice Cream Sandwich). It just plain wasn't designed for variable font sizes, and it makes a lot of the text unreadable.
I've seen applications that preserve the font size for most views, so I know that there has to be a way to do this. Is there a simple way for me to tell the application to ignore the user's font size preference? And if there isn't, how would you suggest that I go about calculating the font sizes? If nothing else, is there a way for me to retrieve the user's font size preference?
I think what you want to do is specify your font sizes in "dp" instead of "sp". All sp units take user font size preferences into account when adjusting their size, while dp units only calculate size based on the device's pixel density.