Android Layout Hints

I’m currently experimenting with the code of Bad Metronome. Since I’m developing natively on a tablet, I need to create the UI by manually editing the XML files, without the support of a user inteface designer tool. Which turned out to be a rather time consuming task. Here are a couple of hints.

Dynamic distribution of widgets

I just wanted to stack three UI elements one above another. I’m using one of the most basic layouts, the LinearLayout in vertical direction. To distribute the widgets equally over the available height, regardless of the screen’s size (handset, tablet), I added empty views above, below, and between the widgets with only the following attributes:


android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"

TextView height

One of the three widgets is a TextView. The amount of lines changes during runtime from one up to three. This means the other user interface elements are repositioned while the user interacts with the app. Fortunately, the TextView provides attributes for the amount of minimum and maximum lines:


android:layout_width="wrap_content"
android:layout_height="wrap_content"

android:maxLines="3"
android:minLines="3"

Icons for ToggleButton

The ToggleButton supports different icons depending on its state. Those can be changed using a selector:


android:background="@drawable/play_button_selector"

The content of the selctor file may contain the following lines, where the icons name are just files within the resource folders:



android:state_checked="true"
android:drawable="@drawable/icon_stop"

android:state_checked="false"
android:drawable="@drawable/icon_play"