I'm using 16sp for text size in TabLayout on small devices i.e. Nexus 4 text is breaking to next line as it can't be fit to singleLine due to big text size , but on big devices i.e. Nexus5,Nexus10 it is showing text in singleLine and it doesn't look as much bigger. Though the unit of textSize is SP (scale-independent pixel) so should the text be auto resized on different screens? why text is not getting small o fit in singleLine in Nexus4 ? How does it actually behaves, i am confused at this point. Any clarity would be appreciable.
-
1read http://stackoverflow.com/questions/32860815/how-to-define-dimens-xml-for-every-different-screen-size-in-android/32861248#32861248 – IntelliJ Amiya Feb 23 '17 at 10:55
-
Thanks, wouldn't it resolve the problem for all devices if i use textAppreance to Small or Medium instead of textSize? – Usman Rana Feb 23 '17 at 11:08
-
No, textAppearance is just a style including attributes such as size, typeface, color and style. https://developer.android.com/reference/android/R.attr.html#textAppearance – Massimo Feb 23 '17 at 11:42
4 Answers
If you want to show same text size on every device just use pt
instead of sp
.You can follow the link https://stackoverflow.com/a/2025541/6891563 for getting detail about pt
and sp
.
I would suggest to do android:maxLines="1"
for text of single line. or you can follow the AutoFitTextView as this link https://stackoverflow.com/a/17786051/6891563.

- 1
- 1

- 57,232
- 27
- 203
- 212
When using sp
the size depends on two factors:
- the display density
- the user configured scale (very small, small, normal, big, very big)
So, when using sp
, the actual size depends also on user's configuration as well as on device display density (mdpi, hdpi, xhdpi, etc.).
Usually the device density scale factors are the following:
- mdpi: x1.0
- hdpi: x1.5
- xhdpi: x2.0
- xxhdpi: x3.0
- xxxhdpi: x4.0
These may change on some devices and have intermediate values. The actual value is the one stored in DisplayMetrics.density
.
dp = px * DisplayMetrics.density
sp = px * DisplayMetrics.scaledDensity (= px * DisplayMetrics.density * Configuration.fontScale)

- 3,436
- 4
- 40
- 68
-
same file of dimen handle values for all above mentioned densities , like if place 10dp then it will auto convert it to 10px,15px,20px and so on . Similarly shouldn't 16sp behave accordingly? – Usman Rana Feb 23 '17 at 11:04
-
1Yes, may you check that `Configuration.fontScale` returns 1.0 on Nexus 4? (inside of an activity do `getResources().getConfiguration().fontScale`) – Massimo Feb 23 '17 at 11:16
-
Check also that the same property does not have a value <1.0 on your Nexus 5 and Nexus 10. – Massimo Feb 23 '17 at 11:37
I think you need to check this Google IO Pdf for Design. In that pdf go to Page No:77 in which you will find how there suggesting for using dimens.xml for different devices of android for Example see Below structure :
res/values/dimens.xml
res/values-small/dimens.xml
res/values-normal/dimens.xml
res/values-large/dimens.xml
res/values-xlarge/dimens.xml for Example you have used below dimens.xml in values.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="text_size">18sp</dimen>
</resources>
In other values folder you need to change values for your text size .

- 141
- 1
- 12
sp doesn't change the font size with respect to device. If you want to use different font size on the basis of device size. Add font size to different dimen folders.
res/values/dimens.xml
res/values-small/dimens.xml
res/values-normal/dimens.xml
res/values-large/dimens.xml
res/values-xlarge/dimens.xml
And use font values from these dimen folders by @dimen/text_size
. Define text_size in all the dimen files with different value like <dimen name="text_size">18sp</dimen>

- 980
- 6
- 13