Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Multi screenlab


Published on

  • Be the first to comment

Multi screenlab

  1. 1. But in tablet...
  2. 2. Do I really need to write everything twice?
  3. 3. NO!
  4. 4. Fragments● “Part of the screen”● Introduced in Android 3.0 (API 11).● Available for older Android platforms via support package / library.● Allows us to change activity appearance at runtime without complex changes.
  5. 5. Fragments philosophy
  6. 6. Creating a fragment● Extend Fragment class.● Implement fragment Callbacks.
  7. 7. Fragment life cycle
  8. 8. Fragment Callbacks● OnCreate() – Called when first creating the fragment. – Initialize members here.● OnCreateView() – Called when the fragments needs to be displayed on screen. – Must return a view object.
  9. 9. Fragment Callbacks● OnPause() – Called when the user leaves the fragment● OnDestroy – Called when fragment is destroyed.
  10. 10. OnCreateView example
  11. 11. Adding fragment to activity / layout
  12. 12. Adding fragment to activity● Activity should extends fragmentActivity instead of Activity
  13. 13. Communicating with the activity● Fragment side: – GetActivity() - returns a reference to the containing activity.● Activity Side:
  14. 14. Communicating with activity● Often a fragment needs to share events with its activity. – For example: itemClicked.● Fragment should define a callback interface
  15. 15. Communicating with activity - cont● Activity should implement that callback interface.● Fragment holds reference to that listener and notifies it when needed.
  16. 16. Checkpoint 0● Checkout checkpoint0 branch from● Adjust project to use actionBarSherlock● Run.
  17. 17. Checkpoint 1● Create a fragment that displays a list of all lectures.● Change MainActivity to use the new fragment.● Do the same for SingleLectureActivity.● Dont forget to implement callback interface.
  18. 18. Supporting tablets● How activity A will change its layout from one fragment to two dynamically?● The answer: xml qualifiers. – We will create two xml layouts. One for phone, and one for tablet. – OS will choose the right layout during runtime. – Good qualifier: sw-600. Smallest width: 600. ● Located at res/layout-sw600dp
  19. 19. Changing fragments programatically
  20. 20. Checkpoint 2● Add main_activity layout under sw-600 qualifier.● Adjust MainActivity code.● Test on phone and tablet.● Bring world peace.