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.
Related
I've been using dp for dimensions and sp for text size for 3 years now, ever since I've started developing apps for Android.
But recently, I've been joined with a couple people that don't believe in dp and sp saying "it doesn't show the same on all devices".
The method they've used is:
When setting the text size, you don't use 24 sp nor 24 dp, you set it as 24% of the screen width or height in pixels. in this way, the text is always the same size in pixels on all devices (including tablets).
How correct/valid is this method? What is the professional way to design? What is the opinion of a professional Android designer to this method?
Don't know how good % can replace dp, but don't use it as a text size.
sp is great because users can resize it by changing their preferred text size in the device settings. Don't take away this possiblity just because it makes your design easier for you.
Accessibility > Design
See this guide for reference: Support different pixel densities
When defining text sizes, however, you should instead use scalable pixels (sp) as your units (but never use sp for layout sizes). The sp unit is the same size as dp, by default, but it resizes based on the user's preferred text size.
Best unit for text is sp and for dimensions best unit is dp.
That is given perfect result of in which you want.
In most of developer use this unit for text and dimensions.
I try for your % but that is not work in my app.
dp stands for density independent pixels. Its goal is, to define a more or less physical dimension. E.g. 48dp are about the size if a finger tip, and that's why tappable icons (e.g. on ActionBar) should have that size.
So for UIs you should always use dp (and sp for texts). It also has the advantage, that you can show more data on a larger screen (e.g. more items in a list are visible etc.).
If you're using % of the screen size, you are doing the exact opposite. On a small device 24% it will be small (physically speaking) on a large tablet it will be large.
Using % might make sense if you're developing a game for example and you want the same image shown on every device, large or small.
DP/SP Does not claim to be the same on all screen sizes but about the same size across screen densities. The reason it is only about the same size is that density is broken up into categories (e.g. mdpi, hdpi, xhdpi...).
You don't want the text to be relative to the screen size because then the text will be really small for devices that have a small screen and really large for devices that have a large screen. Instead you want the font size to be comfortable to read and about the same size on large or small screen. The large screens will just have the advantage of more text on the screen at a time.
In addition as other users have mentioned sp (and apparently now dp) can be scaled based upon user preferences which is really helpful for people with poor eyesight.
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
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'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.
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.