10 Android layout tips for beginners
Therefore, this post is the first one from a series of tips I would like to give to everyone who is just starting to develop applications for Android. The tips will be based on my experience and on what I’ve learnt from more experienced developers over the time.
So without further ado, let’s start:
- Always use shapes (circles and squares) drawn in xml, whenever you can, for performance / flexibility reasons. It is always better to have an oval shape than several png/jpg files with circles.
- Forget about px and AbsoluteLayout as a measure because they are not your friends, unless you are developing for a single device. And even in such case, it is not advised to use them!
- Replace all the occurrences of fill_parent (deprecated on API 8) with match_parent. There are still many examples on the internet with it, when in fact they shouldn’t be used anymore.
- Learn how to use RelativeLayout because you will feel amazed with its flexibility when you need to position its child views. Layout_below, layout_above, layout_toLeftOf, centerInParent ( etc. ) can be really handy sometimes.
- Use LinearLayout when you need to perfectly distribute the space available. For instance, to distribute 3 circles evenly (horizontally or vertically) declare weigthSum = 0.3 on the LinearLayout and then set layout_weigth = 0.1 on each of the 3 children views.
- When dealing with icons, use wrap_content in both width and height of the ImageView and then use margins and paddings to reposition it wherever you within the layout. Remember that if you use match_parent the image will stretch itself, which can lead to poor visual results.
- Use Google’s tools to generate icons for the various screen dimensions in case you have them only in one size.
- Avoid setting fixed dimensions on the layout files (ex: layout_margin = 26dp ). Learn how to use the dimens.xml files to set different sizes,paddings and margins for the various densities. This is especially handy because In case you need to make a small adjustment to a particular screen density (or even add a new density like xxhdpi), you just need to edit the dimens.xml file on the corresponding values folder and not the whole layout file.
- Use styles.xml files to define a particular style for a view. For instance, if you have a big form with a lot of edittexts with a specific border and background color, define a style on the styles.xml file and then reference it on the corresponding layout file. This will clean your layout code substantially.
- Write comments on the strings, styles and colors files to help you and your team know where those values are being referenced.
As a final note, I highly recommend you to use the preview tab available on Android Studio / IntelliJ. Seriously. This is probably the best advice anyone can give you. It will help you understand how views will be displayed when you are still writing their xml code.
PS: Did you know that a Button is a Textview just with a different style? Check the source code here