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:)

Image

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
  1.  
  2. <?xml version="1.0" encoding="utf-8"?>
  3.  
  4. <LinearLayout
  5.  
  6. xmlns:android="http://schemas.android.com/apk/res/android"
  7.  
  8. android:layout_width="fill_parent"
  9.  
  10. android:layout_height="wrap_content"
  11.  
  12. android:orientation="horizontal">
  13.  
  14.  
  15.  
  16. <TextView android:id="@+id/alarmtext"
  17.  
  18. android:textSize="16px"
  19.  
  20. android:layout_width="280px"
  21.  
  22. android:layout_height="wrap_content"/>
  23.  
  24.  
  25.  
  26. <aexp.customview.AlarmingView
  27.  
  28. android:id="@+id/alarming"
  29.  
  30. android:layout_width="wrap_content"
  31.  
  32. android:layout_height="wrap_content"/>
  33.  
  34. </LinearLayout>
  35.  
  36.  
Parsed in 0.002 seconds, using GeSHi 1.0.8.4


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.
paller
Developer
Developer
 
Posts: 29
Joined: Mon Dec 31, 2007 2:33 am
Location: London

Top

Return to Advanced Tutorials

Who is online

Users browsing this forum: No registered users and 3 guests