1 page.title=UI Framework Changes in Android 1.6 2 @jd:body 3 4 <p>Android 1.6 introduces numerous enhancements and bug fixes in the UI 5 framework. This article highlights two improvements in particular: more flexible 6 and robust RelativeLayout and easier click listeners. </p> 7 8 <h3>More flexible, more robust RelativeLayout</h3> 9 10 <p>RelativeLayout is the most versatile layout offered by the Android UI toolkit 11 and can be successfully used to reduce the number of views created by your 12 applications. This layout used to suffer from various bugs and limitations, 13 sometimes making it difficult to use without having some knowledge of its 14 implementation. To make your life easier, Android 1.6 comes with a revamped 15 RelativeLayout. </p> 16 17 <p>This new implementation not only fixes all known bugs in 18 RelativeLayout but also addresses its major limitation: the 19 fact that views had to be declared in a particular order. Consider the following 20 XML layout:</p> 21 22 <pre><?xml version="1.0" encoding="utf-8"?> 23 24 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 25 android:layout_width="fill_parent" 26 android:layout_height="64dip" 27 android:padding="6dip"> 28 29 <TextView 30 android:id="@+id/band" 31 android:layout_width="fill_parent" 32 android:layout_height="26dip" 33 34 android:layout_below="@+id/track" 35 android:layout_alignLeft="@id/track" 36 android:layout_alignParentBottom="true" 37 38 android:gravity="top" 39 android:text="The Airborne Toxic Event" /> 40 41 <TextView 42 android:id="@id/track" 43 android:layout_marginLeft="6dip" 44 android:layout_width="fill_parent" 45 android:layout_height="26dip" 46 47 android:layout_toRightOf="@+id/artwork" 48 49 android:textAppearance="?android:attr/textAppearanceMedium" 50 android:gravity="bottom" 51 android:text="Sometime Around Midnight" /> 52 53 <ImageView 54 android:id="@id/artwork" 55 android:layout_width="56dip" 56 android:layout_height="56dip" 57 android:layout_gravity="center_vertical" 58 59 android:src="@drawable/artwork" /> 60 61 </RelativeLayout></pre> 62 63 <p>This code builds a very simple layoutan image on the left with two lines of 64 text stacked vertically. This XML layout is perfectly fine and contains no 65 errors. Unfortunately, Android 1.5's RelativeLayout is incapable of rendering it 66 correctly, as shown in the screenshot below.</p> 67 68 <img src="images/ui-1.6_002.png" style="width: 320px; height: 480px;"> 69 70 <p>The problem is that this layout uses forward references. For instance, the 71 "band" TextView is positioned below the "track" TextView but "track" is declared 72 after "band" and, in Android 1.5, RelativeLayout does not know how to handle 73 this case. Now look at the exact same layout running on Android 1.6:</p> 74 75 <img src="images/ui-1.6.png" style="width: 320px; height: 480px;"> 76 77 <p>As you can see Android 1.6 is now better able to handle forward reference. 78 The result on screen is exactly what you would expect when writing the 79 layout.</p> 80 81 <h3>Easier click listeners</h3> 82 83 <p>Setting up a click listener on a button is very common task, but 84 it requires quite a bit of boilerplate code:</p> 85 86 <pre>findViewById(R.id.myButton).setOnClickListener(new View.OnClickListener() { 87 public void onClick(View v) { 88 // Do stuff 89 } 90 });</pre> 91 92 <p>One way to reduce the amount of boilerplate is to share a single click 93 listener between several buttons. While this technique reduces the 94 number of classes, it still requires a fair amount of code and it still 95 requires giving each button an id in your XML layout file:</p> 96 97 <pre>View.OnClickListener handler = View.OnClickListener() { 98 public void onClick(View v) { 99 switch (v.getId()) { 100 case R.id.myButton: // doStuff 101 break; 102 case R.id.myOtherButton: // doStuff 103 break; 104 } 105 } 106 } 107 108 findViewById(R.id.myButton).setOnClickListener(handler); 109 findViewById(R.id.myOtherButton).setOnClickListener(handler);</pre> 110 111 <p>With Android 1.6, none of this is necessary. All you have to do is 112 declare a public method in your Activity to handle the click 113 (the method <i>must</i> have one View argument):</p> 114 115 <pre>class MyActivity extends Activity { 116 public void myClickHandler(View target) { 117 // Do stuff 118 } 119 }</pre> 120 121 <p>And then reference this method from your XML layout:</p> 122 123 <pre><Button android:onClick="myClickHandler" /></pre> 124 125 <p>This new feature reduces both the amount of Java and XML you have to write, 126 leaving you more time to concentrate on your application.</p> 127 128 <p>The Android team is committed to helping you write applications in the 129 easiest and most efficient way possible. We hope you find these improvements 130 useful and we're excited to see your applications on Android Market.</p> 131