68

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.

Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
Yverman
  • 683
  • 1
  • 5
  • 5

5 Answers5

92

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());
sth
  • 222,467
  • 53
  • 283
  • 367
Felipe Caldas
  • 2,492
  • 3
  • 36
  • 55
51

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

CrandellWS
  • 2,708
  • 5
  • 49
  • 111
Alex Volovoy
  • 67,778
  • 13
  • 73
  • 54
45

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

Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
  • 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](https://stackoverflow.com/a/42728414/2016165) 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. – Neph May 14 '20 at 12:56
  • @Neph, It's been a while since I've worked on this. Does setting the `dp` value to `80` give the same result as using `myPaint.setTextSize(80)`? – Suragch May 15 '20 at 00:37
  • I honestly didn't expect to get an answer after all this time, so thanks for that! You mean, use ` 17sp` with `dp` instead of `sp`? I just tested it with `dp` too but I get the same result for both: `22sp`/`22dp` give me appr. the same result as `myPaint.setTextSize(80)` - at least with the bigger Pixel XL. On the smaller Nexus One the text is way too small to be readable. – Neph May 15 '20 at 09:37
  • Ok, I'm not sure what is going on then, but thanks for commenting. – Suragch May 15 '20 at 11:28
4

And here is even shorter method to convert dp-s to px-els taking display metrics into account

https://developer.android.com/reference/android/content/res/Resources.html#getDimensionPixelSize(int)

pelotasplus
  • 9,852
  • 1
  • 35
  • 37
4

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.

Tanner Perrien
  • 3,133
  • 1
  • 28
  • 35
  • 2
    In `Paint.setTextSize(float textSize)` the `textSize` 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 are `sp` or `dp` rather than pixels. – Suragch Feb 06 '17 at 09:37
  • My app sets a map marker using `Canvas.drawText()`. I tested different solutions but suggestions like `getDimensionPixelSize(R.dimen.fontSize)` don't work properly: `22sp` 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). – Neph May 14 '20 at 12:03