Presentation is loading. Please wait.

Presentation is loading. Please wait.

Unlocking Android Chapter 4.  Understanding activities and views  Exploring the Activity lifecycle  Working with resources  Defining the AndroidManifest.xml.

Similar presentations


Presentation on theme: "Unlocking Android Chapter 4.  Understanding activities and views  Exploring the Activity lifecycle  Working with resources  Defining the AndroidManifest.xml."— Presentation transcript:

1 Unlocking Android Chapter 4

2  Understanding activities and views  Exploring the Activity lifecycle  Working with resources  Defining the AndroidManifest.xml

3

4  Views and other Android components ◦ compiled into a binary form R.java ◦ available to applications as resources ◦ make use of strings, colors, styles, and graphics  R.java class ◦ automatically generated ◦ provides a reference to individual resources ◦ is the bridge between binary references and source. ◦ to grab a string or a color and add it to a View.  AndroidManifest. Xml ◦ where your application begins ◦ what its permissions are ◦ Activities (services and receivers ) it includes

5  Requirements ◦ Allows the user to search for restaurant reviews based on location and cuisine. ◦ Allow the user to call, visit the website of, or map directions to a selected restaurant.  Need three basic screens to begin with: ◦ A criteria screen where a user enters parameters to search for restaurant reviews ◦ A list-of-reviews screen that shows paged results that match the specified criteria ◦ A detail page that shows the review details for a selected review item  A screen =an Activity

6

7

8  it gives our application a context, ◦ because Activity itself extends android.app.ApplicationContext;  it brings the Android lifecycle methods into play;  it gives the framework a hook to start and run your application; and  it provides a container into which View elements can be placed.

9  Needs three Views ◦ because an Activity represents an interaction with the user, it needs to provide components on the screen. ◦ location, cuisine, and grabReviews  Location ◦ is a type of View ◦ known as an EditText  cuisine ◦ is a fancy select list component, ◦ known in Android terms as a Spinner, and  grabReviews ◦ is a Button.

10  This is one of a series of important lifecycle methods the Activity class provides.  Every Activity will override onCreate(), ◦ component initialization steps are invoked, ◦ not every Activity will need to override other lifecycle methods.

11  setContentView() ◦ normally associate an XML layout file ◦ you do not have to use an XML file at all;  you can instead define all of your layout and View configuration in code, as Java objects. ◦ it is often easier, and better practice use an XML layout resource  decoupling,  each xml layout file for each Activity.  An XML layout file ◦ defines View objects,  are laid out in a tree,  can then be set into the Activity for use.

12  Dynamic Views ◦ Can bind to data, ◦ can be referenced in code ◦ cast to their respective subtypes  Static Views ◦ don’t need to interact with or update at runtime  like labels, ◦ do not need to be referenced in code

13  “Adapter” concept ◦ link views that contain collections ( AdapterView ) with data. ◦ a collection handler that returns each item in the collection as a View.  An Adapter ◦ is an interface ◦ a bridge between an AdapterView and the underlying data for that view.AdapterView ◦ provides access to the data items ◦ making a View for each item in the data setView ◦ many basic adapters:  ListAdapter, ArrayAdapter, GalleryAdapter, CursorAdapter, And more.  can create your own Adapter  An AdapterView  a subclass of view and viewgroup  an abstract class  is a view whose children are determined by an AdapterAdapter ◦ commonly used subclasses of AdapterView ◦ ListView, GridView, Spinner, and Gallery ListViewGridViewSpinnerGallery

14  using an ArrayAdapter that is populated with ◦ Context (this), ◦ a View element defined in an XML resource file  used for the element shown in the Spinner before it is selected; ◦ an array representing the data  After the spinner selected ◦ Set a different View (R.layout.spinner_view_dropdown) for the drop- down

15  Once our Adapter and its View elements are defined, we set it into the Spinner object  Handle event ◦ OnClickListener with our Button, in order to respond when the button is clicked

16  After the onCreate() method is complete, with the binding of data to our Spinner views, we have menu buttons and associated actions. ◦ different than on-screen Button views  Menu Button vs on-screen Button ◦ is invoked by pressing the Menu button on the device and tapping a selection (button and a tap) ◦ an on-screen button (single tap)

