Chapter 1. Working your way around layouts
In this chapter, we’ll cover tips and recommendations for Android layouts. You’ll learn how to create certain types of layouts from scratch as well as how to improve upon existing ones.
At an Android talk I gave to a group of developers, when I was explaining how to create a view using an XML file, someone asked, “What should I write if I want a button to be centered and 50% of its parent width?” At first I didn’t understand what he was asking, but after he drew it on the board, I understood. His idea is shown in figures 1.1 and 1.2.
It looks simple, right? Now take five minutes to try to achieve it. In this hack, we’ll look at how to solve this problem using the LinearLayout’s android:weightSum attribute in conjunction with the LinearLayout’s child android:layout_weight attribute. This might sound like a simple task, but it’s something I always ask about in interviews with developers because a lot of them don’t know the best way to do this.