So, first off, a bit of an introduction. This is the first part of (hopefully) many tutorials related to the android platform. In this tutorial, we're only going to cover the very basics: how to design your application, and how to utilise the XML layouts to design your user interface.
Since this series of tutorials is more directed at the code, I'm not going to cover setup & installation. Instead, here's a link to get yourself set up with the SDK and an emulator.
Personally, I've never really been a huge fan of Java. And when I first tried Android, I wasn't too keen. But it's come a long way since it was first released. Once you get into the android SDK, it begins to get really interesting. Plus, the idea of earning a little bit of cash on the side for designing a little app for your phone has quite a nice appeal.
The market
The market is awesome. It's extremely easy to release and update your applications, and I'll be writing a tutorial all about publishing your apps, how to sign them, how to create a jar keystore, all the fun stuff.
Get started already!!1!one!
Okay. Enough intro, let's get to some development!
...sorry. No programming yet. We're going to be reading the project template, and I'll be explaining it line for line. It's best to take this stuff slow at first, because it's so different to a lot of "regular" projects. But at least there's code!
Create a project, you can call it anything, but I'm going to be following this structure along the series of tutorials:

layout.xml
The layout.xml is... sorta important. It's a great way to design your user interfaces, but it's not always necessary: you can create your interfaces through pure Java, which is personally my preferred method... but it's still good to know how to use the xml layouts. They help to keep the code for your activity more organised. (Coming from a C++/wxWidgets background, I'm in the habit of creating my programs through pure code, you may prefer the XML process)
Right. Enough rambling. I've analysed the ./res/layout/main.xml, and broken it up, line by line.
yep: we just use standard XML to declare the layout:
<?xml version="1.0" encoding="utf-8"?>
We use a LinearLayout to define our layout as being, well... linear:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
Next up, we have some properties:
android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"
Note the three properties:
android:orientation -- which direction the layout is filled. Contrary to what you may think, this isn't based on the angle of the phone. Using "vertical" simply means "add widget number 2 below widget number 1, add widget number 3 below widget number 2, ... add widget number n below widget number n-1".
android:layout_width -- the width of the object. We're going to use "fill_parent" to fill the width of the screen
android:layout_height -- the height of the object. We're going to use "fill_parent" again, to fill the height of the screen.
Lastly, we're going to close the LinearLayout item. Note we want more items inside the LinearLayout, so we're just going to use > instead of />
>
Next, we're going to define a TextView. The TextView is like a Label in .NET, or a wxStaticText in wxWidgets. It basically puts a static piece of text on the screen.
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" />
You'll notice it's pretty similar to how we defined the LinearLayout. Differences:
android:layout_height="wrap_content" -- the TextView will be resized according to the text it displays. The longer the string, the taller the View.
android:text="@string/hello" -- the string to display. @string refers to the ./res/values/strings.xml file. (More on this later)
/> -- this time, we're not putting any other items inside it, so we can close it off XML stylee. We could just as easily use
<TextView ... ></TextView>but it seems a little pointless here.
Since that's all we want in the LinearLayout, we're going to close our layout off:
</LinearLayout>
strings.xml
This one's much simpler. Put simply, this file contains strings which are built in to the application at compile time. Note the hello string, which we referenced in our TextView in ./res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello World, dic_tut1!</string> <string name="app_name">dic_tut1</string> </resources>
The Java!
Lastly, we're going to put our layout together in the code. I've commented this bit up, so it's easy for you to understand.
package dreamincode.tutorials.part_one;
import android.app.Activity; // Activity: the "main" part of the application
import android.os.Bundle; // Bundle: stores a "saved state" for your app to remember how it was when moved to the background
public class dic_tut1 extends Activity { // inheriting the Activity class
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) { // consider this the "entry point" (main) of your application
super.onCreate(savedInstanceState); // call the super to restore the state (does the work for you)
setContentView(R.layout.main); // set the main content view to ./res/layout/main.xml
}
}
That's all for now!
There's obviously more to it than this, things such as the AndroidManifest.xml file, but I'm not going to go into that now. We'll dive into that as and when we need it, it's really rather trivial and honestly nothing to worry about.
** EDIT **
I'll also attach the projects to each tutorial, so you can play with it yourself.
dic_tut1.zip (24.25K)
Number of downloads: 1415
This post has been edited by gabehabe: 06 October 2009 - 10:01 AM








MultiQuote








|