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

, , , ,

  1. #1 by Doctor Who on April 11, 2012 - 8:26 am

    I was looking at some of your content on this internet site and I think this internet site is very informative ! Keep putting up.

  2. #2 by Christmas Nikon Camera Deals 2012 on November 29, 2012 - 7:29 pm

    My spouse and I absolutely love your blog and find almost all of your posts to be just what Im looking for. can you offer guest writers to write content for you personally I wouldnt mind publishing a post or elaborating on many of the subjects you write related to here. Again, awesome website!

  1. A Simple ListView in Android « Android Desk

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: