1

In my current code i'm getting the width of the screen and using it in my breakText as the maxWidth.

    Display display = getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    int width = size.x;

    Paint p = new Paint();
    p.setTextSize(60);
    p.setSubpixelText(true);

endChar = p.breakText(fullText, 0, endText, true, width, null);

This code isn't giving me the desired answers, it is giving more characters than needed, but i guess I'm using the maxWidth wrongly. Can someone explain what should be entered in maxWidth?

Steve Cortis
  • 522
  • 1
  • 6
  • 25

1 Answers1

0

Remember about density of screen and scale factor for text on device. You should divide your screen width in px by scaledDensity to get screen width in sp.

It should look like that:

    Display display = getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    DisplayMetrics metrics = getResources().getDisplayMetrics();
    int width = (int)((float)size.x/metrics.scaledDensity);

    Paint p = new Paint();
    p.setTextSize(60);
    p.setSubpixelText(true);

    endChar = p.breakText(fullText, 0, endText, true, width, null);
Warcello
  • 418
  • 7
  • 13