I now have the text size set to mPaint.setTextSize(15) , but I need it to be 15sp , how should I translate / specify 15sp , what should I do to display the correct size on the screen correctly? I tried to use getResources().getDimension(R.dimen.letters_height) , but the inscription on the screen is less than 15sp.

    1 answer 1

    Paint.setTextSize() takes the value of the text size in pixels. To set the size in other units, you need to convert them to pixels. This is done like this:
    from resources:

     int sizeInPx = context.getResources().getDimensionPixelSize(R.dimen.letters_height); 

    From the number:

     int sp = 20; int px = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, sp , getResources().getDisplayMetrics()); 
    • one
      @Lucky_girl, TypedValue contains constants for all types. Look over there - Vladyslav Matviienko