Is it possible to change the unit for Paint.setTextSize()
? As far as I know, it's pixel but I like to set the text size in DIP for multiple screen support.
5 Answers
I know this topic is old and already answered but I would like to also suggest this piece of code:
int MY_DIP_VALUE = 5; //5dp
int pixel= (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
MY_DIP_VALUE, getResources().getDisplayMetrics());
Convert it like this
// The gesture threshold expressed in dip
private static final float GESTURE_THRESHOLD_DIP = 16.0f;
// Convert the dips to pixels
final float scale = getContext().getResources().getDisplayMetrics().density;
mGestureThreshold = (int) (GESTURE_THRESHOLD_DIP * scale + 0.5f);
// Use mGestureThreshold as a distance in pixels
from here http://developer.android.com/guide/practices/screens_support.html#dips-pels
-
1another simalar approach that uses a resource file stackoverflow.com/a/5369766/1815624 Jan 31, 2016 at 14:07
-
The accepted answer is for gestures, not setting text size. The highest voted answer (at the time of this writing) is close, but the documentation recommends using sp
rather than dp
because in addition to being scaled for screen densities (as dp
values are), sp
is also scaled according to user preferred font sizes.
From an int
in code
int spSize = 17;
float scaledSizeInPixels = spSize * getResources().getDisplayMetrics().scaledDensity;
mTextPaint.setTextSize(scaledSizeInPixels);
Or alternatively
int spSize = 17;
float scaledSizeInPixels = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,
spSize, getResources().getDisplayMetrics());
mTextPaint.setTextSize(scaledSizeInPixels);
From resources
Or if you have the sp
or dp
value in resources:
<resources>
<dimen name="fontSize">17sp</dimen>
</resources>
with
float scaledSizeInPixels = getResources().getDimensionPixelSize(R.dimen.fontSize);
mTextPaint.setTextSize(scaledSizeInPixels);
Other links
-
I tested your suggestions but there's the same problem with all of them: E.g. font size 22 looks fine on a Pixel XL (1440x2560, 560dpi) but on a Nexus One (480x800, hdpi=240dpi) it's tiny/not readable anymore. Someone else suggested this but it causes the same thing to happen. It might be best to just use a hardcoded pixel size (e.g.
myPaint.setTextSize(80)
) because this way the text will be the same real-life width (e.g. 0.5") on every device (the canvas scales it!) - even though it obviously won't scale with Android's "font size" setting.– NephMay 14, 2020 at 12:56 -
@Neph, It's been a while since I've worked on this. Does setting the
dp
value to80
give the same result as usingmyPaint.setTextSize(80)
?– SuragchMay 15, 2020 at 0:37 -
I honestly didn't expect to get an answer after all this time, so thanks for that! You mean, use ` <dimen name="fontSize">17sp</dimen>` with
dp
instead ofsp
? I just tested it withdp
too but I get the same result for both:22sp
/22dp
give me appr. the same result asmyPaint.setTextSize(80)
- at least with the bigger Pixel XL. On the smaller Nexus One the text is way too small to be readable.– NephMay 15, 2020 at 9:37 -
And here is even shorter method to convert dp-s to px-els taking display metrics into account
If your Paint object is being used to draw text on a Canvas, you can let the Canvas handle scaling for you.
When calling Canvas.drawText()
the text size is first determined by the passed in Paint
object, which can be set via Paint.setTextSize()
. The text size is automatically scaled by Canvas
based on the canvas density, which can be found using Canvas.getDensity()
.
When setting the text size on a paint object that will be drawn on Canvas, work with a unit value of dp
or sp
and let Canvas handle the scaling for you.
-
2In
Paint.setTextSize(float textSize)
thetextSize
is a pixel value. It doesn't seem certain to me that this gets automatically scaled. If it did then I'd think the documentation would say that the units aresp
ordp
rather than pixels.– SuragchFeb 6, 2017 at 9:37 -
My app sets a map marker using
Canvas.drawText()
. I tested different solutions but suggestions likegetDimensionPixelSize(R.dimen.fontSize)
don't work properly:<dimen name="fontSize">22sp</dimen>
looks fine on a Pixel XL (1440x2560, 560dpi), on a Nexus 4 (768x1280, xhdpi=320dpi) it's noticeably smaller but on a Nexus One (480x800, hdpi=240dpi) it's tiny/not readable anymore.TextView
s are scaled automatically but the canvas handles certain scaling too: It won't react to changes in Android's font settings but it'll ensure that the text is the same size on every device (e.g. 0.5" wide).– NephMay 14, 2020 at 12:03