Transcript
Page 1: Bust the Android Fragmentation Myth

Bust the Android Fragmentation Myth

Chiu-Ki Chan@chiuki

Page 2: Bust the Android Fragmentation Myth

@chiuki@chiuki

So many devices!

Page 3: Bust the Android Fragmentation Myth

@chiuki@chiuki

Infinite screen sizes!

Page 4: Bust the Android Fragmentation Myth

@chiuki@chiuki

Responsive+

Progressive

Page 5: Bust the Android Fragmentation Myth

Hello World

Page 6: Bust the Android Fragmentation Myth

@chiuki

Page 7: Bust the Android Fragmentation Myth

@chiuki

Page 8: Bust the Android Fragmentation Myth

@chiuki

Page 9: Bust the Android Fragmentation Myth

@chiuki

Page 10: Bust the Android Fragmentation Myth

@chiuki

Page 11: Bust the Android Fragmentation Myth

@chiuki

Page 12: Bust the Android Fragmentation Myth

@chiuki

Page 13: Bust the Android Fragmentation Myth

Declarative Layout

Page 14: Bust the Android Fragmentation Myth

@chiuki

Page 15: Bust the Android Fragmentation Myth

@chiuki

Page 16: Bust the Android Fragmentation Myth

@chiuki

(x,y)

Page 17: Bust the Android Fragmentation Myth

@chiuki

(x,y)

Page 18: Bust the Android Fragmentation Myth

@chiuki

Center

Page 19: Bust the Android Fragmentation Myth

@chiuki@chiuki

Center<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="@string/start" />

Page 20: Bust the Android Fragmentation Myth

@chiuki

Page 21: Bust the Android Fragmentation Myth

@chiuki@chiuki

Proportional Layout<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ImageView android:id="@+id/image1" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" /> <ImageView android:id="@+id/image2" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" /></LinearLayout>

Page 22: Bust the Android Fragmentation Myth

@chiuki

Page 23: Bust the Android Fragmentation Myth

@chiuki

Divider

Page 24: Bust the Android Fragmentation Myth

@chiuki@chiuki

Divider<LinearLayout> <ImageView android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" /> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="@android:color/black" /> <ImageView android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" /></LinearLayout>

Page 25: Bust the Android Fragmentation Myth

@chiuki@chiuki

Divider<LinearLayout> <ImageView android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" /> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="@android:color/black" /> <ImageView android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" /></LinearLayout>

Page 26: Bust the Android Fragmentation Myth

@chiuki

dp

Page 27: Bust the Android Fragmentation Myth

@chiuki

dpDensity-independent pixel

Page 28: Bust the Android Fragmentation Myth

@chiuki

Page 29: Bust the Android Fragmentation Myth

@chiuki

Page 30: Bust the Android Fragmentation Myth

@chiuki@chiuki

res/layout-land<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <ImageView android:id="@+id/image1" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" /> <ImageView android:id="@+id/image2" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" /></LinearLayout>

Page 31: Bust the Android Fragmentation Myth

@chiuki

Page 32: Bust the Android Fragmentation Myth

Resource Folders

Page 33: Bust the Android Fragmentation Myth

@chiuki@chiuki

Resource Folders

Type Variation

layoutvalues

drawablemenu

Language & Region: en, fr, fr-rCAScreen size: small, large, xlargeScreen orientation: port, landScreen density: ldpi, mdpi, hdpi, xhdpi, nodpi, tvdpiPlatform version: v4, v11, v14UI mode: car, desk, television, appliance

http://developer.android.com/guide/topics/resources/providing-resources.html

Page 34: Bust the Android Fragmentation Myth

@chiuki@chiuki

Resource Folders

Type Variation

layoutvalues

drawablemenu

Language & Region: en, fr, fr-rCAScreen size: small, large, xlargeScreen orientation: port, landScreen density: ldpi, mdpi, hdpi, xhdpi, nodpi, tvdpiPlatform version: v4, v11, v14UI mode: car, desk, television, appliance

http://developer.android.com/guide/topics/resources/providing-resources.html

res/layout-land/activity_main.xml

Page 35: Bust the Android Fragmentation Myth

@chiuki@chiuki

Resource Folders

Type Variation

layoutvalues

drawablemenu

Language & Region: en, fr, fr-rCAScreen size: small, large, xlargeScreen orientation: port, landScreen density: ldpi, mdpi, hdpi, xhdpi, nodpi, tvdpiPlatform version: v4, v11, v14UI mode: car, desk, television, appliance

http://developer.android.com/guide/topics/resources/providing-resources.html

res/drawable-hdpi/ic_launcher.png

Page 36: Bust the Android Fragmentation Myth

