In Android, an Activity is a component with which users can interact to perform a task through a screen interface, such as taking a photo, displaying a list of application settings, sending an email etc. Each activity is given a window to draw its user interface. The window typically fills the entire screen, but may also be made smaller than the screen. It floats on top of the other windows.
In this tutorial, we shall learn how to interact with an activity, when a button is clicked, navigate from the current screen (i.e. current activity) to another screen (i.e. another activity).
1. XML Layouts
We shall create the following two XML layout files in “res/layout/” folder:1. res/layout/main.xml – Represents screen 1
2. res/layout/main2.xml – Represents screen 2
File : res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Screen 1
(main.xml)"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Click to see Screen
2" />
</LinearLayout>
File : res/layout/main2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Screen 2
(main2.xml)"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
2. Activities
We shall create two activity classes in the src folder:
1. AppActivity.java –> main.xml
2. AppActivity2.java –> main2.xml
To navigate from one screen to another, use the following code :
Intent intent = new Intent(context,
anotherActivity.class);
startActivity(intent);
File : AppActivity.java
package com.endeavour.android;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
public class AppActivity extends Activity {
Button button;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
addListenerOnButton();
}
public void addListenerOnButton() {
final Context context = this;
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(context, AppActivity2.class);
startActivity(intent);
}
});
}
}
Note: It is always a good idea to kill the current activity before moving on to the next so that the application doesn’t consume too much memory and possibly crash.
It can be done by adding one line to the code: finish();
File : AppActivity2.java
package com.endeavour.android;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
public class AppActivity2 extends Activity {
Button button;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
}
}
3. AndroidManifest.xml
We shall now declare the above two activity classes in AndroidManifest.xml.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.endeavour.android"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".AppActivity" >
<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=".App2Activity" >
</activity>
</application>
</manifest>
No comments:
Post a Comment