Scale-independent Pixels font size of text in Android - android

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

Related

why not just use sp for everything instead of dp?

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.

android - prevent text size change on device text size

I set font size 14sp for my textview in android , the problem is , when I change my phone text size , in the application it changes the size either , How can I prevent this ? How can I force text view to use the defined text size ?
When you declare a font with a size measured in sp ("scaled pixels") that measurement is supposed to be affected by the device font settings. That's fully expected.
If you want a font size that never changes with the font settings, use dp measurements instead.

How to keep Font Size constant irrespective of device font size or screen density

I have kept my EditText font size as 16sp. However when I change the device font size, the EditText font size also changes. I tried using dp instead of sp which worked but I doubt if it will work well on different screens. How to I keep font size constant using sp as the unit irrespective on device font size and screen density?
You should use dp. The only difference between dp and sp is that dp will ignore the user's text size setting for the device. The result will be that the font size measured in real-world units (points, inches, etc.) will be the same on all devices, regardless of pixel density.
Note that screen size never affects any of this. If you want the text size to be defined in terms of a certain fraction of the screen dimension (e.g., width), there's nothing built into Android's unit system to support that. You'll have to either do the calculation in code (taking into account both screen size in pixels and pixel density) or define the size using alternative resources for different screen sizes.

Prevent Font Size reflection in appllication

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

Ignoring ICS' font size setting

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.

Categories

Resources