@chiuki@chiuki

Resource Folders

Type Variation

layoutvalues

drawablemenu

Language & Region: en, fr, fr-rCAScreen size: small, large, xlargeScreen orientation: port, landScreen density: ldpi, mdpi, hdpi, xhdpi, nodpi, tvdpiPlatform version: v4, v11, v14UI mode: car, desk, television, appliance

http://developer.android.com/guide/topics/resources/providing-resources.html

res/drawable-large-land/splash.png

Page 37: Bust the Android Fragmentation Myth

@chiuki

res/drawable-large-land/splash.png

Page 38: Bust the Android Fragmentation Myth

@chiuki

res/drawable/splash.xml

Page 39: Bust the Android Fragmentation Myth

@chiuki

res/drawable/splash.xml

Page 40: Bust the Android Fragmentation Myth

XML drawable

Page 41: Bust the Android Fragmentation Myth

@chiuki@chiuki

XML shape

<shape android:shape="rectangle" > <gradient android:startColor="#063" android:endColor="#030" android:angle="270" /></shape>

Page 42: Bust the Android Fragmentation Myth

@chiuki@chiuki

Gradient

Page 43: Bust the Android Fragmentation Myth

@chiuki@chiuki

Gradient

Page 44: Bust the Android Fragmentation Myth

@chiuki@chiuki

Pattern

Page 45: Bust the Android Fragmentation Myth

@chiuki@chiuki

Tiles

Page 46: Bust the Android Fragmentation Myth

@chiuki@chiuki

Tiles

Page 47: Bust the Android Fragmentation Myth

@chiuki@chiuki

Tiling background<bitmap android:src="@drawable/tile" android:tileMode="repeat" android:dither="true" />

res/drawable/background.xml

Page 48: Bust the Android Fragmentation Myth

@chiuki@chiuki

Tiling background<bitmap android:src="@drawable/tile" android:tileMode="repeat" android:dither="true" />

res/drawable/background.xml

tile.png

Page 49: Bust the Android Fragmentation Myth

@chiuki@chiuki

Tiling background<bitmap android:src="@drawable/tile" android:tileMode="repeat" android:dither="true" />

res/drawable/background.xml

<View android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/background" />

res/layout/activity_main.xml

Page 50: Bust the Android Fragmentation Myth

@chiuki@chiuki

Tile mode

clampReplicates the edge color

repeatRepeats the bitmap in both direction

mirrorRepeats with alternating mirror images

Page 51: Bust the Android Fragmentation Myth

@chiuki@chiuki

GridView

Page 52: Bust the Android Fragmentation Myth

@chiuki@chiuki

Auto fit columns<GridView android:layout_width="match_parent" android:layout_height="match_parent" android:numColumns="auto_fit" android:columnWidth="@dimen/workbook_column_width" android:padding="@dimen/workbook_padding" android:horizontalSpacing="@dimen/workbook_spacing" android:verticalSpacing="@dimen/workbook_spacing" android:scrollbarStyle="outsideOverlay" />

Page 53: Bust the Android Fragmentation Myth

@chiuki@chiuki

Auto fit columns<GridView android:layout_width="match_parent" android:layout_height="match_parent" android:numColumns="auto_fit" android:columnWidth="@dimen/workbook_column_width" android:padding="@dimen/workbook_padding" android:horizontalSpacing="@dimen/workbook_spacing" android:verticalSpacing="@dimen/workbook_spacing" android:scrollbarStyle="outsideOverlay" />

Page 54: Bust the Android Fragmentation Myth

@chiuki@chiuki

Auto fit columns<GridView android:layout_width="match_parent" android:layout_height="match_parent" android:numColumns="auto_fit" android:columnWidth="@dimen/workbook_column_width" android:padding="@dimen/workbook_padding" android:horizontalSpacing="@dimen/workbook_spacing" android:verticalSpacing="@dimen/workbook_spacing" android:scrollbarStyle="outsideOverlay" />

Page 55: Bust the Android Fragmentation Myth

@chiuki@chiuki

dimen<resources> <dimen name="workbook_padding">8dp</dimen> <dimen name="workbook_spacing">12dp</dimen> <dimen name="workbook_column_width">120dp</dimen><resources>

res/values/dimens.xml

<resources> <dimen name="workbook_padding">32dp</dimen> <dimen name="workbook_spacing">18dp</dimen> <dimen name="workbook_column_width">180dp</dimen><resources>

res/values-large/dimens.xml

Page 56: Bust the Android Fragmentation Myth

@chiuki@chiuki

7-inch

Page 57: Bust the Android Fragmentation Myth

@chiuki@chiuki

Phone

