A few simple observations about what metrics are used in Android to define the width of buttons, the size of the text etc.
One of the questions the students often ask is whether it is necessary to use pixels to set the width of the different elements present in a View layout.
Android Well, the world is different from the web world, so you have to apply the general rule is to NEVER use pixels to define the unit of measurement of the elements of a layout.
What do you have to use instead of pixels? Here are the two units are to learn dp and sp:
- dp (Density-Independent pixels) – As the name implies, is an independent unit of the pixel density and the screen size and ensures uniformity between devices. This unit will need to always use for each component, except for the text.
For example, you could write:
android:layout_height="60dp" – Imposed height to 60 “pixels”. The actual number of pixels used will depend on the size of the screen of the user’s device and the density, but this way I guarantee that it is always set correctly whatever the screen that will display it.
- sp (Scale-Independent pixels) – Unit used for text. It takes into account the screen size and density, as well as user-selected text settings.
android:textSize="21sp" – Set the text to a size of 21 “pixels”. The actual number of pixels here also depends on the screen size, the density, as well as the settings made by the user for text.
Remember therefore to always use these two units during the design of your layout.
Did it serve a little, Article? Share it NOW on social! Thank you 🙂
How useful was this post?
Click on a star to rate it!
Average rating / 5. Vote count:
We are sorry that this post was not useful for you!
Let us improve this post!
Thanks for your feedback!