Ahmad Sattout
Ahmad Sattout

Reputation: 2476

Best unit for text and dimensions in Android

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?

Upvotes: 1

Views: 1905

Answers (4)

Ridcully
Ridcully

Reputation: 23655

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.

Upvotes: 1

Kanaiyalal Badal
Kanaiyalal Badal

Reputation: 59

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.

Upvotes: 1

cyroxis
cyroxis

Reputation: 3711

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.

Upvotes: 0

Alexander Hoffmann
Alexander Hoffmann

Reputation: 6014

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.

Upvotes: 2

Related Questions