Custom views

Tutorials with advanced 'difficulty' and more Lines of Code.

Custom views

Postby paller » Mon Apr 28, 2008 10:07 pm

Click here for the cross-linked version of the entry.

Now that I entertained myself (and hopefully the readers of the blog as well) with custom adapters and animations, I set out to an even more adventurous exercise. In Android, entirely new view classes could be created and used in conjunction with XML layout files, similarly to built-in views. Views have their own lifecycle. Intererested readers should consult the documentation of android.view.View class. Very shortly: views may handle the event when their inflation is finished, when they are being measured by the layout manager, when they are being laid out, when their size change, when they gain or lose focus, when they are attached to and detached from a window and when the window they are attached to changes visibility. This is pretty complex so I chose an easier way to introduce custom views by extending a built-in view.

The scenario which absolutely requires custom views is the following: we have a list of alarm events and some of them requires the user's acknowledgement. There is a flashing exclamation mark beside such alarms. Like this (except that the exclamation mark is flashing:)


You can download the example program from here.

We will support the list with a custom adapter, as we have seen previously. A single row for this adapter looks like this (located under res/layout/alarm_row.xml, XML mangling because of blog engine limitation).

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  2. <?xml version="1.0" encoding="utf-8"?>
  4. <LinearLayout
  6. xmlns:android=""
  8. android:layout_width="fill_parent"
  10. android:layout_height="wrap_content"
  12. android:orientation="horizontal">
  16. <TextView android:id="@+id/alarmtext"
  18. android:textSize="16px"
  20. android:layout_width="280px"
  22. android:layout_height="wrap_content"/>
  26. <aexp.customview.AlarmingView
  28. android:id="@+id/alarming"
  30. android:layout_width="wrap_content"
  32. android:layout_height="wrap_content"/>
  34. </LinearLayout>
Parsed in 0.002 seconds, using GeSHi

TextView is our old friend but the second view in the row is our own. If you check the aexp.customview.AlarmingView class, you will see that it is a descendant of ImageView. We still have to define three constructors (one for the creation from code, 2 others for inflating from XML) but the rest of the view lifecycle handling is inherited from ImageView. The flashing effect is achieved by periodically updating the drawable resource displayed by the underlying ImageView.

There is one point worth highlighting. There is a single private class handling the periodic flashing (coded as singleton for all AlarmingView instances to save resources and achieve synchronous flashing) but that class does not use the java.lang.Thread mechanism. If you code it that way (I did first :-( ), the application is shut down with an error message that views are not thread-safe and can be manipulated only from the thread that created them. Hence, we create the private class instance when the first AnimatedView is created and therefore the Handler instance in the private class will also be created in the context of the thread that creates the AnimatedView in the first place. Then we request this Handler instance to update the state of our views periodically.
Posts: 29
Joined: Mon Dec 31, 2007 2:33 am
Location: London


Return to Advanced Tutorials

Who is online

Users browsing this forum: No registered users and 1 guest