[android] What is the difference between "px", "dip", "dp" and "sp"?

Definitions

px or dot is a pixel on the physical screen.

dpi are pixels per inch on the physical screen and represent the density of the display.

Android gives alias names to several densities

  • ldpi (low) ~120dpi
  • mdpi (medium) ~160dpi
  • hdpi (high) ~240dpi
    • most devices in 2015 are here
  • xhdpi (extra-high) ~320dpi
    • Apple iPhone 4/5/6, Nexus 4
  • xxhdpi (extra-extra-high) ~480dpi
    • Nexus 5
  • xxxhdpi (extra-extra-extra-high) ~640dpi

dip or dp are density-indenpendant pixels, i.e. they correspond to more or less pixels depending on the physical density.

  • 1dp = 1px on mdpi

enter image description here

sp or sip is a scale-independant pixel. They are scaled when the Large Text option is turned on in Settings > Accessibility

  • 1sp = 1dp
  • 1sp = 1.2dp with accessibility Large Text

What to use?

Use sp for Text size.

Use dp for everything else.

Examples related to android

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How to implement a simple scenario the OO way My eclipse won't open, i download the bundle pack it keeps saying error log getting " (1) no such column: _id10 " error java doesn't run if structure inside of onclick listener Cannot retrieve string(s) from preferences (settings) strange error in my Animation Drawable how to put image in a bundle and pass it to another activity FragmentActivity to Fragment A failure occurred while executing com.android.build.gradle.internal.tasks

Examples related to android-layout

How to check if a key exists in Json Object and get its value How to center the elements in ConstraintLayout Android - how to make a scrollable constraintlayout? Add ripple effect to my button with button background color? This view is not constrained vertically. At runtime it will jump to the left unless you add a vertical constraint Is it possible to put a ConstraintLayout inside a ScrollView? Differences between ConstraintLayout and RelativeLayout How to remove title bar from the android activity? How to have EditText with border in Android Lollipop Android: ScrollView vs NestedScrollView

Examples related to user-interface

Calling another method java GUI How do I center text vertically and horizontally in Flutter? Is it possible to put a ConstraintLayout inside a ScrollView? How to change color of the back arrow in the new material theme? How to create RecyclerView with multiple view type? Android RecyclerView addition & removal of items tkinter: how to use after method Presenting a UIAlertController properly on an iPad using iOS 8 Android ViewPager with bottom dots How do I get the height and width of the Android Navigation Bar programmatically?

Examples related to dimension

Load dimension value from res/values/dimension.xml from source code What is the difference between "px", "dip", "dp" and "sp"?

Examples related to units-of-measurement

Should I use px or rem value units in my CSS? Converting bytes to megabytes What is the difference between "px", "dip", "dp" and "sp"? Why em instead of px?