Showing Android resources some love

Written by on April 13, 2011, 12:42 pm

I have ranted at length on some of the shortfalls I have found with Android development, particularly when compared with my iPhone exploits. However, there are some things about Android dev that I love, so I thought it was time I talked about them.

If you have done much iPhone development you will be pretty familiar with laying out UI in Interface Builder, or if you are one of those ''XIBs are the work of Satan'' guys, then laying out your UI in code. Either way it can get pretty hairy, pretty quickly. In IB you struggle with entering layout through the UI and in code you end up with your UI metrics spread across multiple source files.

Android has a much better solution to UI definition. If you are a web developer, you will find the XML layout definition language pretty familiar. All your UI is nicely contained in source control friendly, well structured XML files.

Let''s say I want to arrange a couple of text views and a custom button view vertically down the page:

<LinearLayout android:layout_width="1px" android:layout_weight="1" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:text="Some bold heading type text" android:layout_weight="1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:padding="10dp" android:textSize="16sp" android:textStyle="bold" android:textColor="@android:color/black"/> <TextView android:text="Some regular text" android:id="@+id/detail" android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="10dp" android:textSize="14sp" android:textColor="@android:color/black"/> <Button style="@style/Button" android:id="@+id/something_funky_button" android:text="Ow! Too Funky!" android:layout_height="88dp"> </Button> </LinearLayout>

A couple of things to notice about this snippet:

  • Define ids on your views so you can get to them from your code with View.findViewById. All the properties you can define here can be played with from the View subclass. So you have complete control over how things are laid out at design and runtime.
  • Padding, layout widths, heights, etc work predictably and intuitively. This makes scaling your app for various devices work out of the box most of the time.
  • The style attribute on the Button element refers to a separate style resource, so you can define commonly used UI elements in a single XML file and use it in all your layouts:
<style name="Button"> <item name="android:textSize">18sp</item> <item name="android:textColor">@color/button_text</item> <item name="android:textStyle">bold</item> <item name="android:padding">5sp</item> <item name="android:background">@drawable/button</item> <item name="android:layout_width">fill_parent</item> <item name="android:layout_height">wrap_content</item> </style>

I have just scratched the surface of what you can do here in your Android project''s resource directory, but it is all pretty awesome stuff.

Permalink - Tags: Development,Android,Google