0

I've been struggling with this issue for a while:

I'm using canvas to draw text onto the screen, for my Android app. Right now, I'm using this to scale the text size:

float scaledTextSize = textSize * getResources().getDisplayMetrics().density;
paint.setTextSize(scaledTextSize);

It does scale a little bit, but doesn't fully keep its "size ratio" compared to the current screen dimensions. I've also tried the method using a dimension from a resource file, but nothing changed.

For example, if I set the text size to be suitable for a Nexus 5 screen, it would be too small for a Nexus 7, and a little bit too big for a Nexus 4.

Any help would be appreciated!

redside100
  • 63
  • 8

2 Answers2

2

This worked:

double relation = Math.sqrt(canvas.getWidth() * canvas.getHeight()) / 250;
paint.setTextSize((float) (textSize * relation));

Answer taken from this thread: Android: Canvas.drawText() text size on different screen resolutions

redside100
  • 63
  • 8
0

First convert text size from sp to pixel instead of using dp.

Scale-independent Pixels is used for specifying font sizes, so they will be adjusted for both the screen density and user’s preference.

int spSize = 12;//your sp size
  // Convert the sp to pixels
float scaledTextSize = spSize * getResources().getDisplayMetrics().scaledDensity;

then set paint setTextSize

paint.setTextSize(scaledTextSize);
sasikumar
  • 12,540
  • 3
  • 28
  • 48