17

18  Menu class add() method to create a single MenuItem element B. ◦ passing a group ID, ◦ An ID, ◦ an order, and ◦ a text resource reference to create the menu item.  Assigning to the menu item an icon with the setIcon method. ◦ The text and the image are externalized from the code, again using Android’s concept of resources.  The MenuItem ◦ duplicates the on-screen Button with the same label for the “Get reviews” purpose.

19 onMenuItemSelected() event method C, where we parse the ID of the multiple possible menu items with a case/switch statement.

20 HandleGetReviews puts the user’s selection state in the Application object sets up to call the next screen. used in multiple places, 1.from our on-screen Button 2.from our MenuItem.

21  Used internally by Android for many purposes,  Can be extended, ◦ RestaurantFinderApplication ◦ includes a few member variables in JavaBean style  Store global state information ◦ Can be retrieve later  One way to pass objects back and forth between activities; ◦ You can also use public static members and Intent extras with Bundle objects. ◦ You can use the provided SQLite database, or ◦ You can implement your own ContentProvider and store data there.

22 asking another Activity to respond to the user’s selection of a menu item by calling startActivity (Intent intent)

23 validate() Called before we allow the next Activity to be invoked, display a pop-up-style alert dialog to the user if the location has not been specified. OnClickListener() button can be made to respond to a click event AlertDialog.BuilderAlertDialog.Builder(Context context)Context Constructor using a context for this builder and the AlertDialog it creates. AlertDialog

24

25  Every process running on the Android platform  is placed on a stack.  Android can’t keep every process running forever ◦ system resources are finite.  How to use the limit resources? ◦ When you use an Activity in the foreground, the system process that hosts that Activity is placed at the top of the stack, ◦ previous process (the one hosting whatever Activity was previously in the foreground) is moved down one notch

26  Foreground activity ◦ at the top of the screen that the user is currently interacting with  Visible activity ◦ an activity that is visible to the user but not in the foreground, ◦ such as one sitting behind a foreground dialog  Background activity ◦ Any process hosting a background Activity is next in line.  Empty process ◦ Any process not hosting any Activity (or Service or BroadcastReceiver)is last in line.

27

28

29

30

31

32 Related to layout

33

34

35  Layout is defined in terms of ◦ ViewGroup ◦ LayoutParams  ViewGroup ◦ is a container  is a View that contains other views (has children)  Child View elements must fit into the layout specified by their parents.  a child View has to lay itself out based on its parents’ LayoutParams  a child View specify a different layout for its own children  LayoutParams ◦ public static class- ViewGroup.LayoutParams ◦ used by views to tell their parents how they want to be laid out ◦ dimensions —width and height ◦ relative or absolute placement ◦ Margins

36

37

38  Allow other components to attach and listen for events. ◦ View class methods:  onFocusChanged(), onSizeChanged(), onLayout(), onTouchEvent()  Similarly, other items ◦ Activity lifecycle methods  onCreate(), onFreeze()

39

40

41

42 This maps to an XML layout file at src/res/layout/review_detail.xml

43

44  Views and layout are defined in XML ◦ rather than in Java code ◦ as resources  Benefits ◦ easier to work with ◦ decoupled from the code ◦ Reusable  Resource files ◦ are placed in the res/layout source directory ◦ many XML files allowed ◦ the root of these XML files is  subclasses ofViewGroup  RelativeLayout, LinearLayout, FrameLayout ◦ body of XML  represent the view/layout tree.

45 layout_[attribute] Android: padding = setPadding() android:id="@+id/[name] android: layout_below="@id/[name] style="@style/[stylename]

46

47

48

49

50

51  Android requires a manifest file for every application  Is placed in the root directory of the project source  Describes ◦ application context ◦ supported activities ◦ services ◦ intent receivers ◦ content providers ◦ permissions

52 To inform the system which implicit intents they can handle Each filter describes a capability of the component, a set of intents that the component is willing to receive.

53


Download ppt "Unlocking Android Chapter 4.  Understanding activities and views  Exploring the Activity lifecycle  Working with resources  Defining the AndroidManifest.xml."

Similar presentations


Ads by Google