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.
Read before: Hello Android - Your first Application
Difficulty: 1 of 5
See also: (detailed)
What it will look like:
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.
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.
Now run your app the same way as before. You'll see no changes