• RSS
  • Delicious
  • Digg
  • Facebook
  • Twitter
  • Linkedin
Posted by Jack - - 2 comments


Activity Lifecycle

Activities in the system are managed as an activity stack. When a new activity is started, it is placed on the top of the stack and becomes the running activity -- the previous activity always remains below it in the stack, and will not come to the foreground again until the new activity exits.
An activity has essentially four states:
·         If an activity in the foreground of the screen (at the top of the stack), it is active or running.
·         If an activity has lost focus but is still visible (that is, a new non-full-sized or transparent activity has focus on top of your activity), it is paused. A paused activity is completely alive (it maintains all state and member information and remains attached to the window manager), but can be killed by the system in extreme low memory situations.
·         If an activity is completely obscured by another activity, it is stopped. It still retains all state and member information, however, it is no longer visible to the user so its window is hidden and it will often be killed by the system when memory is needed elsewhere.
·         If an activity is paused or stopped, the system can drop the activity from memory by either asking it to finish, or simply killing its process. When it is displayed again to the user, it must be completely restarted and restored to its previous state.
The following diagram shows the important state paths of an Activity. The square rectangles represent callback methods you can implement to perform operations when the Activity moves between states. The colored ovals are major states the Activity can be in.
https://lh4.googleusercontent.com/_xnrF0YSUul8/TWubJRPfQ6I/AAAAAAAAADc/fJ7QbXfZl6Q/s512/activity_lifecycle.png

Posted by Jack - - 4 comments


CHECKBOX


Android checkbox is used to select more than one option at a time.
Example:  In an admission form we may need to select both diploma & BE in Graduate option.
For this we can use checkbox which will enable us to select multiple options.



SOURCE CODE [main.xml] is

<xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/LinearLayout01"
                android:layout_width="fill_parent"

android:layout_height="fill_parent"
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:orientation="vertical">
                <CheckBox android:id="@+id/check1"
android:layout_width="100px"
                                android:layout_height="wrap_content"
android:text="Android" />
                <CheckBox android:id="@+id/check2"
android:layout_width="100px"
                                android:layout_height="wrap_content"
android:text="iPhone" />
                <Button android:id="@+id/button1"
android:layout_width="100px"
                                android:layout_height="wrap_content"
android:text="Check" />
</LinearLayout>
 
SOURCE CODE [CheckBoxExample.java] is

package com.CheckBoxExample;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;

public class CheckBoxExample extends Activity implements OnClickListener
{
Button b1;
CheckBox c1, c2;
Toast msg;

public void onCreate(Bundle icicle)
{
                super.onCreate(icicle);
                setContentView(R.layout.main);

                c1 = (CheckBox) findViewById(R.id.check1);
                c2 = (CheckBox) findViewById(R.id.check2);
                b1 = (Button) findViewById(R.id.button1);
                b1.setOnClickListener(this);
}

public void onClick(View arg0)
{
                // TODO Auto-generated method stub
                if (c1.isChecked()==true && c2.isChecked()==true)
                {
                                msg = Toast.makeText(CheckBoxExample.this,
                                                "Android and Iphone", Toast.LENGTH_SHORT);
                                 msg.show();
                }
                if (c1.isChecked()==true && c2.isChecked()==false)
                                {
                                msg = Toast.makeText(CheckBoxExample.this,
                                                "Android", Toast.LENGTH_SHORT);
                                                 msg.show();
                                }
                                if (c1.isChecked()==false && c2.isChecked()==true)
                                {
                                                msg = Toast.makeText(CheckBoxExample.this,
                                                                "Iphone", Toast.LENGTH_SHORT);
                                                msg.show();
                                }
                                if (c1.isChecked()==false && c2.isChecked()==false)
{
                                                msg = Toast.makeText(CheckBoxExample.this,
                                                                "Nothing Selected", Toast.LENGTH_SHORT);
                                                msg.show();
                }
                }

}


The OUTPUT will be

https://lh5.googleusercontent.com/_xnrF0YSUul8/TWYzJnWo1II/AAAAAAAAACc/ZyoXm6hd3xg/check_box_1.png https://lh6.googleusercontent.com/_xnrF0YSUul8/TWYzJi19ccI/AAAAAAAAACg/-hkJR62MUYw/check_box_2.png
 

Posted by Jack - - 8 comments


SIMPLE TOAST


To display a simple toast, we need to makeText for that toast & then call show() to display it.
To use makeText(), we need to send 3 arguments.
1) Context
2) Text to display in toast
3) Duration How much time its need to be show
At last call show() to display toast.


SOURCE CODE is

package com.ToastExample;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;

public class ToastExample extends Activity
{
                public void onCreate(Bundle savedInstanceState)
                {
                                super.onCreate(savedInstanceState);
                                setContentView(R.layout.main);

                                 Toast msg = Toast.makeText(ToastExample.this,
                                                "YOUR TEXT HERE", Toast.LENGTH_LONG);
                                msg.show();

                 }
}



The OUTPUT will be


https://lh4.googleusercontent.com/_xnrF0YSUul8/TVpxrtIzlkI/AAAAAAAAAA0/rIqQmEb4AJ0/simple_toast.png