Pr.Pg Next Pg

Setting Up An Activity tutorials

Activity

  • An Activity is an application component that provides a screen with which users can interact in order to do something, such as dial the phone, take a photo, send an email, or view a map.

  • Each activity is given a window in which to draw its user interface.

  • The window typically fills the screen, but may be smaller than the screen and float on top of other windows.

  • Each individual screen within an Android application is an activity, and users can perform actions by interacting with the visual components within an activity.

  • Think of it as an individual web page within a modern web application, where the user can perform certain functions without leaving the page (using JavaScript), but must proceed to a different page in order to access a new set of tasks/functionality.

 

Creating a java file for the activity

  • To create a new activity, we have to create a new class.

  • We create a new class by right clicking on the package name under the src folder and then New > Class.

  • Give the class a name and it a good practice to start the name with Uppercase letters.

  • In our example, we are going to create a class with the name Example.

  • We need to modify the class as shown below

 

 

  • We add extends Activity to the class declaration because it is an Activity.

  • To add the onCreate method, right click in between the braces of the class and then click on

  • Source > Override/Implement Methods

  • A list of methods that can be overriden or implemented from the parent Activity class are displayed.

  • Select onCreate and then click on OK.

  • The new method will be displayed as shown above.

  • Now to add the layout for this activity, we use the method setContentView().

  • We will now add the layout test which we created in the previous topic as a parameter to the method.

 

Example.java

 

package com.example.helloandroid;

 

import android.app.Activity;

import android.os.Bundle;

 

public class Example extends Activity {

 

@Override

protected void onCreate(Bundle savedInstanceState) {

// TODO Auto-generated method stub

super.onCreate(savedInstanceState);

setContentView(R.layout.test);

}

 

}

 

 

Adding Activity to the AndroidManifest.xml

  • To make sure that our android application recognizes our activity Example, we need to add it to the AndroidManifest.xml

  • In our example, we will add a button Change Activity to switch over to the activity Example.

  • The main.xml file looks like this

 

main.xml

 

<LinearLayout 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"

android:orientation="vertical">

 

<TextView

android:id="@+id/id_tv"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_gravity="center"

android:gravity="center"

android:textSize="40sp"

android:text="@string/hello" />

 

<Button

android:id="@+id/id_button1"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_gravity="center"

android:text="Click Me"

android:gravity="center"

/>

<Button

android:id="@+id/id_button2"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_gravity="center"

android:gravity="center"

android:text="Change Activity" />

 

</LinearLayout>

 

  • We have also changed the IDs of the Views so as to avoid the conflicts.

  • Now we have to modify the AndroidManifest.xml as shown in the image.

 

 

 

 

AndroidManifest.xml

 

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

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

package="com.example.helloandroid"

android:versionCode="1"

android:versionName="1.0">

 

<uses-sdk

android:minSdkVersion="11"

android:targetSdkVersion="17" />

 

<application

android:allowBackup="true"

android:icon="@drawable/ic_launcher"

android:label="@string/app_name"

android:theme="@style/AppTheme">

<activity

android:name="com.example.helloandroid.MainActivity"

android:label="@string/app_name"

android:configChanges="keyboardHidden|orientation|screenSize">

<intent-filter>

<action android:name="com.example.helloandroid.MAINACTIVITY" />

 

<category android:name="android.intent.category.DEFAULT" />

</intent-filter>

</activity>

<activity

android:name="com.example.helloandroid.Example"

android:label="@string/app_name">

<intent-filter>

<action android:name="com.example.helloandroid.EXAMPLE" />

 

<category android:name="android.intent.category.DEFAULT" />

</intent-filter>

</activity>

</applicaton>

</manifest>

 

 

Code Explanation

  • The < activity > tag defines an activity.

  • Since our application at the moment contains to activities, we have two < activity > tags.

  • The first < activity > is the one which was already defined before, the one which calls the MainActivity.java.

  • The second < activity > represents the Example activity.

  • Now we explain the various attributes of the < activity >.

 

android:name

This defines the name of class that implements the activity. The attribute value should be a full qualified class name.

 

android:label

A user-readable label for the activity. The label is displayed on-screen when the activity must be represented to the user. It's often displayed along with the activity icon.

If this attribute is not set, the label set for the application as a whole is used instead (see the<application> element's label attribute).

 

Now we explain the various attributes of the < intent-filter >

Specifies the types of intents that an activity, service, or broadcast receiver can respond to.

An intent filter declares the capabilities of its parent component what an activity or service can do and what types of broadcasts a receiver can handle.

It opens the component to receiving intents of the advertised type, while filtering out those that are not meaningful for the component.

 

Now we explain the various attributes of the < action >

This basically adds an action to the intent filter.

android:name

This defines the name of the action. This name is used to reference this action.

 

Now we explain the various attributes of the < category >.

This basically adds a category to the intent filter.

  • android:name

  • The name of the category. Standard categories are defined in the Intent class as CATEGORY_nameconstants. The name assigned here can be derived from those constants by prefixing "android.intent.category."

  • In this part, <category android:name="android.intent.category.LAUNCHER"> means that this will be the first activity which will be launched because of the LAUNCHER keyword. Application will find see the Launcher tag and then look for that activity name and then launch it.

  • Similarly <category android:name="android.intent.category.DEFUALT" means ghat this activity is a part of the android application but it will be launched only when it is called explicitly.

 

Pr.Pg border                                              Next Pg