advertisement
Login | Register   
  Include Code  Search Tips
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Partners & Affiliates
advertisement
advertisement
advertisement
advertisement
Average Rating: 2.2/5 | Rate this item | 11 users have rated this item.
Getting Down and Dirty with Google's Android (cont'd)
Using Custom Views
In its most simplistic form, a View in Android must overload only one function onDraw(). Custom Views can be complicated 3D playback implementations or they can be ultra simplistic text rendering. Here's what the CreateView function listed above looks like:

public void CreateView()
{
customView = new CustomView(this);
}
advertisement
This function simply evokes the constructor for the CustomView object. The definition of which follows below:

public class CustomView extends View
{
LocateMe overlord;
public CustomView(LocateMe pCtx)
{
super(pCtx);
overlord = pCtx;
}
public void onDraw(Canvas cvs)
{
Paint p = new Paint();
String sLat = "Latitude: " + overlord.getLat();
String sLon = "Longitude: " + overlord.getLon();
cvs.drawText(sLat , 32, 32, p);
cvs.drawText(sLon, 32, 44, p);
}
}
This custom Android view renders the latitude and longitude test data to the screen in text. It requires a pointer to LocateMe, the Activity extender that is the heart of this application. Its two methods are the constructor and the onDraw methods. The constructor evokes the super-class constructor and stashes off the Activity pointer. onDraw will create a new Paint object (an object that encapsulates color, transparency, and other basic theme information) which will access the default color theme. From there, it's a matter of setting up the display strings, and drawing them to the screen using the Canvas pointer. For those of you who know something about J2ME's game canvas, this should look very familiar.

The Waiting Game
From purely a development standpoint, Android is a very powerful SDK. It combines XML layout schemes with custom view rendering and in house scrollbar, maps, and other in house widgets. All which can be overloaded and customized on the whim of a developer. Its documentation is rough; there are missing features and gaps in functionality (like SMS interop and a clear monetization plan), but overall Android looks very promising for what even Google acknowledges is a "First Look" SDK. Now we'll just have to see if anyone actually deploys a phone that uses it.

Previous Page: Android in Action  
Chris Haseman is an independent software engineer specializing in wireless development. He can be found riding his bike between coffee shops in San Francisco. He's the author and editor of the blog: 'Zen and the Art of Debugging.' In his spare time, he's a resident DJ at xtcradio.com and a martial arts instructor. Chris would like to thank to Meghan Kane for her copy edits and Ray Rischpater for a technical review.
Page 1: IntroductionPage 3: Android in Action
Page 2: ActivitiesPage 4: Using Custom Views
Please rate this item (5=best)
 1  2  3  4  5
advertisement