Friday, March 16, 2012

lifecycle events of Android Activity tutorial


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <LinearLayout
      android:orientation="horizontal"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content">
      <TextView
            android:id="@+id/uname"
            android:layout_width="80dp"
            android:layout_height="wrap_content"
            android:text="@string/Uname">
      </TextView>
      <EditText
            android:id="@+id/utextbox"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">
      </EditText>
    </LinearLayout>
    <LinearLayout
      android:orientation="horizontal"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content">
      <TextView
            android:id="@+id/pass"
            android:layout_width="80dp"
            android:layout_height="wrap_content"
            android:text="@string/Pass">
      </TextView>
      <EditText
            android:id="@+id/ptextbox"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:password="true">
      </EditText>
    </LinearLayout>
    <LinearLayout
      android:orientation="horizontal"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:gravity="center">
      <Button
            android:id="@+id/login"
            android:layout_width="70dp"
            android:layout_height="wrap_content"
            android:text="@string/Login">
      </Button>
      <Button
            android:id="@+id/clear"
            android:layout_width="70dp"
            android:layout_height="wrap_content"
            android:text="@string/Clear">
      </Button>
    </LinearLayout>
   
</LinearLayout>

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:id="@+id/mainLayout"
  android:gravity="center">
      <TextView
             android:id="@+id/welcomeNote"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Welcome !!"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:gravity="center">
</TextView>
      <Button
            android:id="@+id/setting"
            android:layout_width="90dp"
            android:layout_height="wrap_content"
            android:text="@string/setting"
            android:textSize="20dp"
            android:gravity="center">
</Button>
     
</LinearLayout>






<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:gravity="center">
    <TextView
      android:layout_width="wrap_content"
      android:text="@string/msg"
      android:layout_height="wrap_content"
      android:textAppearance="?android:attr/textAppearanceLarge"
      android:id="@+id/cs"
      android:textSize="30dp"
      android:gravity="center"
      android:textColor="@color/yellow">
    </TextView>    
</LinearLayout>



Extra Files :


strings.xml


<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World, Ex1Activity!</string>
    <string name="app_name">Ex1</string>
    <string name="Uname">User Name : </string>
    <string name="Pass">Password : </string>
    <string name="Login">Login</string>
    <string name="Clear">Clear</string>
    <string name="WelcomeNote">Welcome </string>
    <string name="setting">Setting</string>
    <string name="msg">Coming Soon...</string>
</resources>



color.xml 


<?xml version="1.0" encoding="UTF-8"?>
<resources>
        <color name="yellow">#FFFF00</color>      
</resources>


Ex1Activity.java

package com.exc.first;

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

public class Ex1Activity extends Activity {
    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
      //read component from xml file
        Button login = (Button)findViewById(R.id.login);
       
      /**
       * set OnClickListener on button
       * so that code which is in between { } is run on click on login button
       */
        login.setOnClickListener(new OnClickListener() {
                 
                  @Override
                  public void onClick(View v) {
                        // TODO Auto-generated method stub
                       
                        /**
                         * make Intent to call other activity
                         * here Ex1Activity.this to go second.class
                         * on click login button
                         */
                        Intent SecAct = new Intent(Ex1Activity.this,second.class);
                       
                        //start second activity
                        startActivity(SecAct);
                       
                  }
            });
       
       
    }
    @Override
      protected void onStart() {
            // TODO Auto-generated method stub
            super.onStart();
            /**
             * it show massage on LogCat when Activity starts
             */
            Log.i("Login Activity", "Start");
      }
     
      @Override
      protected void onPause() {
            // TODO Auto-generated method stub
            super.onPause();
            /**
             * It show massage on LogCat when Activity OnPause
             */
            Log.i("Login Activity", "Pause");
      }
      @Override
      protected void onRestart() {
            // TODO Auto-generated method stub
            super.onRestart();
            /**
             * it show massage on LogCat when Activity onRestart
             */
            Log.i("Login Activity", "Restart");
      }
      @Override
      protected void onResume() {
            // TODO Auto-generated method stub
            super.onResume();
            /**
             * it show massage on LogCat when Activity OnResume
             */
            Log.i("Login Activity", "Resume");
      }
     
      @Override
      protected void onStop() {
            // TODO Auto-generated method stub
            super.onStop();
            /**
             * it show massage on LogCat when Activity Stop
             */
            Log.i("Login Activity", "Stop");
      }
      @Override
      protected void onDestroy() {
            // TODO Auto-generated method stub
            super.onDestroy();
            /**
             * it show massage on LogCat when Activity Destroys
             */
            Log.i("Login Activity", "Destroy");
      }
}

