Your SlideShare is downloading. ×
Android Lesson 2
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Saving this for later?

Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime - even offline.

Text the download link to your phone

Standard text messaging rates apply

Android Lesson 2

656
views

Published on

- Android Application Overview …

- Android Application Overview
- Activity Lifecycle
- Basic Android User Interface components
- Layouts
- XML Layout Attributes
- Dimensions

Published in: Technology

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
656
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
26
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Android - Lesson 2 Daniela da Cruz Universidade Lusófona do Porto September, 20121 of 35
  • 2. Android Application OverviewActivity LifecycleBasic Android User Interface components Activity Fragments View and ViewGroupLayoutsXML Layout AttributesDimensionsExercise2 of 35
  • 3. LayoutAndroid Application OverviewActivity LifecycleBasic Android User Interface components Activity Fragments View and ViewGroupLayoutsXML Layout AttributesDimensionsExercise3 of 35
  • 4. Android Application OverviewAn Android application consists of various functionalities. Someexamples are editing a note, playing a music file, ringing an alarm, oropening a phone contact.These functionalities can be classified intofour different Android components:Every application is made up of one or more of these components.4 of 35
  • 5. LayoutAndroid Application OverviewActivity LifecycleBasic Android User Interface components Activity Fragments View and ViewGroupLayoutsXML Layout AttributesDimensionsExercise5 of 35
  • 6. Activity Lifecycle6 of 35
  • 7. Activity LifecycleNote the following:• Changing the screen orientation destroys and recreates the activity from scratch.• Pressing the Home button pauses the activity, but does not destroy it.• Pressing the Application icon might start a new instance of the activity, even if the old one was not destroyed.• Letting the screen sleep pauses the activity and the screen awakening resumes it. (This is similar to taking an incoming phone call.)7 of 35
  • 8. LayoutAndroid Application OverviewActivity LifecycleBasic Android User Interface components Activity Fragments View and ViewGroupLayoutsXML Layout AttributesDimensionsExercise8 of 35
  • 9. ActivityAn Activity represents the visual representation of an Androidapplication.Activities use Views and Fragments to create the user interface andto interact with the user.An Android application can have several Activities.9 of 35
  • 10. FragmentsFragments are components which run in the context of an Activity.Fragment components encapsulate application code so that it is easierto reuse it and to support different sized devices.Fragments are optional, you can use Views and ViewGroups directly inan Activity but in professional applications you always use them toallow the reuse of your user interface components on different sizeddevices.10 of 35
  • 11. View and ViewGroupViews are user interface widgets, e.g. buttons or text fields. Viewshave attributes which can be used to configure their appearance andbehavior.A ViewGroup is responsible for arranging other Views. ViewGroups isalso called layout managers.ViewGroups can be nestled to create complex layouts. You should notnestle ViewGroups too deeply as this has a negative impact on theperformance.11 of 35
  • 12. View and ViewGroupThe user interface for each component of your app is defined using ahierarchy of View and ViewGroup objects.The easiest and most effective way to define a layout is with an XMLfile.12 of 35
  • 13. LayoutAndroid Application OverviewActivity LifecycleBasic Android User Interface components Activity Fragments View and ViewGroupLayoutsXML Layout AttributesDimensionsExercise13 of 35
  • 14. LayoutsAn Android layout is a class that handles arranging the way itschildren appear on the screen. Anything that is a View (or inheritsfrom View) can be a child of a layout. All of the layouts inherit fromViewGroup (which inherits from View) so you can nest layouts.The standard Layouts are:• AbsoluteLayout• FrameLayout• LinearLayout• RelativeLayout• TableLayout14 of 35
  • 15. AbsoluteLayout (deprecated)AbsoluteLayout is based on the simple idea of placing each control atan absolute position. You specify the exact x and y coordinates onthe screen for each control.15 of 35
  • 16. FrameLayoutFrameLayout is designed to display a single item at a time. You canhave multiple elements within a FrameLayout but each element will bepositioned based on the top left of the screen. Elements that overlapwill be displayed overlapping.16 of 35
  • 17. LinearLayoutLinearLayout is a view group that aligns all children in a singledirection, vertically or horizontally.You specify whether that line is vertical or horizontal usingandroid:orientation.17 of 35
  • 18. RelativeLayout (1)RelativeLayout lays out elements based on their relationships with oneanother, and with the parent container. RelativeLayout is verypowerful but the most complicated one, and we need severalproperties to actually get the layout we want.18 of 35
  • 19. RelativeLayout (2)These properties will layout elements relative to the parentcontainer:• android:layout_alignParentBottom — Places the bottom of the element on the bottom of the container.• android:layout_alignParentLeft — Places the left of the element on the left side of the container.• android:layout_alignParentRight — Places the right of the element on the right side of the container.• android:layout_alignParentTop — Places the element at the top of the container.19 of 35
  • 20. RelativeLayout (3)(cont.)• android:layout_centerHorizontal — Centers the element horizontally within its parent container.• android:layout_centerInParent — Centers the element both horizontally and vertically within its container.• android:layout_centerVertical — Centers the element vertically within its parent container.20 of 35
  • 21. RelativeLayout (4)These properties allow you to layout elements relative to otherelements on screen.• android:layout_above — Places the element above the specified element.• android:layout_below — Places the element below the specified element21 of 35
  • 22. RelativeLayout (5)(cont.)• android:layout_toLeftOf — Places the element to the left of the specified element.• android:layout_toRightOf — Places the element to the right of the specified element.The value for each of these elements must be a reference to anotherresource, in the form “@[+][package:]type:name”.22 of 35
  • 23. RelativeLayout (6)These properties allow you to specify how elements are aligned inrelation to other elements.• android:layout_alignBaseline — Aligns baseline of the new element with the baseline of the specified element.• android:layout_alignBottom — Aligns the bottom of new element in with the bottom of the specified element.• android:layout_alignLeft — Aligns left edge of the new element with the left edge of the specified element.23 of 35
  • 24. RelativeLayout (7)(cont.)• android:layout_alignRight — Aligns right edge of the new element with the right edge of the specified element.• android:layout_alignTop — Places top of the new element in alignment with the top of the specified element.24 of 35
  • 25. RelativeLayout (8)25 of 35
  • 26. TableLayoutTableLayout organizes content into rows and columns. The rows aredefined in the layout XML, and the columns are determinedautomatically by Android. This is done by creating at least onecolumn for each element.26 of 35
  • 27. LayoutAndroid Application OverviewActivity LifecycleBasic Android User Interface components Activity Fragments View and ViewGroupLayoutsXML Layout AttributesDimensionsExercise27 of 35
  • 28. XML Layout AttributesAt compile time, references to the resources are gathered into anauto-generated wrapper class called R.java. The Android AssetPackaging Tool (aapt) autogenerates this file.The syntax for an ID, inside an XML tag is:android:id="@+id/my_button"The at-symbol (@) at the beginning of the string indicates that theXML parser should parse and expand the rest of the ID string andidentify it as an ID resource. The plus-symbol (+) means that this is anew resource name that must be created and added to the R.java file.28 of 35
  • 29. XML Layout AttributesWhen referencing an Android resource ID, you do not need theplus-symbol, but must add the android package namespace, like so:android:id="@android:id/empty"With the android package namespace in place, we’re now referencingan ID from the android.R resources class, rather than the localresources class.29 of 35
  • 30. LayoutAndroid Application OverviewActivity LifecycleBasic Android User Interface components Activity Fragments View and ViewGroupLayoutsXML Layout AttributesDimensionsExercise30 of 35
  • 31. DimensionsA dimension is specified with a number followed by a unit of measure.The following units of measure are supported by Android:• dp — Density-independent Pixels: An abstract unit that is based on the physical density of the screen. These units are relative to a 160 dpi (dots per inch) screen, on which 1dp is roughly equal to 1px. When running on a higher density screen, the number of pixels used to draw 1dp is scaled up by a factor appropriate for the screen’s dpi.• sp — Scale-independent Pixels: This is like the dp unit, but it is also scaled by the user’s font size preference.• pt — Points: 1/72 of an inch based on the physical size of the screen.31 of 35
  • 32. Dimensions(cont.)• px — Pixels: Corresponds to actual pixels on the screen. This unit of measure is not recommended because the actual representation can vary across devices.• mm — Millimeters: Based on the physical size of the screen.• in — Inches: Based on the physical size of the screen.32 of 35
  • 33. LayoutAndroid Application OverviewActivity LifecycleBasic Android User Interface components Activity Fragments View and ViewGroupLayoutsXML Layout AttributesDimensionsExercise33 of 35
  • 34. ExerciseCreate your first Android application to understand the activitylifecycle.You should display a message as soon as an event (onCreate,onStart, etc) is fired.To display messages use the class android.widget.Toast.34 of 35
  • 35. Slides made using the help of:• http://developer.android.com/• Programming Android. Zigurd Mednieks, Laird Dornin, G. Blake Meike, Masumi Nakamura. O’Reilly Media. July 2011.• The Android Developer’s Cookbook: Building Applications with the Android SDK. James Steele, Nelson To.• http://www.learn-android.com35 of 35