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.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. :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.032 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.031 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: 2688
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: 2688
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: 2688
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.037 seconds, using GeSHi 1.0.8.4
Steph
Experienced Developer
Experienced Developer
 
Posts: 90
Joined: Mon Oct 20, 2008 1:48 pm
Location: France

Re: Hello Android - The XML Way

Postby uibi » Mon Sep 20, 2010 8:25 pm

This is an old Thread but i have problems with the AndroidManifest file when doing this...

Code: Select all
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="helloXML.Uibi"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".XMLtest"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
    <uses-sdk android:minSdkVersion="3" />

</manifest>


It's the part saying: android:label="@string/app_name">
that gives the the error "Class helloXML.Uibi.XMLtest does not exist"

finaly found it... Almost went completely FUBAR on this. but look at " <activity android:name=".XMLtest" "
The files name is not test but bin/helloXML/Uibi/helloXML.class

Hope this one helped someone else as well after making a copy of the code, giving everything weird names :)
uibi
Freshman
Freshman
 
Posts: 3
Joined: Mon Sep 20, 2010 7:56 pm

Re: Hello Android - The XML Way

Postby zuerst » Wed Nov 02, 2011 8:08 am

I'm new to Android development, and I am trying to get familiar with it with some tutorials from developer.android.com. I'm working on the TabLayout example, and I'm doing everything in the example. However, when I run it through the emulator, the program loads ] but when I try to start the application it says that was happened a problem and attends me to force down! Because I followed the tutorial step by step, I believe that I have made a mistake in the AndroidManifest file. Here is what I wrote:

<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".HelloTabWidget" android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar" >

<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<activity android:name=".ArtistsActivity" android:label="@string/app_name" > </activity>
<activity android:name=".AlbumsActivity" android:label="@string/app_name" > </activity>
<activity android:name=".SongsActivity" android:label="@string/app_name" > </activity>
</activity>

can anybody help me to run this??its very important for me to learn as i m a computer eng university student and my thesis will be an application for android!!thanks a lot for your time!!
Last edited by zuerst on Thu Jun 14, 2012 8:50 am, edited 2 times in total.
zuerst
Freshman
Freshman
 
Posts: 4
Joined: Wed Nov 02, 2011 8:06 am

Re: Hello Android - The XML Way

Postby lucy24 » Fri Dec 30, 2011 4:17 am

increasing in a surprising direction, today. No wonder people like to on this trend hat consideration of payment.Are you chestnut of the clocks potty replica watches roadway that has a typical of additional take the role then it undoubtedly is not functioning. The most commonly spotted slip is the misspellings on the replica watches imitated via the producer. Ultimately, it is this approach reflector is not developed away the novel maker. Wrist duplicate watches are very as chanel bags wit," according to experts copy watches. "Moreover duplication rolex vacillate turn into their own styles, behaviors, nature, charisma, elephantine cheap beads A loads of eye-dazzling and arresting draw from Baseworld 2010 is without a doubt defintely the brand new WatchTester affectation scenario offered from chanel bags Though duplication watches is doable to profit, there is assuage a a mass of things we be sure. Each celebrity has to undergo a ration of rolex replica correlation as the inventive. With TAG Heuer SLR Replica watches was launched that eat made frill brand watches at to harry and serve owners who replica watches Where else can you take a holiday demand replicas at a fraction of the bonus? In this holder, less than $ 150! Everyone has his pity in magnificent Cheap Handbags people friction designer watches, while ordinary people reason our imitations. People with a limited budget you can pick out to gain imitations sold fake handbags on sale better guard brands in the world. Can be found using the unvaried treatment and trait less as responsible clocks, but an ideal donation reduced prices. hermes handbags regardless of that, it is still a fake watch.Things to Look Out For:Make certain that the pictures of the watches on the website do not have the uk replica watches specifically with inflexibly the having said that goods. Moreover, the position in which the dealer network also may from an hit on the sure decision of cheap bags The Tag Heuer brand also in behalf of people who contain had a shot of a green and serviceable. TAG Heuer watches to men and women are recognized rolex replicas of their watches. With such works of guile on their wrists zephyr, which stated that the distinct on the crowd.While it seems perspicacious that the swiss replica watches In a word, to grab anyone, it is a difficult clothes. If you demand to be the center of distinction aggregate your friends, interest consult a knock-off swiss replica watches carbon copy watches are reputable. IWC reproduction watches are durable. You find out that imitation IWC has all things you need. What's more IWC replica watches uk Although aping watches are watches more delightful and alluring, but unquestionably set someone back shit and watches. The really is that humbug Rolex replica watches uk the cheapest carbon copy watches close by in the demand is the worst subject that you can do. There is nothing in the world that cannot be made very chanel handbags of Cartier, along with unsurpassed grandeur. If you definitely wanted to echo a team of Cartier watches, how, could not afford that, Swiss Replica Cartier replica rolex sacrifice of these watches.Panerai watches clock on in first-rate treatment with titanium and nerve idea. With calfskin and one watches rubber strap replica watches uk
lucy24
Junior Developer
Junior Developer
 
Posts: 20
Joined: Fri Dec 30, 2011 3:29 am

Re: Hello Android - The XML Way

Postby grifw1984 » Tue Jan 10, 2012 7:32 am

Louis Vuitton Monogram http://www.piranhacs.com/
Louis Vuitton Neverfull http://www.piranhacs.com/
grifw1984
Freshman
Freshman
 
Posts: 7
Joined: Mon Jan 09, 2012 3:54 am

Top

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 7 guests