lecture 2 activities
TRANSCRIPT
-
8/13/2019 Lecture 2 Activities
1/54
Lecture 2ACTIVITIES
-
8/13/2019 Lecture 2 Activities
2/54
Application Life Cycle
Application components life cycle is the cycle from instantiating the component uncompletely destroying it.
Application lifetime is not directly controlled by you. Its determined by the system
How important these components are and how much memory is available are the kprocess
-
8/13/2019 Lecture 2 Activities
3/54
Activity Life Cycle
-
8/13/2019 Lecture 2 Activities
4/54
ActivitiesSubclasses 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
Running new activity closes the previous one and we get notified by the state via tlife cycle callbacks.
-
8/13/2019 Lecture 2 Activities
5/54
Activity Life Cycle
-
8/13/2019 Lecture 2 Activities
6/54
Activity Life Cycle
-
8/13/2019 Lecture 2 Activities
7/54
Activity Life Cycle
-
8/13/2019 Lecture 2 Activities
8/54
Creating ActivitiesExtend activity class
Add entry to it the manifest file
open your manifest file and add an element as a child of the
-
8/13/2019 Lecture 2 Activities
9/54
How to manage Activity Life CycBy implementing callback methods
-
8/13/2019 Lecture 2 Activities
10/54
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
-
8/13/2019 Lecture 2 Activities
11/54
Running DDMSFrom Eclipse:
Click Window > Open Perspective > Other... > DDMS.
-
8/13/2019 Lecture 2 Activities
12/54
Using LogCatLogCat outputs the messages that you print out using the Log class along with othmessages 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
-
8/13/2019 Lecture 2 Activities
13/54
How to use itLog is a logging class that you can utilize in your code to print out messages to theCommon 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 );
-
8/13/2019 Lecture 2 Activities
14/54
Example
-
8/13/2019 Lecture 2 Activities
15/54
Building UserInterfaces
-
8/13/2019 Lecture 2 Activities
16/54
How to Build our GUIAll 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"
HybridGUI components and resources are described in XML files
All other stuff are written in Java
-
8/13/2019 Lecture 2 Activities
17/54
Note
Use Java-based for building dynamic guiOtherwise, use the hybrid approach
Easy to maintain
Good separation between Presentation layer and business lo
-
8/13/2019 Lecture 2 Activities
18/54
Building GUI in Jav
-
8/13/2019 Lecture 2 Activities
19/54
-
8/13/2019 Lecture 2 Activities
20/54
-
8/13/2019 Lecture 2 Activities
21/54
Building UI in XML
-
8/13/2019 Lecture 2 Activities
22/54
Building UI in XMLIn Java
In XML
-
8/13/2019 Lecture 2 Activities
23/54
Building UI in Hybri
-
8/13/2019 Lecture 2 Activities
24/54
Hybrid
-
8/13/2019 Lecture 2 Activities
25/54
-
8/13/2019 Lecture 2 Activities
26/54
-
8/13/2019 Lecture 2 Activities
27/54
Link the xml component with theJava Component
findViewById ( View )Use it to get a reference to a view
-
8/13/2019 Lecture 2 Activities
28/54
Common View Attributesandroid:layout_width="105dp"
android:layout_height="wrap_content"
android:text=Hello"
android:textSize="20sp
-
8/13/2019 Lecture 2 Activities
29/54
MeasurementsThe small quick advice:
Use sp for fonts
Use dp for every thing else
*dp stands for density pixels or Density-independent pixel
-
8/13/2019 Lecture 2 Activities
30/54
MeasurementsScreen 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 pper inch).
-
8/13/2019 Lecture 2 Activities
31/54
View IDEach 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"
-
8/13/2019 Lecture 2 Activities
32/54
Event ListenersUsing Separate Listener Class
Using Named Inner Class
Using Anonymous inner class
Having the main activity implements the listener onClickListener
-
8/13/2019 Lecture 2 Activities
33/54
View groupsThey are invisible containers that holds other views.
The base for all layouts like FrameLayout, GridView, List View, Table Layout and ma
-
8/13/2019 Lecture 2 Activities
34/54
Viewsare 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
-
8/13/2019 Lecture 2 Activities
35/54
Example
-
8/13/2019 Lecture 2 Activities
36/54
Layouts
-
8/13/2019 Lecture 2 Activities
37/54
LayoutsYour 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
-
8/13/2019 Lecture 2 Activities
38/54
Linear LayoutLinearLayout is a ViewGroup that displays child View elements in a linear direction
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
-
8/13/2019 Lecture 2 Activities
39/54
Linear Layout commonly usedattributesOrientation : 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.
-
8/13/2019 Lecture 2 Activities
40/54
Example
-
8/13/2019 Lecture 2 Activities
41/54
Relative Layout
Lets child views specify their position relative to the parent
view or to each other (specified by ID)
-
8/13/2019 Lecture 2 Activities
42/54
Relative LayoutReferring to the container :
android:layout_alignParentTopandroid: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
-
8/13/2019 Lecture 2 Activities
43/54
Relative LayoutIs a very powerful utility for designing a user interface
It can eliminate nested view groups and keep your layout hierarchy flat, which impperformance.
If you find yourself using several nested LinearLayout groups, you may be able to rwith 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/
http://developer.android.com/guide/topics/ui/layout/relative.htmlhttp://www.mkyong.com/android/android-relativelayout-example/http://www.mkyong.com/android/android-relativelayout-example/http://www.mkyong.com/android/android-relativelayout-example/http://www.mkyong.com/android/android-relativelayout-example/http://www.mkyong.com/android/android-relativelayout-example/http://www.mkyong.com/android/android-relativelayout-example/http://www.mkyong.com/android/android-relativelayout-example/http://developer.android.com/guide/topics/ui/layout/relative.htmlhttp://developer.android.com/guide/topics/ui/layout/relative.html -
8/13/2019 Lecture 2 Activities
44/54
Table LayoutViews are grouped in rows and columns
Used in conjunction with TableRow
Columns are determined by Android based on the maximum number of the viewsrow.
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.
-
8/13/2019 Lecture 2 Activities
45/54
Table layout
-
8/13/2019 Lecture 2 Activities
46/54
Frame Layoutdesigned 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.
-
8/13/2019 Lecture 2 Activities
47/54
Views
-
8/13/2019 Lecture 2 Activities
48/54
TextViewandroid: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"
-
8/13/2019 Lecture 2 Activities
49/54
ImageView
-
8/13/2019 Lecture 2 Activities
50/54
ImageViewChange Image at runtime
image.setImageResource(R.drawable.myImage);
-
8/13/2019 Lecture 2 Activities
51/54
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
-
8/13/2019 Lecture 2 Activities
52/54
CheckBoxA checkbox is a specific type of two-states button that can be either checked or un
-
8/13/2019 Lecture 2 Activities
53/54
Radio Buttons
-
8/13/2019 Lecture 2 Activities
54/54
END