Saturday, April 28, 2012

Android Multi-Activities One

Start a new Android project. For this demonstration we will use simple activities. The first one just takes a name and an email address in EditText boxes and a button. The second one for now will just have a ViewText that says "Thank you."

Here is the XML for first Activity


Main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=
     "http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <EditText
        android:id="@+id/editTextName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textPersonName" 
        android:hint="Last Name, First Name">

        <requestFocus />

    </EditText>

    <EditText
        android:id="@+id/editTextEmail"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textEmailAddress" 
        android:hint="Email"/>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/buttonText" />

</LinearLayout>

here is the code for the strings resource

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="hello">Entry Form</string>
    <string name="app_name">MultiActivityExample</string>
    <string name="buttonText">Confirm</string>
    <string name="thankyou">Thank You</string>

</resources>

Here is the code for the main activity MultiActivityExampleActivity.java

package com.spconger.multiactivityexample;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;


public class MultiActivityExampleActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        
    }
}

Here is the first activity running

Now that we have the first activity set up, we will add another. Here is the XML for the second activity.

Confirm.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/thankyou"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium" />

</LinearLayout>

Now we will add a new class. It should have a superclass of android.app.Activity and it should inherit abstract methods

here is the code for the new Activity


package com.spconger.multiactivityexample;

import android.app.Activity;
import android.os.Bundle;

public class Confirmation extends Activity {
 
 @Override
 public void onCreate(Bundle savedInstanceState){
  super.onCreate(savedInstanceState);
  setContentView(R.layout.confirm);

}


Now we have to modify the Manifest file. Modify the xml so it looks like this:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.spconger.multiactivityexample"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".MultiActivityExampleActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <!-- add this for new activity -->
        <activity android:name="Confirmation"></activity>
    </application>

</manifest>

Now when you press the button you should get the second activity


No comments:

Post a Comment