Which unit of measurement does the Paint.setTextSize(float) use?

I want to draw text with an specific height(in pixels) on a view using Canvas. Can you simply use Paint.setTextSize(float) with the number of pixels or is this using dp or sp?

  • Android - multiple OnClickListener?
  • DDMS files not found Error
  • Rotate ImageView source from layout xml file
  • Eclipse Android Change API Level
  • Android - Sliding menu with sub menu
  • cannot access memory at address 0X1 after setting up gdb and eclipse to debug shared library from Android Application
  • Related posts:

    Android Studio run configuration for ORMLite config generation
    SharedPreferences application context vs activity context
    onGlobalLayout differentiate between various invocations
    ADB - Android - Getting the name of the current activity
    Way to parse XML (org.w3c.Document) on Android
    Android - Creating listview with floating headers
  • Action Bar Icon hidden after SearchView expands
  • android espresso testing : empty test suite. no tests were found
  • Handling onActivityResult from outside the Activity
  • Resume playing VideoView from onResume
  • Calling context.getResources() returns null
  • Android: access saved google locations programmatically
  • 3 Solutions collect form web for “Which unit of measurement does the Paint.setTextSize(float) use?”

    It uses pixels, but you can convert it to dp using this code:

    double getDPFromPixels(double pixels) {
        DisplayMetrics metrics = new DisplayMetrics();
         case DisplayMetrics.DENSITY_LOW:
                    pixels = pixels * 0.75;
         case DisplayMetrics.DENSITY_MEDIUM:
                     //pixels = pixels * 1;
         case DisplayMetrics.DENSITY_HIGH:
                     pixels = pixels * 1.5;
        return pixels;

    The easiest way to get a px value for such methods is to simply define the appropriate dp or sp value in the dimens.xml file and retrieve it like this:

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

    You actually have 3 methods available to use depending on your needs:

    • getDimension() Returns a float in pixels.

    • getDimensionPixelSize() Returns an int in pixels. This is the same as getDimension(), except the returned value is ROUNDED to the nearest integer value and it ensures that a non-zero input value results in a non zero output value (eg 0.1 is returned as 1, not 0).

    • getDimensionPixelOffset() Returns an int in pixels. This is the same as getDimension(), except the returned value is TRUNCATED (ie. rounded down). The result may be zero.

    These methods include DisplayMetrics that can be added into future Android SDK.

    Pixels to dip:

    public static float getDipFromPixels(float px) {
            return TypedValue.applyDimension(

    Dip to pixels:

    public static float getPixelsFromDip(float dip) {
            return TypedValue.applyDimension(
    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.