Introduction to Android 2 application development
Written by Sing Li   
Wednesday, 25 August 2010
Article Index
Introduction to Android 2 application development
iProgrammer example
A practical Activity
Compatability
Ready to run
Using the Eclipse ADT

Banner

 

Listing 1 shows a very basic Activity - the IProgrammerActivity.

Listing 1.

package info.iProgrammer.android;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class IProgrammerActivity
extends Activity {
/** Called when the activity is
first created. */
@Override
public void onCreate(
Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button button =
(Button) findViewById(R.id.button);
final TextView textBox =
(TextView) findViewById(R.id.text);
button.setOnClickListener(
new View.OnClickListener() {
public void onClick(View v) {
textBox.setTextColor(Color.YELLOW);
textBox.setTextSize(80);
textBox.setText(getString(
R.string.thankyou));
}
});
}
}

In fact, you will become very familiar with Activities similar to the one shown in Listing 1.

It is Android's entry point into your application - whenever Android deems to be ready. It is your responsibility, in coding this Activity, to set up and tell the Android platform everything that your application will need - so that the one in control,  i.e. the Android Platform,  can call you back again or rather your app's custom workings when it needs them.

On an Android device, every application runs within its own restricted Java VM (an Android specific Dalvik VM) in a separate process.

Making Sense of Android Activities

Android learns about the initial Activity of your App through a configuration file that every Android application must have, named AndroidManifest.xml.

Listing 2 shows the AndroidManifest.xml with the XML lines that tell Android about our main hook IProgrammerActivity highlighted.

Listing 2

<?xml version="1.0" encoding="utf-8"?> 
<manifest
xmlns:android=
"http://schemas.android.com/
apk/res/android"
package="info.iProgrammer.android"
android:versionCode="1"
android:versionName="1.0">
<application
android:icon="@drawable/icon"
android:label="@string/app_name">
<activity
android:name=".IProgrammerActivity"
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="7" />
</manifest>

Accessing String Resources in Android Applications

The @string/app_name and @drawable/icon notations within AndroidManifest.xml refers to named resources specified in another XML file. The actual string value for @string/app_name resource is actually located in an XML resource file, res/values/string.xml, see Listing A.

Named resources enable you to write Java code and create configuration files without hard-coding of text strings and other resources - such as a graphical icon for the App. You just specify the string or resource you need via a name, and the platform or the compilation/assembly mechanism will fetch the actual string or resource for you.

By giving up direct control here, some magic can occur. For example, you ask for the string named @string/thankyou, and the platform can detect if your App is running on an English or a Chinese Smart Phone, and then fetch either the English "Thank you!" or the Chinese "谢谢!" string for you. All of a sudden, without recompiling the code, your application can work internationally! Relinquishing control does have its benefits.

Listing A

<?xml version="1.0" encoding="utf-8"?> 
<resources>
<string name="app_name">
i-Programmer
</string>
<string name="voteiprogrammer">
Vote i-Programmer.info
</string>
<string name="thankyou">
Thank you!
</string>
</resources>

Banner

<ASIN:0132121360>

<ASIN:1430224193>

<ASIN:1430226471>

<ASIN:0981678017>

<ASIN:0672330768>

<ASIN:1430232765>



Last Updated ( Sunday, 19 September 2010 )
 
 

   
RSS feed of all content
I Programmer - full contents
Copyright © 2014 i-programmer.info. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.