I'll focus more on using XML layout.
Tips: Good practice is to Run the program by choosing
run as as shown in the figure beside, click on the dropdown and select the *.java file to run. It is to avoid you run the *xml file, because it is messy sometime if you don't know how to handle it. When we saw something like main.out.xml , you are able to delete this file, but you can't run your project anymore. A error message saying "Your project contains error(s), please fix them before running your application". But obviously no error was found by user. What you can do is to "clean" it. In Eclipse, go to Project > Clean.. . Then you are now able to run your application again.
Lets start some revisions
I'll only cover on exercise 1 and 2
i. Create New Project named HelloAndroid
Application name : HelloAndroid
Package name : example.helloworld
1. Edit main.xml with TextView
2. Edit strings.xml resources by adding strings
_____________________________________________________________
A little recap for creating new project and Android Virtual Device (AVD). Create Project
- File> New> Android Project or Alt+Shift+N > Android Project
- Fill in Project Name, choose platform, fill in Apps Name, Package Name, Create Activity tag and Min SDK Version (details description may check on the previous post - New Project & Hello World)
- Window > Android SDK and AVD Manager
- Go to Virtual Devices (left panel) , click new button
- Fill in Name, select target (if no target available, please refers to the post Installation for android development tools (ADT) and eclipse)
- Click Create AVD button
1. Edit main.xml with TextView
As we learned on previous tutorial, here are some details explanation to get you understand the concept.
First we copy back the code to the res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/textview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="@string/hello" />
What we can see is that on the last line android:text= ="@string/hello" is referring to the tag with string / <string name="hello">, so hello is the name for the tag.
2. Edit Strings.xml resources by adding strings
We do the same, copy the code first.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">O My God,Android, this is the strings.xml!
<string name="app_name">Hello,Android, the string is inside res/values>
</resources>
<resources>
<string name="hello">O My God,Android, this is the strings.xml!
<string name="app_name">Hello,Android, the string is inside res/values>
</resources>
So as we can see, inside the resources node, the tag string is named="hello" is fed with a sentence.
This string will be called by main.xml. So once you run the apps, the setContentView(R.layout.main) in HelloAndroid.java will display the sentence.
Virtually it should be something like below:
In the next session, we will have more fun playing with font style by using some simple html tag like <u>, <b>, <i>, <a href> and change the color of font and background.
No comments:
Post a Comment