Page 58: Bust the Android Fragmentation Myth

Progressive

Page 59: Bust the Android Fragmentation Myth

@chiuki@chiuki

Resource Folders

Type Variation

layoutvalues

drawablemenu

Language & Region: en, fr, fr-rCAScreen size: small, large, xlargeScreen orientation: port, landScreen density: ldpi, mdpi, hdpi, xhdpi, nodpi, tvdpiPlatform version: v4, v11, v14UI mode: car, desk, television, appliance

http://developer.android.com/guide/topics/resources/providing-resources.html

Page 60: Bust the Android Fragmentation Myth

@chiuki@chiuki

Resource Folders

Type Variation

layoutvalues

drawablemenu

Language & Region: en, fr, fr-rCAScreen size: small, large, xlargeScreen orientation: port, landScreen density: ldpi, mdpi, hdpi, xhdpi, nodpi, tvdpiPlatform version: v4, v11, v14UI mode: car, desk, television, appliance

http://developer.android.com/guide/topics/resources/providing-resources.html

Page 61: Bust the Android Fragmentation Myth

@chiuki@chiuki

Toggle button

Page 62: Bust the Android Fragmentation Myth

@chiuki@chiuki

layout-v14

<ToggleButton android:layout_width="wrap_content" android:layout_height="wrap_content" />

res/layout/compound_button.xml

<Switch android:layout_width="wrap_content" android:layout_height="wrap_content" />

res/layout-v14/compound_button.xml

Page 63: Bust the Android Fragmentation Myth

@chiuki@chiuki

Include<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <include android:id="@+id/turbo" layout="@layout/compound_button" /> <include android:id="@+id/reset" layout="@layout/compound_button" /></LinearLayout>

Page 64: Bust the Android Fragmentation Myth

@chiuki@chiuki

Cast to superclassCompoundButton turbo = (CompoundButton) findViewById(R.id.turbo);if (!turbo.isChecked()) { // Slow down the computer}

Page 65: Bust the Android Fragmentation Myth

@chiuki@chiuki

API level

Page 66: Bust the Android Fragmentation Myth

@chiuki@chiuki

Detect API level

android.os.Build.VERSION.SDKString, available from API level 1

android.os.Build.VERSION.SDK_INTint, available from API level 4

Page 67: Bust the Android Fragmentation Myth

@chiuki@chiuki

Progressive

// Default valueint heapSize = 16;

// Detect OS versionint sdk = Integer.parseInt(Build.VERSION.SDK);

// Progressive enhancement for newer devicesif (sdk >= Build.VERSION_CODES_ECLAIR) { // Static helper for API level 4 and below heapSize = HeapSizeGetter.getHeapSize(this);}

Page 68: Bust the Android Fragmentation Myth

@chiuki@chiuki

Static helperpublic abstract class HeapSizeGetter { public static int getHeapSize(Context context) { ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); return activityManager.getMemoryClass(); }}

Page 69: Bust the Android Fragmentation Myth

@chiuki@chiuki

Progressive

// Default valueint heapSize = 16;

// Detect OS versionint sdk = Integer.parseInt(Build.VERSION.SDK);

// Progressive enhancement for newer devicesif (sdk >= Build.VERSION_CODES_ECLAIR) { // Static helper for API level 4 and below heapSize = HeapSizeGetter.getHeapSize(this);}

Page 70: Bust the Android Fragmentation Myth

@chiuki@chiuki

Support libraries

Ship new functionality with your app!

Page 71: Bust the Android Fragmentation Myth

@chiuki

Official Support Library

Fragment

TaskStackBuilder

LruCache

ViewPager

DrawerLayoutSlidingPaneLayout

Loader

Page 72: Bust the Android Fragmentation Myth

@chiuki

Community Support Libraries

• Action Bar Sherlock

• View Pager Indicator

• Nine Old Android

• Holo Everywhere

Page 73: Bust the Android Fragmentation Myth

@chiuki@chiuki

View Pager Indicator

Page 74: Bust the Android Fragmentation Myth

Summary

Page 75: Bust the Android Fragmentation Myth

@chiuki@chiuki

SummaryResponsive layoutDeclarative layoutDensity-independent pixels (dp)Resource foldersXML drawables

Progressive functionalityResource foldersVersion checkSupport libraries

Page 76: Bust the Android Fragmentation Myth

@chiuki@chiuki

Thank you!

Learn morehttp://pluralsight.com/courses/android-layout-fundamentalshttp://is.gd/FluidAndroidLayoutshttp://is.gd/BeautifulAndroid

Stay in touchhttp://eepurl.com/lR5uDhttp://blog.sqisland.comhttp://twitter.com/chiuki


Top Related