Hello Android - The XML Way

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

Hello Android - The XML Way

Postby plusminus » Fri Nov 16, 2007 3:17 pm

Hello Android - The XML Way

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.

:idea: Read before: Hello Android - Your first Application

Difficulty: 1 of 5 :D
:idea: See also: (detailed)

What it will look like:
Image


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.

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <TextView xmlns:android="http://schemas.android.com/apk/res/android"
  3.  android:layout_width="fill_parent"
  4.  android:layout_height="fill_parent"
  5.  android:text="Hello Android - by: anddev.org"
  6. />
Parsed in 0.002 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. :wink:

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:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package org.anddev.android.Hello_Android;
  2.  
  3. import android.app.Activity;
  4. import android.os.Bundle;
  5. import android.widget.TextView;
  6.  
  7. public class Hello_Android extends Activity {
  8.     /** Called when the activity is first created. */
  9.     @Override
  10.     public void onCreate(Bundle icicle) {
  11.         super.onCreate(icicle);
  12.         // We want to view some very simple text, so we need a TextView
  13.         TextView tv = new TextView(this);
  14.         // Put some text to the newly created TextVIew
  15.         tv.setText("Hello Android - by: anddev.org n" +
  16.                         "This is soooo simple =D ");
  17.         // Tell our App to display the textView
  18.         this.setContentView(tv);
  19.     }
  20. }
Parsed in 0.053 seconds, using GeSHi 1.0.8.4

After:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package org.anddev.android.Hello_Android;
  2.  
  3. import android.app.Activity;
  4. import android.os.Bundle;
  5.  
  6. public class Hello_Android extends Activity {
  7.     /** Called when the activity is first created. */
  8.     @Override
  9.     public void onCreate(Bundle icicle) {
  10.         super.onCreate(icicle);
  11.         // Make Android use the main.xml-layout-file.  
  12.         this.setContentView(R.layout.main);
  13.     }
  14. }
Parsed in 0.051 seconds, using GeSHi 1.0.8.4


Now run your app the same way as before. You'll see no changes :)

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2696
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Top

R.java

Postby longshot » Sun Dec 02, 2007 7:09 pm

I'm on the demo/notepad1 (Eclipse adt_0.3.1) and can run, edit, debug the project just fine, but I can't get the R.java to update when adding or editing files in the project's res/ directory. Is there a path setting somewhere that I should check?

[UPDATE] Sorry, I saw another post advising to delete the R.java and that did the trick for me. It was automatically recreated, and then further edits to xml files would automatically update the R.java file.
longshot
Freshman
Freshman
 
Posts: 3
Joined: Sun Dec 02, 2007 6:56 pm

Postby longshot » Thu Dec 06, 2007 11:02 am

I started having trouble again with the R.java updates. It was working fine for a handful of edits, then suddenly it stopped doing the auto updates.

This time, I tried moving the R.java file, restarting eclipse, and making edits and adding new xml files to the resource directory to try to trigger it to update or recreate the file, but no deal.

Does anyone know if there is some kind of project setting or path that tells the android plugin which directory in your project it needs to monitor for resource changes?
longshot
Freshman
Freshman
 
Posts: 3
Joined: Sun Dec 02, 2007 6:56 pm

Postby plusminus » Thu Dec 06, 2007 1:51 pm

Hello longshot,

the aapt.exe is responsable to generate the R.java (Official description of aapt.exe).

These things come to my mind.
  1. Check for process: aapt.exe and kill it, should restart immediately or at least on system restart.
  2. System Restart :roll:
  3. Handish use of aapt.exe (check possibilities in command-line: "aapt.exe -help")

When I had trouble with the aapt.exe (that crashed from time to time, having typed sth wrong in an xml-file) I finally "solved" it with a system-restart :?

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2696
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby Nitinkcv » Fri Dec 07, 2007 6:39 am

Hi,

Another way which i found useful was to delete the R.java manually.. it gives a warning prompt but we need to click yes. Then make some changes to the layout xml files. This automatically generated the R.java file which in my case had all the updated changes.

Hope this helps :lol:

