Sie sind auf Seite 1von 54

Lecture 2

ACTIVITIES

Application Life Cycle


Application components life cycle is the cycle from instantiating the component until completely destroying it. Application lifetime is not directly controlled by you. Its determined by the system itself. How important these components are and how much memory is available are the keys for this process

Activity Life Cycle

Activities
Subclasses of the Activity class Managed by Activity Manager (managed components)

Each activity is given a window in which to draw its user interface.


The "main" activity is presented to the user when launching the application for the first time Running new activity closes the previous one and we get notified by the state via the activity life cycle callbacks.

Activity Life Cycle

Activity Life Cycle

Activity Life Cycle

Creating Activities
Extend activity class Add entry to it the manifest file

open your manifest file and add an <activity> element as a child of the <application> element
The android: name attribute is a must to have attribute and Once you publish your application, you should not change this name. Implement at least onCreate() method Implement the other required call backs

How to manage Activity Life Cycle


By implementing callback methods

Dalvik Debug Monitor Server (DDMS)


The android debugging tool Provides

logging information (logcat ),


Thread and heap information on the device process, and radio state information incoming call and SMS spoofing, location data spoofing, and more

Running DDMS
From Eclipse: Click Window > Open Perspective > Other... > DDMS.

Using LogCat
LogCat outputs the messages that you print out using the Log class along with other system messages such as stack traces when exceptions are thrown. Filter certain messages with the following buttons: Verbose Debug Info Warn Error You can also setup your own custom filter

How to use it
Log is a logging class that you can utilize in your code to print out messages to the LogCat. Common logging methods include: v(String, String) (verbose) d(String, String) (debug) i(String, String) (information) w(String, String) (warning) e(String, String) (error) Example Log.i(This is an information message);

Example

Building User Interfaces

How to Build our GUI


All in Java GUI components, listeners and event handlers are written in Java

All in XML
GUI components and event handler assignment are written in XML E.g. android:onClick="oClickHandler" Hybrid GUI components and resources are described in XML files All other stuff are written in Java

Note
Use Java-based for building dynamic gui Otherwise, use the hybrid approach
Easy to maintain Good separation between Presentation layer and business logic

Building GUI in Java

Building GUI in Java


LinearLayout myTestLayout = new LinearLayout(this); Button b = new Button(this);

b.setText("I'm Here");
myTestLayout.addView(b); setContentView(myTestLayout);

Building UI in XML

Building UI in XML
In Java

In XML <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btn1" android:onClick="oClickHandler" />

Building UI in Hybrid

Hybrid

Link the xml component with the Java Component


findViewById ( View ) Use it to get a reference to a view

Common View Attributes


android:layout_width="105dp" android:layout_height="wrap_content"

android:text=Hello"
android:textSize="20sp

Measurements
The small quick advice: Use sp for fonts

Use dp for every thing else


*dp stands for density pixels or Density-independent pixel

Measurements
Screen size Actual physical size, measured as the screen's diagonal.

E.g. 2.3 inches


Screen density The quantity of pixels within a physical area of the screen; usually referred to as ppi (dots/pixel per inch).

View ID
Each view you use should have an id When you are going to refer to this view you will need its id.

To refer to view id inside xml


android:layout_below="@id/label" To generate an id for a view android:id="@+id/ok"

Event Listeners
Using Separate Listener Class Using Named Inner Class

Using Anonymous inner class


Having the main activity implements the listener onClickListener

View groups
They are invisible containers that holds other views. The base for all layouts like FrameLayout, GridView, List View, Table Layout and many others

Views
are the main building blocks for user interface components The base class for all widgets

Examples are Buttons, Edit Texts, Text Views,


All extends class android.view.View

Example

Layouts

Layouts
Your layout is the architecture for the user interface in an Activity It defines the layout structure and holds all the elements that appear to the user

Example:
Linear Layout Frame Layout Table Layout Relative Layout

Linear Layout
LinearLayout is a ViewGroup that displays child View elements in a linear direction, either vertically or horizontally. Via the orientation attribute supports assigning a weight to children android:orientation="horizontal" How Can you build this ?

Linear Layout commonly used attributes


Orientation : horizontal or vertical fill model: fill_parent, wrap_content

Weight: 1,2,3,.0
Gravity: alignment (left, center, right, top, bottom) Padding: space from boundaries to content Margin: space from boundaries to the parents Android gravity vs. layout_gravity android:gravity sets the gravity of the content of the View its used on. android:layout_gravity sets the gravity of the View or Layout in its parent.

Example

Relative Layout
Lets child views specify their position relative to the parent view or to each other (specified by ID)

Relative Layout
Referring to the container : android:layout_alignParentTop android:layout_alignParentLeft android:layout_centerInParent android:layout_centerHorizontal Referring to other widgets android:layout_alignTop android:layout_alignLeft, android:layout_alignRight android:layout_below android:layout_toLeftOf

Relative Layout
Is a very powerful utility for designing a user interface It can eliminate nested view groups and keep your layout hierarchy flat, which improves performance. If you find yourself using several nested LinearLayout groups, you may be able to replace them with a single RelativeLayout. For more about Relative layout

http://developer.android.com/guide/topics/ui/layout/relative.html
http://www.mkyong.com/android/android-relativelayout-example/

Table Layout
Views are grouped in rows and columns Used in conjunction with TableRow

Columns are determined by Android based on the maximum number of the views in the table row.
Layout_Span attribute can be used to make a view span multiple columns each column occupies space equal to the size of the largest widget in it.

Table layout

Frame Layout
designed to block out an area on the screen to display a single item. FrameLayout should be used to hold a single child view

Multiple inner views will


be overlapped.

Views

TextView
android:id="@+id/myTextView1" android:layout_width="wrap_content"

android:layout_height="wrap_content"
android:background="#ff0000ff" android:padding="3px" android:text="Enter User Name" android:textSize="16sp" android:textStyle="bold" android:gravity="center"

ImageView
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <ImageView android:layout_width="250" android:layout_height="250" android:src="@drawable/1" android:layout_centerInParent="true"/> </RelativeLayout>

ImageView
Change Image at runtime image.setImageResource(R.drawable.myImage);

Edit Text
android:autoText, (true/false) provides automatic spelling assistance android:capitalize, (words/sentences) automatic capitalization

android:digits, to configure the field to accept only certain digits


android:singleLine, is the field for single-line / multiple-line input android:passwords, (true/false) controls fields visibility android:numeric, (integer, decimal, signed) controls numeric format android:phonenumber, (true/false) Formatting phone numbers

CheckBox
A checkbox is a specific type of two-states button that can be either checked or unchecked. <CheckBox

android:id="@+id/checkBox1"
android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="true" android:text="Meat with rice" android:textSize="24" />

Radio Buttons
<RadioGroup android:layout_width="fill_parent" android:layout_height="fill_parent" > <RadioButton android:id="@+id/btnTea" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Tea" android:textSize="30sp" android:checked="true"/> </RadioGroup>

END

Das könnte Ihnen auch gefallen