What is this: This tutorial shows how to create the simplest application possible at all, this time using an XML based Layout.
What you learn: Using XML for non-programmatic layouts.
Difficulty: 1 of 5

What it will look like:

Description:
Instead of coding your Android-UI (User Interface) you can describe it using XML-Files.
Google wrote:The general structure of an Android XML layout file is simple. It's a tree of tags, where each tag is the name of a View class. In this example, it's a very simple tree of one element, a TextView. You can use the name of any class that extends View as a tag name in your XML layouts, including custom View classes you define in your own code. This structure makes it very easy to quickly build up UIs, using a much simpler structure and syntax than you would in source code. This model is inspired by the web development model, where you can separate the presentation of your application (its UI) from the application logic used to fetch and fill in data.
When you are using Eclipse to create your Android-Apps, you get an main.xml-file created in your Project (under PROJECT_NAME/res/layout/)
So we are now going to alter the project you created within: Hello Android - Your first Application
Locate the main.xml and paste the following code.
Using xml Syntax Highlighting
- <?xml version="1.0" encoding="utf-8"?>
- <TextView xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:text="Hello Android - by: anddev.org"
- />
Parsed in 0.001 seconds, using GeSHi 1.0.8.4
xmlns:android="http://schemas.android.com/apk/res/android"
This is an XML namespace declaration that tells the Android tools that you are going to refer to common attributes defined in the Android namespace. The outermost tag in every Android layout file must have this attribute.
android:text="Hello Android - by: anddev.org"
This sets the text that the TextView should contain. In this example, it is just: "Hello Android - by: anddev.org"message.
Magically the /src/packag_names/R.java has changed.
Google wrote:A project's R.java file is an index into all the resources defined in the file. You use this class in your source code as a sort of short-hand way to refer to resources you've included in your project. This is particularly powerful with the code-completion features of IDEs like Eclipse because it lets you quickly and interactively locate the specific reference you're looking for.
The important thing to notice for now is the inner class named "layout", and its member field "main". The Eclipse plugin noticed that you added a new XML layout file and then regenerated this R.java file. As you add other resources to your projects you'll see R.java change to keep up.
The last thing to do now is, to simplify your Main-Application-Code.
Before:
Using java Syntax Highlighting
- package org.anddev.android.Hello_Android;
- import android.app.Activity;
- import android.os.Bundle;
- import android.widget.TextView;
- public class Hello_Android extends Activity {
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle icicle) {
- super.onCreate(icicle);
- // We want to view some very simple text, so we need a TextView
- TextView tv = new TextView(this);
- // Put some text to the newly created TextVIew
- tv.setText("Hello Android - by: anddev.org n" +
- "This is soooo simple =D ");
- // Tell our App to display the textView
- this.setContentView(tv);
- }
- }
Parsed in 0.032 seconds, using GeSHi 1.0.8.4
After:
Using java Syntax Highlighting
- package org.anddev.android.Hello_Android;
- import android.app.Activity;
- import android.os.Bundle;
- public class Hello_Android extends Activity {
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle icicle) {
- super.onCreate(icicle);
- // Make Android use the main.xml-layout-file.
- this.setContentView(R.layout.main);
- }
- }
Parsed in 0.032 seconds, using GeSHi 1.0.8.4
Now run your app the same way as before. You'll see no changes
Regards,
plusminus






