You are on page 1of 10

ACTIVITY:

An Application can have one or more activities and the main purpose of an activity is to interact with the user. From the moment an activity appears on the screen to the moment it is hidden, it goes through a number of stages known as an activity life cycle. import android.app.Activity; import android.os.Bundle; public class MainActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } } Your activity class would then load its UI component using the XML file defined in your res/layout folder. In this example, you would load the UI from the main.xml file: setContentView(R.layout.main); Every activity you have in your application must be declared in your AndroidManifest.xml file,like this: <?xml version=1.0 encoding=utf-8?> <manifest xmlns:android=http://schemas.android.com/apk/res/android package=net.learn2develop.Activities android:versionCode=1 android:versionName=1.0> <application android:icon=@drawable/icon android:label=@string/app_name> <activity android:name=.MainActivity android:label=@string/app_name> <intent-filter> <action android:name=android.intent.action.MAIN /> <category android:name=android.intent.category.LAUNCHER/> </intent-filter> </activity> </application> <uses-sdk android:minSdkVersion=9 /> </manifest> The Activity base class defines a series of events that governs the life cycle of an activity. The Activity class defines the following events: onCreate() : Called when the activity is first created onStart() : Called when the activity becomes visible to the user

onResume() :Called when the activity starts interacting with the user onPause() :Called when the current activity is being paused and the previous activity is being resumed onStop() : Called when the activity is no longer visible to the user onDestroy() : Called before the activity is destroyed by the system onRestart() : Called when the activity has been stopped and is restarting again

public void onStart() { super.onStart(); Log.d(tag, In the onStart() event); } public void onRestart() { super.onRestart(); Log.d(tag, In the onRestart() event); } public void onResume() { super.onResume(); Log.d(tag, In the onResume() event); } public void onPause() { super.onPause(); Log.d(tag, In the onPause() event); } public void onStop() { super.onStop(); Log.d(tag, In the onStop() event); } public void onDestroy() { super.onDestroy(); Log.d(tag, In the onDestroy() event); } }

3. Press F11 to debug the application on the Android Emulator. 4. When the activity is first loaded, you should see the following in the LogCat window (click on the Debug perspective; see also Figure 2-3): 12-28 13:45:28.115: DEBUG/Events(334): In the onCreate() event 12-28 13:45:28.115: DEBUG/Events(334): In the onStart() event 12-28 13:45:28.115: DEBUG/Events(334): In the onResume() event

LINEAR LAYOUT
The LinearLayout arranges views in a single column or a single row. Child views can be arranged either vertically or horizontally. To see how LinearLayout works, consider the following elements typically contained in the main.xml file: VERTICAL
<?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" > <Button android:id="@+id/button1" android:layout_width="275dp" android:layout_height="wrap_content" android:text="button1" /> <Button android:id="@+id/button2" android:layout_width="278dp" android:layout_height="wrap_content" android:text="button2" /> <Button android:id="@+id/button3" android:layout_width="282dp" android:layout_height="wrap_content" android:text="button3" /> </LinearLayout>

HORIZONTAL:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="left" android:layout_weight="0.53" android:text="Button" /> <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_weight="0.50" android:text="Button" />

<Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="0.73" android:text="BUTTON" /> </LinearLayout>

Linear

Horizontal

ABSOLUTE LAYOUT
The AbsoluteLayout enables you to specify the exact location of its children. Consider the following UI defined in main.xml:
<?xml version=1.0 encoding=utf-8?> <AbsoluteLayout android:layout_width=fill_parent android:layout_height=fill_parent xmlns:android=http://schemas.android.com/apk/res/android > <Button android:layout_width=188dp android:layout_height=wrap_content android:text=Button android:layout_x=126px android:layout_y=361px/> <Button android:layout_width=113dp android:layout_height=wrap_content android:text=Button android:layout_x=12px android:layout_y=361px/> </AbsoluteLayout>

The two Button views located at their specified positions using the android_layout_x and android_layout_y attributes.

However, there is a problem with the Absolute Layout when the activity is viewed on a highresolution screen .For this reason, the Absolute Layout has been deprecated since Android

However, there is a problem with the Absolute Layout when the activity is viewed on a highresolution screen (see Figure 3-6). For this reason, the Absolute Layout has been deprecated since Android 1.5 (although it is still supported in the current version). You should avoid using the AbsoluteLayout in your UI, as it is not guaranteed to be supported in future versions of Android

RELATIVE LAYOUT:
The RelativeLayout enables you to specify how child views are positioned relative to each other.Consider the following main.xml file:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="28dp" android:layout_marginTop="29dp" android:text="Large Text"/> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/textView1" android:layout_below="@+id/textView1" android:layout_marginTop="22dp" android:text="Large Text" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/textView2" android:layout_marginLeft="29dp" android:layout_marginTop="23dp" android:layout_toRightOf="@+id/textView2" android:text="Button" /> </RelativeLayout>

Notice that each view embedded within the RelativeLayout has attributes that enable it to align with another view. These attributes are as follows: layout_alignParentTop layout_alignParentLeft layout_alignLeft layout_alignRight layout_below layout_centerHorizontal TABLE LAYOUT The TableLayout groups views into rows and columns. You use the <TableRow> element to designate a row in the table. Each row can contain one or more views. Each view you place within a row forms a cell. The width of each column is determined by the largest width of each cell in that column.
<?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="match_parent" > <TableRow > <TextView android:text="User Name" android:width ="120dp"/> <EditText android:id="@+id/txtUserName"

android:width="200dp" /> </TableRow> <TableRow> <TextView android:text="PassWord" /> <EditText android:id="@+id/editText2" android:password="true" /> </TableRow> <TableRow android:id="@+id/tableRow1" android:layout_width="wrap_content" android:layout_height="wrap_content" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" /> </TableRow> </TableLayout>

FRAME LAYOUT:

<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <ImageView android:id="@+id/imageView1" android:layout_width="301dp" android:layout_height="301dp" android:src="@drawable/ic_launcher" /> <ImageView android:id="@+id/imageView2" android:layout_width="206dp" android:layout_height="206dp" android:src="@drawable/ic_launcher" /> <ImageView android:id="@+id/imageView3" android:layout_width="120dp" android:layout_height="120dp" android:src="@drawable/ic_launcher" /> </FrameLayout>

You might also like