Posts Tagged Activity in android

Activity in android

An Activity is the essential building block of an android application which provides UI. An android application usually consists of more than one activity. Each activity is stored in the back stack (which is a stack) in the order in which each of them are opened and so the first activity let’s say the main activity goes to the bottom of the stack and the next activity which is opened from the first one goes on top of stack and that gets the user focus. The stack pops the activities in the LIFO manner and as we press the back button on the device, the current activity which is on top of stack pops out and the previous activity comes in front which is at the stack top then and so on.

Also, don’t forget to declare the activity in the Manifest file so as to make it accessible by the system.

Let’s now create an activity. For that you need to extend a class named Activity. Here is the code for a simple activity which displays the text ‘Android Desk welcomes you’ using a TextView widget. Just have a look.

package com.deepthi.myactivity;
import android.app.Activity;
import android.os.Bundle;
 public class MyActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

The setContentView() method is called to define which layout our activity uses as a UI. The layout resource is passed in this method. Here main.xml is the layout for the UI.

The xml code for main.xml is shown below:

<?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="wrap_content"
        android:layout_height="wrap_content"
        android:text="Android Desk welcomes you"
        android:textSize="25dip"
        android:typeface="monospace"
        android:textStyle="bold"
        android:textColor="#F0F"/>
 </LinearLayout>

The Output looks like this:

So, now, you might have got an idea about activity in android.

Advertisements

, , , ,

3 Comments