0% found this document useful (0 votes)
41 views

Support Library and ActionBar

This document discusses making Android apps backward compatible to older platform versions. It introduces support libraries that allow using newer APIs on older platforms, such as Fragments and Loaders from the support library and the Action Bar from ActionBarSherlock. The document demonstrates how to detect the platform version at runtime and conditionally call newer APIs, as well as how to use the support libraries to backport features to older versions. It describes a code lab project that takes an app designed for Ice Cream Sandwich and extends it to also support Froyo using these techniques.

Uploaded by

SteinerOk
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views

Support Library and ActionBar

This document discusses making Android apps backward compatible to older platform versions. It introduces support libraries that allow using newer APIs on older platforms, such as Fragments and Loaders from the support library and the Action Bar from ActionBarSherlock. The document demonstrates how to detect the platform version at runtime and conditionally call newer APIs, as well as how to use the support libraries to backport features to older versions. It describes a code lab project that takes an app designed for Ice Cream Sandwich and extends it to also support Froyo using these techniques.

Uploaded by

SteinerOk
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

Support Library

& Action Bar


Backward Compatible Apps
Nick Butcher
Android Developer Advocate
Monday, July 2, 2012
Codelab Format
I will introduce a feature then you code it!
Follow the worksheet
Help each other!
2
Monday, July 2, 2012
worksheet: goo.gl/KOPHe
Monday, July 2, 2012
Why be backward compatible?
To use the latest APIs
4
Monday, July 2, 2012
New shiny
Take advantage of exciting new features e.g. Android Beam
Benet from platform enhancements
Conform to platform guidelines
Keep up with system apps
Compatibility Behaviour
5
Monday, July 2, 2012
Give users the best experience
- Bleeding edge often most vocal e.g. bloggers
Skate where the puck is going, not
where it has been
Wayne Gretzky
Monday, July 2, 2012
Add new feature, design for latest
Platform Distribution
7
Data collected during a 14-day period ending on June 1, 2012 - http://developer.android.com/about/dashboards/index.html
4
3
2.3
2.2
2.1
<2.1
Monday, July 2, 2012
Takes a while for users to update
Want to be compatible with as many devices as possible
To demonstrate this we will take an app designed for ICS and extend support back to Froyo
Honeypad
8
Monday, July 2, 2012
Honeypad
9
Monday, July 2, 2012
Honeypad
10
Monday, July 2, 2012
Worksheet: Steps 1 & 2
Monday, July 2, 2012
Import and setup the project and run the app
Minimum vs Target SDK
<uses-sdk
android:minSdkVersion=8
android:targetSdkVersion=15 />
12
This is a contract!
XML
Monday, July 2, 2012
Your responsibility to not use newer APIs on older platforms
Sounds like hard work... how to nd these...
Minimum vs Target SDK
13
Lint: NewApi check
Monday, July 2, 2012
DEMO
- Change API Level to 8
- Run Lint
- Open UiUtils - add @TargetApi(11) on setActivatedCompat
Worksheet: Step 3
Monday, July 2, 2012
...So now that we know how to detect errors, lets nd out how to manage them
API Gating
Detect what version you are running on
Progressively enhance functionality
15
Monday, July 2, 2012
API Gating
16
At run time
if (Build.VERSION.SDK_INT >=
Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
// awesome ICS functionality
}
JAVA
Monday, July 2, 2012
In-lined by the compiler so safe to execute on older platforms
Pre 2.0 classloader barfs - use Factory pattern
API Gating
17
Declaratively
<receiver
android:name="com.example.WidgetProvider"
android:enabled="@bool/postHoneycomb" ...>
XML
Monday, July 2, 2012
API Gating
18
Tell Lint that its safe to call this code
@TargetApi(11)
JAVA
Monday, July 2, 2012
Worksheet: Step 4
Monday, July 2, 2012
Use API gating on the StackView widget
... even better then graceful degradation is to use newer functionality on older releases.
Support Package
Static libraries to
- Make newer functionality available to older platforms
Fragments
Loaders
LruCache
*Compat classes
- O!er utility APIs beyond the framework
ViewPager
TaskStackBuilder
20
Monday, July 2, 2012
Were going to make use of 2 of these: Fragments & Loaders
Fragments
Modular sections of an Activity
Code once, layout multiple ways
Helpful for multiple screen sizes & rotation
Helpful subclasses:
- ListFragment
- DialogFragment
- PreferenceFragment
21
Monday, July 2, 2012
Fragments
22
HomeActivity
NoteListFragment NoteEditFragment
Monday, July 2, 2012
Fragments
23
HomeActivity
NoteListFragment
NoteEditFragment
EditNoteActivity
Monday, July 2, 2012
Fragments
Support package provides identical API except:
- Import android.support.v4.* classes
- Activities must extend FragmentActivity
- Call getSupportFragmentManager()
24
Monday, July 2, 2012
Loaders
Easy asynchronous data loading
Monitors data source & delivers updates
Handles con"guration changes e.g. rotation
CursorLoader is the best way to load data from a ContentProvider
Implement LoaderManager.LoaderCallbacks
Identical API:
- Import android.support.v4.* classes
25
Monday, July 2, 2012
Worksheet: Step 5
Monday, July 2, 2012
Add support library and change Fragment and Loaders to use its implementations
Action Bar
The action bar is arguably the most important structural
element of an Android app. It's a dedicated piece of real
estate at the top of each screen that is generally persistent
throughout the app.
27

Android Design
Monday, July 2, 2012
Action Bar
App Icon + Up Navigation
28
View Control
Action Buttons
Action Over!ow
Monday, July 2, 2012
Great!
Unfortunately not part of the Support Package
ActionBarSherlock
A community backport of the ICS Action Bar
- Thanks Jake Wharton!
Download and build as a library project
Aims to be API compatible with ICS except:
- Use SherlockActivity
- Use SherlockFragment
- Use Sherlock Menu, MenuItem, MenuIn#ater
- Set a Sherlock theme
29
Monday, July 2, 2012
Worksheet: Step 6
Monday, July 2, 2012
Add ABS and change Action Bar to use its implementation
Back Ported
31
HomeActivity
NoteEditFragment
EditNoteActivity
Monday, July 2, 2012
Thank You!
+Nick Butcher
Feedback: goo.gl/zr23y
Monday, July 2, 2012

You might also like