Top Banner
Supporting Multiple Android Device Definitions Chiu-Ki Chan @chiuki bit.ly/muland
82

Supporting Multiple Android Device Definitions

Apr 14, 2017

Download

Technology

Chiu-Ki Chan
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Supporting Multiple Android Device Definitions

Supporting Multiple Android Device Definitions

Chiu-Ki Chan@chiuki

bit.ly/muland

Page 2: Supporting Multiple Android Device Definitions

@chiuki@chiuki

So many devices!

Page 3: Supporting Multiple Android Device Definitions

@chiuki@chiuki

Infinite screen sizes!

Page 4: Supporting Multiple Android Device Definitions

@chiuki@chiuki

Responsive+

Progressive

Page 5: Supporting Multiple Android Device Definitions

Hello World

Page 6: Supporting Multiple Android Device Definitions

@chiuki

Page 7: Supporting Multiple Android Device Definitions

@chiuki

Page 8: Supporting Multiple Android Device Definitions

@chiuki

Page 9: Supporting Multiple Android Device Definitions

@chiuki

Page 10: Supporting Multiple Android Device Definitions

@chiuki

Page 11: Supporting Multiple Android Device Definitions

@chiuki

Page 12: Supporting Multiple Android Device Definitions

@chiuki

Page 13: Supporting Multiple Android Device Definitions

Declarative Layout

Page 14: Supporting Multiple Android Device Definitions

@chiuki

Page 15: Supporting Multiple Android Device Definitions

@chiuki

Page 16: Supporting Multiple Android Device Definitions

@chiuki

(x,y)

Page 17: Supporting Multiple Android Device Definitions

@chiuki

(x,y)

Page 18: Supporting Multiple Android Device Definitions

@chiuki

Center

Page 19: Supporting Multiple Android Device Definitions

@chiuki@chiuki

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

Page 20: Supporting Multiple Android Device Definitions

@chiuki

Page 21: Supporting Multiple Android Device Definitions

@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: Supporting Multiple Android Device Definitions

@chiuki

Page 23: Supporting Multiple Android Device Definitions

@chiuki

Divider

Page 24: Supporting Multiple Android Device Definitions

@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: Supporting Multiple Android Device Definitions

@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: Supporting Multiple Android Device Definitions

@chiuki

dp

Page 27: Supporting Multiple Android Device Definitions

@chiuki

dpDensity-independent pixel

Page 28: Supporting Multiple Android Device Definitions

@chiuki

Page 29: Supporting Multiple Android Device Definitions

@chiuki

Page 30: Supporting Multiple Android Device Definitions

@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: Supporting Multiple Android Device Definitions

@chiuki

Page 32: Supporting Multiple Android Device Definitions

Resource Folders

Page 33: Supporting Multiple Android Device Definitions

@chiuki@chiuki

Resource Folders

Type Variation

layoutvalues

drawablemenu

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

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

Page 34: Supporting Multiple Android Device Definitions

@chiuki@chiuki

Resource Folders

Type Variation

layoutvalues

drawablemenu

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

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

res/layout-land/activity_main.xml

Page 35: Supporting Multiple Android Device Definitions

@chiuki@chiuki

Resource Folders

Type Variation

layoutvalues

drawablemenu

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

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

res/drawable-hdpi/ic_launcher.png

Page 36: Supporting Multiple Android Device Definitions

@chiuki@chiuki

Resource Folders

Type Variation

layoutvalues

drawablemenu

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

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

res/drawable-large-land/splash.png

Page 37: Supporting Multiple Android Device Definitions

@chiukires/drawable-large-land/splash.png

Page 38: Supporting Multiple Android Device Definitions

@chiukires/drawable/splash.xml

Page 39: Supporting Multiple Android Device Definitions

@chiukires/drawable/splash.xml

Page 40: Supporting Multiple Android Device Definitions

XML drawable

Page 41: Supporting Multiple Android Device Definitions

@chiuki@chiuki

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

Page 42: Supporting Multiple Android Device Definitions

@chiuki@chiuki

Gradient

Page 43: Supporting Multiple Android Device Definitions

@chiuki@chiuki

Gradient

Page 44: Supporting Multiple Android Device Definitions

@chiuki@chiuki

Pattern

Page 45: Supporting Multiple Android Device Definitions

@chiuki@chiuki

Tiles

Page 46: Supporting Multiple Android Device Definitions

@chiuki@chiuki

Tiles

Page 47: Supporting Multiple Android Device Definitions

@chiuki@chiuki

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

res/drawable/background.xml

Page 48: Supporting Multiple Android Device Definitions

@chiuki@chiuki

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

res/drawable/background.xml

tile.png

Tiling background

Page 49: Supporting Multiple Android Device Definitions

@chiuki@chiuki

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

res/layout/activity_main.xml

res/drawable/background.xml

Tiling background

Page 50: Supporting Multiple Android Device Definitions

@chiuki@chiuki