Thanks,
Nitin
Nitinkcv
Developer
Developer
 
Posts: 29
Joined: Thu Nov 29, 2007 1:02 pm

Postby longshot » Fri Dec 07, 2007 8:37 am

plusminus wrote:the aapt.exe is responsable to generate the R.java (Official description of aapt.exe).

These things come to my mind.
  1. Check for process: aapt.exe and kill it, should restart immediately or at least on system restart.
  2. System Restart :roll:
  3. Handish use of aapt.exe (check possibilities in command-line: "aapt.exe -help")

When I had trouble with the aapt.exe (that crashed from time to time, having typed sth wrong in an xml-file) I finally "solved" it with a system-restart :?


Thanks for the reply. I tried rebooting, but after restarting eclipse, I don't see the aapt.exe in Windows Task Manager. Running it from the command line, I can see there are a lot of options, but no explanation for how it's configured to be run from Eclipse. If you have Windows, can you check if aapt.exe is always running in your task manager while Eclipse is up? I do have two adb.exe processes (which are also from the tools directory) so it's partially working. But it's weird that it seemed to work one minute and then die forever the next.
longshot
Freshman
Freshman
 
Posts: 3
Joined: Sun Dec 02, 2007 6:56 pm

Top

Postby plusminus » Fri Dec 07, 2007 9:12 am

Hello longshot,

I'm not sure if it is always running, but when it crashed with me, I had some of these processes hanging around in the taskmanager. Probably it just gets started when Eclipse detected a change to on of the res-files...

I'll have a closer look, when I'm back home.

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2696
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby ronenfe » Tue Sep 09, 2008 7:11 pm

in my computer i don't see it running at allbut still i got the r.java updated. it gets updated only when you save your xml file.
anyway after some trials and errors in my computer , because i had the same problem, i found that if you have a resource file with errors in it it prevents r.java from being updated, so you first need to prevent any errors.
ronenfe
Freshman
Freshman
 
Posts: 4
Joined: Tue Sep 09, 2008 5:12 pm

Postby haden » Thu Sep 11, 2008 12:16 pm

Thanks.. a really nice tutorial.. simple.. god foor a noob like me to start with..
haden
Experienced Developer
Experienced Developer
 
Posts: 74
Joined: Thu Sep 11, 2008 11:51 am

Postby Steph » Sun Nov 15, 2009 5:52 pm

Hi.
I try to load an xml file in data/data/myapp/file But i have a problem. I have a null null exception. That's really difficult to resolve. And that's the end of my app. So, i really need to solve that point.

Please, if you have some idea, help me.

Thanks in advance.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. try
  3.  
  4.         {
  5.  
  6.                 //load xml file
  7.  
  8.                 SAXParserFactory spf = SAXParserFactory.newInstance();
  9.  
  10.                 SAXParser sp = spf.newSAXParser();
  11.  
  12.                 XMLReader xr = sp.getXMLReader();
  13.  
  14.        
  15.  
  16.                 MyLocalHandler myHandler = new MyLocalHandler();
  17.  
  18.                 xr.setContentHandler(myHandler);
  19.  
  20.  
  21.  
  22.                 FileInputStream fin= this.openFileInput("seriesname.xml");
  23.  
  24.                
  25.  
  26.                 xr.parse(new InputSource("file://"+FILE_NAMES_PATH));
  27.  
  28.                 xr.parse(new InputSource(fin));
  29.  
  30.                 //parsed data to the user
  31.  
  32.                 if(myHandler.getParsedData()!=null)
  33.  
  34.                         _seriesName=myHandler.getParsedData();
  35.  
  36.         }
  37.  
  38.         catch (Exception e)
  39.  
  40.         {
  41.  
  42.                 Log.e("US Finder", "Load XmlFile "+e.getMessage()+" "+e.getCause());
  43.  
  44.         }
  45.  
  46.  
Parsed in 0.059 seconds, using GeSHi 1.0.8.4
Steph
Experienced Developer
Experienced Developer
 
Posts: 58
Joined: Mon Oct 20, 2008 1:48 pm
Location: France

Top

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 3 guests