Editing the Project Files

Open main.xml in either an XML editor or (if you do not have a specific XML editor) Notepad. This will let you edit the file and remove the <TextView/> definition that is within it. Save main.xml as shown in the next illustration.

3 mairSml - Notepad I a II ID

File Edit Format View Help [!?-xml ver;ion="1. 0" encodi ng="utf-S"?>

-!L"i near Layout xml ns : andr oi d="http://Schemas. androi d. com/apk/res/androi d" andron d: ori entati on---'Verti ca"i" anciroi d: 1 ayouT_wi dth = :,f i 1 '¡„parent " androi d:1ayout_hei qht = "fi11_parent"

The result of saving main.xml is an empty shell. This gives you a platform on which to edit your <activity>.java file. The <activity>.java file is in a folder that is several directories deep, AndroidHelloWorld\src\android\programmers\guide.

To create your Hello World! application, add the following lines to create, set, and use a TextView:

/**Create TextView */ TextView HelloWorldTextView = new TextView(this);

/**Set text to Hello World */ HelloWorldTextView.setText("Hello World!");

/**Set ContentView to TextView */ setContentView(HelloWorldTextView); /**END */

Do not forget to add the TextView package to the beginning of the file:

import android.widget.TextView;

The finished HelloWorldCommandLine.java file should look like that in the following illustration.

J HelloWorldCbmmandLirie.java - Notepad □ II El

Pile Edit Format View Help package android, programmers. guide:

import android.app.Activity; import android.o s.Bund!e; import android.widget.TextView; |

public class Hel1owor1dcommandLine extends Activity-

/"■"- called when the activity is first created. ©ovarri de public void oncreate(Bundl e icicle}

super. oner eate(i ci cli setcontentvi ew(R. 1 ayuur, mai n^ /•"•Hello world JFD ■/■"•'BEGIN "/

/""create TextView / TextView Hel 1 owor 1 d'rextVi ew = new TextVi e\v(thi;

/""Set text to Hello World / He! 1 oworldTextViev. setTextf "hel lo world! ");

/""Set contentview to TextView / setcontentvi ew(Hel1oworldTextvi ew);

pSWMffl V"

Your project files should now be set. You can now compile your program and run it in the Android Emulator.

0 0

Post a comment