In my XML I can set textSize="18sp". Is it possible to change the scale of the sp unit which would affect all sp sizes but not affect dp sizes?
It is currently not possible. It was meant to be a user setting but it hasn't been exposed in the UI so far.
Related
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.
Hi Iam developing android Titlebar , so I need to clarify which is use to android text either sp or dip
or
Which is standard and what is the difference between sp and dip? Thanks!
SP is always used for textSize
DP is used for pretty much everything else
See "Supporting Different Densities" for more info.
From the docs:
One common pitfall you must avoid when designing your layouts is using absolute pixels to define distances or sizes. Defining layout dimensions with pixels is a problem because different screens have different pixel densities, so the same number of pixels may correspond to different physical sizes on different devices. Therefore, when specifying dimensions, always use either dp or sp units. 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).
sp is used for text size and dp for everything else. These are mainly to achieve the best support for multiple screens with different sizes and density
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 :)
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
I'm designing an android activity which I want it can be compatible with all screens resolutions. Right now, I did it knowing that the screen will have 1024x600pixels, and in the layout_weidth and height, I used absolute pixels... (I started with dp's but it doesn't work in my mobile, but it works in the emulator... very confusing, so I decided to try with pixels and they worked) but if I get another resolution, then it crash...
So I though in creating a xml with percents of the actual resolution of the screen, so it can be compatible with all the screens...
But thoughting, what can I do with the textSize, for example? How can I make it compatible?
I need some advice... thanks
Using Relative layout is a better option
Use dp - density independent pixels for UI components and sp for Text sizes
sp
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.