Pages - Menu

Saturday 22 September 2012

MULTIPLE ACTIVITY


Now we are going to discuss about how to handle multiple activities in an android app. I explain it with an example.Open the eclipse IDE and start a new project with name MultipleActivityApp.
Create the layout for second Activity:
Expand the layout folder in res folder and right click the layout folder select other option and select the new Android XML file




Name the file as second_layout.(It is necessory to name all the XML files in small letters) and click finish. Edit the second_layout.xml as shown bellow.


<?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="match_parent"
        android:layout_height="wrap_content"
        android:text="HELLO FROM SECOND ACTIVITY" />
</LinearLayout>


Create the Activity:
Expand the project folder and expand the folder with name src. Right click the package (here it is com.codeglympse) select new class( if the class option is not found then select the other option and select the class)


Name  the class as SecondActivity and click finish.


Now make the SecondActivity.java file extends the Activity class and link the layout and activity class of the second activity using the statement setContentView(R.layout.second_layout). Here the second_layout is the name of the XML file which we have already created. SecondActivity.java file is shown below


package com.codeglympse;
import android.app.Activity;
import android.os.Bundle;
public class SecondActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
       // TODO Auto-generated method stub
       super.onCreate(savedInstanceState);
setContentView(R.layout.second_layout);
} 
}

Add a button with id 'bn' and text 'NEXT ACTIVITY' in the main.xml as shown bellow.
<?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="HELLO FROM MAIN ACTIVITY" />

    <Button
        android:id="@+id/bn"
        android:layout_width="148dp"
        android:layout_height="wrap_content"
        android:text="NEXT ACTIVITY" />
</LinearLayout>

Now go to the MultipleActivity.java file and create an intent for the new activity in the onClickListener of the button.
                  Intent in = new Intent(getApplicationContext(),SecondActivity.class);
                         startActivity(in);
    MultipleActivity.java is shown bellow

package com.codeglympse;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MultipleActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    Button bn = (Button) findViewById(R.id.bn);
   
    bn.setOnClickListener(new OnClickListener() {
             
              @Override
              public void onClick(View arg0) {
                     // TODO Auto-generated method stub
                     Intent in = new Intent(getApplicationContext(),SecondActivity.class);
                     startActivity(in);
                                  }
              });
        }
        }

Now open the AndroidManifest.xml and specify the Second Activity name there.

            <activity
            android:label="@string/app_name"
            android:name=".SecondActivity" >
            </activity>
The AndroidManifest.xml file is shown below.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.codeglympse"
    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:label="@string/app_name"
            android:name=".MultipleActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
         <activity
            android:label="@string/app_name"
            android:name=".SecondActivity" >
            </activity>
    </application>
</manifest>


Run the application and you got the following output.





























No comments:

Post a Comment