Manifest File
The manifest file glues everything together. It is this file that explains what the application
consists of, what all its main building blocks are, what permissions it requires,
and so on
consists of, what all its main building blocks are, what permissions it requires,
and so on
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="android.programing.tutorials" android:versionCode="1" android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".HelloWorld" 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="8" />
</manifest>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="android.programing.tutorials" android:versionCode="1" android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".HelloWorld" 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="8" />
</manifest>
Layout XML Code
The layout file specifies the layout of your screen. In this case, shown in Example,
we have only one screen, and it’s loaded by the HelloWorld.java code seen in Example
Example
res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="@string/hello" />
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="@string/hello" />
</LinearLayout>
This is another XML file that contains all the text that your application uses. For example,
the names of buttons, labels, default text, and similar types of strings go into
this file. This is the best practice for separating the concerns of various files, even if they
are XML files. In other words, layout XML is responsible for the layout of widgets, but
strings XML is responsible for their textual content (see Example).
Example res/values/strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, HelloWorld!</string>
<string name="app_name">Hello, World!!!</string>
</resources>
<resources>
<string name="hello">Hello World, HelloWorld!</string>
<string name="app_name">Hello, World!!!</string>
</resources>
The R File
The R file is the glue between the world of Java and the world of resources (see Example). It is an automatically generated file, and as such, you never modify it. It is
recreated every time you change anything in the res directory, for example, when you
add an image or XML file.
You don’t need to look at this file much. We will use the data in it quite a bit, but we’ll
use Eclipse to help us refer to values stored in this file.
Example R.java
/* AUTO-GENERATED FILE. DO NOT MODIFY.
*
* This class was automatically generated by the
* aapt tool from the resource data it found. It
* should not be modified by hand.
*/
package android.programing.tutorial;
public final class R {
public static final class attr {
}
public static final class drawable {
public static final int icon=0x7f020000;
}
public static final class layout {
public static final int main=0x7f030000;
}
public static final class string {
public static final int app_name=0x7f040001;
public static final int hello=0x7f040000;
}
}
Java Source Code
The Java code is what drives everything. This is the code that ultimately gets converted
to a Dalvik executable and runs your application (see Example ).
Example HelloWorld.java
package android.programing.tutorial;
import android.app.Activity;
import android.os.Bundle;
public class HelloWorld extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
import android.app.Activity;
import android.os.Bundle;
public class HelloWorld extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
}
}
No comments:
Post a Comment