Tile modeclampReplicates the edge color

repeat Repeats the bitmap in both direction

mirror Repeats with alternating mirror images

Page 51: Supporting Multiple Android Device Definitions

@chiuki@chiuki

GridView

Page 52: Supporting Multiple Android Device Definitions

@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: Supporting Multiple Android Device Definitions

@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: Supporting Multiple Android Device Definitions

@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: Supporting Multiple Android Device Definitions

@chiuki@chiuki

<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

values/dimen.xml

Page 56: Supporting Multiple Android Device Definitions

@chiuki@chiuki

values-large/dimen.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 57: Supporting Multiple Android Device Definitions

@chiuki@chiuki

7-inch

Page 58: Supporting Multiple Android Device Definitions

@chiuki@chiuki

Phone

Page 59: Supporting Multiple Android Device Definitions

@chiuki@chiuki

Fragments

github.com/codepath/android-master-detail-demo

Page 60: Supporting Multiple Android Device Definitions

Progressive

Page 61: Supporting Multiple Android Device Definitions

@chiuki@chiuki

Resource Folders

Type Variation

layoutvalues

drawablemenu

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

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

Page 62: Supporting Multiple Android Device Definitions

@chiuki@chiuki

Resource Folders

Type Variation

layoutvalues

drawablemenu

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

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

Page 63: Supporting Multiple Android Device Definitions

@chiuki@chiuki

Ripples

Page 64: Supporting Multiple Android Device Definitions

@chiuki@chiuki

Ripples

Page 65: Supporting Multiple Android Device Definitions

@chiuki@chiuki

drawable-v21<ripple android:color="@color/colorAccent"> <item> <shape android:shape="rectangle"> <solid android:color="@color/colorPrimary" /> </shape> </item></ripple>

res/drawable-v21/background.xml

Page 66: Supporting Multiple Android Device Definitions

@chiuki@chiuki

drawable as default<selector> <item android:state_pressed="true" android:drawable="@color/colorAccent" /> <item android:drawable="@color/colorPrimary" /></selector>

res/drawable/background.xml

Page 67: Supporting Multiple Android Device Definitions

@chiuki@chiuki

android:background<TextView android:layout_width="match_parent" android:layout_height="match_parent" android:background="drawable/background" android:text="@string/click_me" />

Page 68: Supporting Multiple Android Device Definitions

@chiuki@chiuki

Progressive

Page 69: Supporting Multiple Android Device Definitions

@chiuki@chiuki

API level

Page 70: Supporting Multiple Android Device Definitions

@chiuki@chiuki

Detect API level

android.os.Build.VERSION.SDK_INT

Page 71: Supporting Multiple Android Device Definitions

@chiuki@chiuki

// Default valueint heapSize = 16;

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

// Progressive enhancement for newer devicesif (sdk >= Build.VERSION_CODES_ECLAIR) { heapSize = getHeapSize(this);}

Progressive

Page 72: Supporting Multiple Android Device Definitions

@chiuki@chiuki

getHeapSize

public int getHeapSize() { ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); return activityManager.getMemoryClass();}

Page 73: Supporting Multiple Android Device Definitions

@chiuki@chiuki

Ship new functionality with your app!

Support Library

Page 74: Supporting Multiple Android Device Definitions

@chiuki

Support LibraryFragment

TaskStackBuilderLruCache

ViewPager

DrawerLayoutSlidingPaneLayout

Loader

AppCompat

VectorDrawableRecyclerView

Page 75: Supporting Multiple Android Device Definitions

@chiuki

AppCompat

Material Design

Page 76: Supporting Multiple Android Device Definitions

@chiuki

Support library only

RecyclerView

Page 77: Supporting Multiple Android Device Definitions

Summary

Page 78: Supporting Multiple Android Device Definitions

@chiuki@chiuki

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

Progressive functionalityResource foldersVersion checkSupport library

Slides

bit.ly/muland

Page 79: Supporting Multiple Android Device Definitions

@chiuki@chiuki

Thank you!Learn moredeveloper.android.com/training/multiscreenis.gd/FluidAndroidLayoutsis.gd/BeautifulAndroidpluralsight.com/authors/chiuki-chan

Stay in touchtinyletter.com/sqislandblog.sqisland.comtwitter.com/chiuki

Slides

bit.ly/muland

Page 80: Supporting Multiple Android Device Definitions

@chiuki@chiuki

Page 81: Supporting Multiple Android Device Definitions

@chiuki@chiuki

360|AnDev Core Track• Activities in the Wild: Exploring the Activity

Lifecycle• Fragments: What are they good for• Async Task, Threads, Pools and Executors• Measure, Layout, Draw, Repeat: Custom Views

and ViewGroups• A Practical Guide to Material Design

implementation for Android Developers• Libraries I wish I knew about when I started• Intro to iOS for Android developers

Page 82: Supporting Multiple Android Device Definitions

@chiuki@chiuki

Discount Code: DroidCommunity