Multi screenlab
Upcoming SlideShare
Loading in...5

Multi screenlab






Total Views
Views on SlideShare
Embed Views



0 Embeds 0

No embeds



Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
Post Comment
Edit your comment

Multi screenlab Multi screenlab Presentation Transcript

  • But in tablet...
  • Do I really need to write everything twice?
  • NO!
  • 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.
  • Fragments philosophy
  • Creating a fragment● Extend Fragment class.● Implement fragment Callbacks.
  • Fragment life cycle
  • 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.
  • Fragment Callbacks● OnPause() – Called when the user leaves the fragment● OnDestroy – Called when fragment is destroyed.
  • OnCreateView example
  • Adding fragment to activity / layout
  • Adding fragment to activity● Activity should extends fragmentActivity instead of Activity
  • Communicating with the activity● Fragment side: – GetActivity() - returns a reference to the containing activity.● Activity Side:
  • Communicating with activity● Often a fragment needs to share events with its activity. – For example: itemClicked.● Fragment should define a callback interface
  • Communicating with activity - cont● Activity should implement that callback interface.● Fragment holds reference to that listener and notifies it when needed.
  • Checkpoint 0● Checkout checkpoint0 branch from● Adjust project to use actionBarSherlock● Run.
  • 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.
  • 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
  • Changing fragments programatically
  • Checkpoint 2● Add main_activity layout under sw-600 qualifier.● Adjust MainActivity code.● Test on phone and tablet.● Bring world peace.