Sunday, 27 October 2013

In android, a single screen is called an Activity. An activity (single screen) has other user interface elements such as Button, TextView, EditText etc. 

The official guide on Activities will explain you more details about activities in android.

In this android tutorial, you will learn how to start one activity from another.

Note that the xml files represent the activity in android

Code to start new activity:


startActivity(new Intent(MainActivity.this, Second.class));
1. Create XML

activity_main.xml  - This is the first screen (first activity)

File: res/layout/activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="Button" />

</RelativeLayout>
secondActivity.xml - This is the second screen

File: res/layout/secondactivity.xml

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

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="@string/secondActivity" />

</RelativeLayout>
strings.xml - this file already exists, you only have to update the contents of the file

File: res/values/strings.xml

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

    <string name="app_name">Android Activity</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>
    <string name="secondActivity">This is second Activity</string>

</resources>
2. Create the Java Code

MainActivity.java - 

package com.mavenscientists.androidactivity;

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

public class MainActivity extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  
  Button b = (Button) findViewById(R.id.button1);
  b.setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {
    startActivity(new Intent(MainActivity.this, Second.class));
   }
  });
  
 }

}
Second.java

package com.mavenscientists.androidactivity;

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

public class Second extends Activity
{
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.secondactivity);
 }

}
3. Add Activity to AndroidManifest.xml

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

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.mavenscientists.androidactivity.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="Second"/>
    </application>

</manifest>
Now, run the application and you will see the following output:


Upon clicking the button in the Main Activity, you will see a new activity as below. 



Developed using Android SDK API 18 | Eclipse - Kepler Release

References

1. Activities in Android

Get Ebooks delivered to your email id

Comments

Subscribe to our channel

Facebook

Powered by Blogger.

Home | Contact Us | DMCA | Terms of Service | Privacy | Advertise

Maven Scientists