1 / 53

UI Concepts

UI Concepts. Unlocking Android Chapter 4. Goal. Understanding activities and views Exploring the Activity lifecycle Working with resources Defining the AndroidManifest.xml. Understanding activities. Views and other Android components compiled into a binary form R.java

clint
Télécharger la présentation

UI Concepts

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. UI Concepts Unlocking Android Chapter 4

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

  3. Understanding activities

  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. RestaurantFinder • 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. 1 • 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.

  7. 2 • 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.

  8. 3 • 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.

  9. 4 • 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.

  10. 5 • 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

  11. 6 • “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. • provides access to the data items • making a View for each item in the data set • 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 Adapter • commonly used subclasses of AdapterView • ListView, GridView, Spinner, and Gallery

  12. 7- ArrayAdapter • 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

  13. 8-9 • 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

  14. menu buttons • 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)

  15. 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.

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

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

  18. Application object • 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.

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

  20. 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.Builder(Contextcontext) Constructor using a context for this builder and the AlertDialog it creates.

  21. Exploring the Activity lifecycle

  22. Activity lifecycle • 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

  23. Process priorities • 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.

  24. Working with Views

  25. View- building blocks of the UI Related to layout

  26. Understanding layout • 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 Viewhas 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

  27. Grasping events

  28. View items are Observable • Allow other components to attach and listen for events. • View class methods: • onFocusChanged(), onSizeChanged(), onLayout(), onTouchEvent() • Similarly, other items • Activity lifecycle methods • onCreate(), onFreeze()

  29. Using resources

  30. Supported resource types

  31. Referencing resources in Java

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

  33. Defining views and layouts through XML resources • 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.

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

  35. Externalizing values

  36. Providing animations

  37. Understanding the AndroidManifest file

More Related