second.java 




package com.exc.first;

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

public class second extends Activity{

      @Override
      protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.second);
           
            //read button from xml file
            Button setting =(Button)findViewById(R.id.setting);
            setting.setOnClickListener(new OnClickListener() {
                 
                  @Override
                  public void onClick(View v) {
                        // TODO Auto-generated method stub
                       
                        Intent goToSetting = new Intent(second.this,setting.class); //go to third Activity
                        startActivity(goToSetting);                    
                  }
            });
           
      }
      @Override
      protected void onStart() {
            // TODO Auto-generated method stub
            super.onStart();
            /**
             * it show massage on LogCat when Activity starts
             */
            Log.i("Second Activity", "Start");
      }
     
      @Override
      protected void onPause() {
            // TODO Auto-generated method stub
            super.onPause();
            /**
             * it show massage on LogCat when Activity on pause
             */
            Log.i("Second Activity", "Pause");
      }
      @Override
      protected void onRestart() {
            // TODO Auto-generated method stub
            super.onRestart();
            /**
             * it show massage on LogCat when Activity Restart
             */
            Log.i("Second Activity", "Restart");
      }
      @Override
      protected void onResume() {
            // TODO Auto-generated method stub
            super.onResume();
            /**
             * it show massage on LogCat when Activity Resume
             */
            Log.i("Second Activity", "Resume");
      }
     
      @Override
      protected void onStop() {
            // TODO Auto-generated method stub
            super.onStop();
            /**
             * it show massage on LogCat when Activity  stop
             */
            Log.i("Second Activity", "Stop");
      }
      @Override
      protected void onDestroy() {
            // TODO Auto-generated method stub
            super.onDestroy();
            /**
             * it show massage on LogCat when Activity Destroy
             */
            Log.i("Second Activity", "Destroy");
      }

}


setting.java



package com.exc.first;

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

public class setting extends Activity {

      @Override
      protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.setting);
           
           
      }
      @Override
      protected void onStart() {
            // TODO Auto-generated method stub
            super.onStart();
            /**
             * it show massage on LogCat when Activity starts
             */
            Log.i("Third Activity", "Start");
      }
     
      @Override
      protected void onPause() {
            // TODO Auto-generated method stub
            super.onPause();
            /**
             * it show massage on LogCat when Activity Pause
             */
            Log.i("Third Activity", "Pause");
      }
      @Override
      protected void onRestart() {
            // TODO Auto-generated method stub
            super.onRestart();
            /**
             * it show massage on LogCat when Activity Restarts
             */
            Log.i("Third Activity", "Restart");
      }
      @Override
      protected void onResume() {
            // TODO Auto-generated method stub
            super.onResume();
            /**
             * it show massage on LogCat when Activity Resume
             */
            Log.i("Third Activity", "Resume");
      }
     
      @Override
      protected void onStop() {
            // TODO Auto-generated method stub
            super.onStop();
            /**
             * it show massage on LogCat when Activity stop
             */
            Log.i("Third Activity", "Stop");
      }
      @Override
      protected void onDestroy() {
            // TODO Auto-generated method stub
            super.onDestroy();
            /**
             * it show massage on LogCat when Activity Destroy
             */
            Log.i("Third Activity", "Destroy");
      }
}



and don't forget to enter entry of your new java file into manifest file



<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.exc.first"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="10" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".Ex1Activity"
                  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"
                  android:label="@string/app_name">
           
        </activity>
        <activity android:name=".setting"
                  android:label="@string/app_name">
           
        </activity>

    </application>
</manifest>

Output



No comments: