multi screenlab

23

Upload: ran-nachmany

Post on 17-May-2015

524 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Multi screenlab
Page 2: Multi screenlab
Page 3: Multi screenlab

But in tablet...

Page 4: Multi screenlab

Do I really need to write everything twice?

Page 5: Multi screenlab

NO!

Page 6: Multi screenlab

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.

Page 7: Multi screenlab

Fragments philosophy

Page 8: Multi screenlab

Creating a fragment

● Extend Fragment class.● Implement fragment Callbacks.

Page 9: Multi screenlab

Fragment life cycle

Page 10: Multi screenlab

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.

Page 11: Multi screenlab

Fragment Callbacks

● OnPause()– Called when the user leaves the fragment

● OnDestroy– Called when fragment is destroyed.

Page 12: Multi screenlab

OnCreateView example

Page 13: Multi screenlab

Adding fragment to activity / layout

Page 14: Multi screenlab

Adding fragment to activity

● Activity should extends fragmentActivity instead of Activity

Page 15: Multi screenlab

Communicating with the activity

● Fragment side:– GetActivity() - returns a reference to the containing

activity.

● Activity Side:

Page 16: Multi screenlab

Communicating with activity

● Often a fragment needs to share events with its activity. – For example: itemClicked.

● Fragment should define a callback interface

Page 17: Multi screenlab

Communicating with activity - cont

● Activity should implement that callback interface.

● Fragment holds reference to that listener and notifies it when needed.

Page 18: Multi screenlab

Checkpoint 0

● Checkout checkpoint0 branch from

https://github.com/RanNachmany/AndconLab● Adjust project to use actionBarSherlock● Run.

Page 19: Multi screenlab

Checkpoint 1

● Create a fragment that displays a list of all lectures.

● Change MainActivity to use the new fragment.● Do the same for SingleLectureActivity. ● Don't forget to implement callback interface.

Page 20: Multi screenlab

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

Page 21: Multi screenlab
Page 22: Multi screenlab

Changing fragments programatically

Page 23: Multi screenlab

Checkpoint 2

● Add main_activity layout under sw-600 qualifier.

● Adjust MainActivity code. ● Test on phone and tablet. ● Bring world peace.