Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Getting Down and Dirty with Google's Android : Page 2

The best way to understand a new SDK is to work with it. With that in mind, we'll show you how to do some basic location based services and simple UI layout using Google's newest upstart mobile offering.


advertisement
Activities
Every mobile environment has its foundation class. J2ME applications have midlets, BREW has applets, and Android uses activities. The activity provides you with basic access to the phone's OS and events. It will contain your base constructor, key handler, suspend/resume functionality, and other low-level hand-set access. Your app, in essence, will be an extension of the Activity class. Here's how our sample application's Activity looked before we cluttered it up with functionality:

public class LocateMe extends Activity { public void onCreate(Bundle params) { super.onCreate(params); setContentView(R.layout.main); } public boolean onKeyDown(int keyCode, KeyEvent event) { return true; } }

The onCreate function will be called when your application is started for the first time. The bundle object contains basic information including any launch parameters or environment data needed on startup. Activities can be full screen or floating, they can be nested but each is a semi-autonomous unit. What, you might ask, is setContentView?

The "View"
The View class is Android's super-class for nearly all screen rendering; there are several varieties. Each view has a rectangle it's responsible for drawing on. That rectangle can be defined and extended in a myriad of ways. For the sake of expediency this article will only cover two major players: Custom Views and Android's XML content views. Since the basic project, upon creation, starts with a pre-defined "Hello World" XML view, that seems a natural place to start.



If you snoop around in a new Android project, at some point you'll find a file called main.xml. This file, through a simple XML schema, describes a screen layout to Android. Here's what our sample application's main.xml looks like:

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:text="Press the center key to locate yourself" /> </RelativeLayout>

While this seems relatively straightforward, let's go over it anyway. This particular file defines a relative layout, which means all the elements are described by their relation to one-another or to their parent. As with Views, there are several layout methods but we'll stick to this one for the sake of clarity.

The RealtiveLayout listed above contains one text element which is set to fill its parent (your LocateMe activity.) The LocateMe activity is full screen by default, so the text element will inherit that property and the text will appear in the top left corner of the screen. Additionally, a reference number must be set for the XML file so that Android can find it from your source code. By default, these reference numbers are stored in R.java and will look something like this:

public final class R { public static final class layout { public static final int main=0x7f030001; } }

Views may be nested so, unlike in J2ME, it's possible to mix custom views with the Widgets released by the Android team. In J2ME, developers were forced to choose between a GameCanvas and J2ME's application canvas. This meant that if you wanted a custom look, you'd have to roll all your own widgets on top of the GameCanvas. Not so with Android; view types can be mixed and matched in code with ease. Android also comes with a widget library (built on top of the View class) for scrollbars, text-entry, progress-bars, and many more. These standard widgets can be overloaded and customized at the whim of your UI team. Now that you've a few building blocks in place, it's time to get into the sample application.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap