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
Related
I have textviews inside third party component and the parent container for this is RelativeLayout.Is there any way to programmatically set all Textview Text Size to be static I mean not change when the user uses Big fonts or big zoom from his phone settings
Scale-independent Pixels - sp means it is scaled by the user's font size preference.
Density-independent Pixels - dp is fixed size mainly used for Layouts.
If you need the same TextView size in different phone regardless of user font size preference then you should use dp.
Use this,
textView.setTextSize( TypedValue.COMPLEX_UNIT_DIP, intSize);
But sp is recommended for TextView
Try this :
text.setTextSize(TypedValue.COMPLEX_UNIT_DIP,14);
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.
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 :)
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
The app design that I'm implementing requires icon sizes to be a function of font size. The height of the icon should roughly match the height of the font.
It appears to be possible to specify any view size in sp.
Google has decreed that layout sizes should not be specified in sp but is there anything to stop me specifying an ImageView size in sp? It appears that nobody else is doing this and I'm worried about falling into unforeseen traps.
You could use the align options to size the icon
android:layout_alignTop="#+id/your_font_view"
android:layout_alignBottom="#+id/your_font_view"
and set android:scaleType to the appropriate value.
It would probably be best to be in a RelativeLayout as well
From android dev site
"A dp is a density-independent pixel that corresponds to the physical size of a pixel at 160 dpi. 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)."
Yes, you can safely specify the ImageView size in sp. This is not recommended for layouts because they typically scale to fit their content and specifying their size in sp would a) be unnecessary and b) probably look bad.