85
www.immobilienscout24.de www.immobilienscout24.de Best Practices to develop for different Android Device Classifications Droidcon | Amsterdam | 26.11.2013 | Hasan Hosgel

Droidcon nl 2013 best practices to develop for different android device classifications

Embed Size (px)

Citation preview

Page 1: Droidcon nl 2013 best practices to develop for different android device classifications

www.immobilienscout24.dewww.immobilienscout24.de

Best Practices to develop for different Android Device Classifications

Droidcon | Amsterdam | 26.11.2013 | Hasan Hosgel

Page 2: Droidcon nl 2013 best practices to develop for different android device classifications

Droidcon NL 2013 | Develop for different device classifications | Hasan Hosgel

About me

+Hasan Hosgel

Twitter: @alosdevGithub: alosdevSlideshare: hosgel

CO-Organizer @ GDG Android in Berlin& community events

developer @ ImmobilienScout24

Page 3: Droidcon nl 2013 best practices to develop for different android device classifications

Droidcon NL 2013 | Develop for different device classifications | Hasan Hosgel

About ImmobilienScout24

Germany’s largest real estate listing company.

> 10 Mio. Monthly unique users> 1.5 Mio. real estates> 300 Mio. detail views> 1500 Servers~ 6.5 Mio. App downloads

~ 3 Mio. Android~ 50% mobile traffic

Page 4: Droidcon nl 2013 best practices to develop for different android device classifications

Fragmentation

Droidcon NL 2013 | Develop for different device classifications | Hasan Hosgel

Page 5: Droidcon nl 2013 best practices to develop for different android device classifications

Fragmentation

Droidcon NL 2013 | Develop for different device classifications | Hasan Hosgel

> 4400 Android Devices

Page 6: Droidcon nl 2013 best practices to develop for different android device classifications

Here comes The Nightmare

Droidcon NL 2013 | Develop for different device classifications | Hasan HosgelImage source:http://www.flickr.com/photos/boogeyman13/4553188509/

Page 7: Droidcon nl 2013 best practices to develop for different android device classifications

Here comes The Nightmare

Droidcon NL 2013 | Develop for different device classifications | Hasan Hosgel

For developers

Image source:http://www.flickr.com/photos/boogeyman13/4553188509/

Page 8: Droidcon nl 2013 best practices to develop for different android device classifications

Let’s build the fundament

Droidcon NL 2013 | Develop for different device classifications | Hasan HosgelImage source:http://www.flickr.com/photos/hertenberger/1434191066/

Page 9: Droidcon nl 2013 best practices to develop for different android device classifications

Droidcon NL 2013 | Develop for different device classifications | Hasan Hosgel

Device Classification

Images sources:https://play.google.com/store/devices

Page 10: Droidcon nl 2013 best practices to develop for different android device classifications

Droidcon NL 2013 | Develop for different device classifications | Hasan Hosgel

Device Classification

Images sources:https://play.google.com/store/deviceshttp://www.htc.com/de/

Page 11: Droidcon nl 2013 best practices to develop for different android device classifications

Droidcon NL 2013 | Develop for different device classifications | Hasan Hosgel

Device Classification

Images sources:http://www.sony.de/hub/google-tv

Page 12: Droidcon nl 2013 best practices to develop for different android device classifications

Device Classification

Droidcon NL 2013 | Develop for different device classifications | Hasan HosgelImages Sourceshttps://developer.ford.com/

Page 13: Droidcon nl 2013 best practices to develop for different android device classifications

Droidcon NL 2013 | Develop for different device classifications | Hasan Hosgel

Wearables

Page 14: Droidcon nl 2013 best practices to develop for different android device classifications

Droidcon NL 2013 | Develop for different device classifications | Hasan Hosgel

Glass

Page 15: Droidcon nl 2013 best practices to develop for different android device classifications

Droidcon NL 2013 | Develop for different device classifications | Hasan Hosgel

Hard Work Ahead

Source: http://www.flickr.com/photos/16210667@N02/9172895225

Page 16: Droidcon nl 2013 best practices to develop for different android device classifications

Resource Folders

Droidcon NL 2013 | Develop for different device classifications | Hasan Hosgel

You can use several qualifiers in the resource folders name for serving the best matching resource.

Page 17: Droidcon nl 2013 best practices to develop for different android device classifications

Droidcon NL 2013 | Develop for different device classifications | Hasan Hosgel

Qualifiers

●Language (-en)●Language & Region (-en-rUS)●Smallest Width (–sw600dp)●Screensize (-small, -normal, -large)●Screen Orientation (-port, -land)●Screen Pixel Densitiy (-hdpi, -xhdpi, -xxhdpi)●Platform Version (-v11, -v13)

Page 18: Droidcon nl 2013 best practices to develop for different android device classifications

Droidcon NL 2013 | Develop for different device classifications | Hasan Hosgel

Best Matching Resources Win

1.res/values/strings.xml2.res/values-en-rUS/strings.xml3.res/values-large/strings.xml4.res/values-sw600dp/strings.xml

Page 19: Droidcon nl 2013 best practices to develop for different android device classifications

Droidcon NL 2013 | Develop for different device classifications | Hasan Hosgel

Best Matching Resources Win

1.res/values/strings.xml2.res/values-en-rUS/strings.xml3.res/values-large/strings.xml4.res/values-sw600dp/strings.xml

The order of the qualifiers in the previous slides gives the ranking, if two resources have the same matching number of qualifiers.

Page 20: Droidcon nl 2013 best practices to develop for different android device classifications

Droidcon NL 2013 | Develop for different device classifications | Hasan Hosgel

Images Resources

• Use the different qualifiers for the screen pixel density (mdpi, hdpi, etc.)

• If you are forced to use text on images use language and region (en, es-rUS, en-rUS, etc.)

• Better approach is to use 9-patch drawables, which stretches automatically depending on the content inside.

• You must provide different launcher icons for Froyo, Honeycomb and above? Use the platform version. (v4, v11, v14)

Page 21: Droidcon nl 2013 best practices to develop for different android device classifications

Droidcon NL 2013 | Develop for different device classifications | Hasan Hosgel

Classifications for Layouts

Platform version at least v13 (Honeycomb MR2)

Page 22: Droidcon nl 2013 best practices to develop for different android device classifications

Droidcon NL 2013 | Develop for different device classifications | Hasan Hosgel

Classifications for Layouts

Platform version at least v13 (Honeycomb MR2)

project-folder/res/layout/

Page 23: Droidcon nl 2013 best practices to develop for different android device classifications

Droidcon NL 2013 | Develop for different device classifications | Hasan Hosgel

Classifications for Layouts

Platform version at least v13 (Honeycomb MR2)

project-folder/res/layout/ small phones

Page 24: Droidcon nl 2013 best practices to develop for different android device classifications

Droidcon NL 2013 | Develop for different device classifications | Hasan Hosgel

Classifications for Layouts

Platform version at least v13 (Honeycomb MR2)

project-folder/res/layout/ small phoneslayout-sw320dp/ other phones

Page 25: Droidcon nl 2013 best practices to develop for different android device classifications

Droidcon NL 2013 | Develop for different device classifications | Hasan Hosgel

Classifications for Layouts

Platform version at least v13 (Honeycomb MR2)

project-folder/res/layout/ small phoneslayout-sw320dp/ other phoneslayout-sw600dp/ tablets 7”

Page 26: Droidcon nl 2013 best practices to develop for different android device classifications

Droidcon NL 2013 | Develop for different device classifications | Hasan Hosgel

Classifications for Layouts

Platform version at least v13 (Honeycomb MR2)

project-folder/res/layout/ small phoneslayout-sw320dp/ other phoneslayout-sw600dp/ tablets 7”layout-sw720dp/ tablets 10”

* You should also use the orientation qualifier

Page 27: Droidcon nl 2013 best practices to develop for different android device classifications

Droidcon NL 2013 | Develop for different device classifications | Hasan Hosgel

Platform version at lower v11

project-folder/res/layout/ phones layout-sw320dp/ other phoneslayout-sw600dp/ tablets 7”layout-sw720dp/ tablets 10”

Page 28: Droidcon nl 2013 best practices to develop for different android device classifications

Droidcon NL 2013 | Develop for different device classifications | Hasan Hosgel

Platform version at lower v11

project-folder/res/layout/ phoneslayout-v11/ tablets 10”

layout-sw320dp/ other phoneslayout-sw600dp/ tablets 7”layout-sw720dp/ tablets 10”

Page 29: Droidcon nl 2013 best practices to develop for different android device classifications

Droidcon NL 2013 | Develop for different device classifications | Hasan Hosgel

Platform version at lower v11

project-folder/res/layout/ phoneslayout-v11/ tablets 10”layout-v13/ small phoneslayout-sw320dp/ other phoneslayout-sw600dp/ tablets 7”layout-sw720dp/ tablets 10”

Page 30: Droidcon nl 2013 best practices to develop for different android device classifications

Droidcon NL 2013 | Develop for different device classifications | Hasan Hosgel

Hint

The smallest width qualifier gets automatically platform version ”-v13” through the packager, for avoiding problems with the number of matching qualifiers.

Page 31: Droidcon nl 2013 best practices to develop for different android device classifications

Droidcon NL 2013 | Develop for different device classifications | Hasan Hosgel

Howto Classify In Code

• Read configuration from the device

Page 32: Droidcon nl 2013 best practices to develop for different android device classifications

Droidcon NL 2013 | Develop for different device classifications | Hasan Hosgel

Howto Classify In Code

• Read configuration from the device• Smarter approach is to use boolean

resources

Page 33: Droidcon nl 2013 best practices to develop for different android device classifications

Droidcon NL 2013 | Develop for different device classifications | Hasan Hosgel

project-folder/res/values/layouts.xml<resources>

</resources>

Page 34: Droidcon nl 2013 best practices to develop for different android device classifications

Droidcon NL 2013 | Develop for different device classifications | Hasan Hosgel

project-folder/res/values/layouts.xml<resources>

<bool > </bool>

</resources>

Page 35: Droidcon nl 2013 best practices to develop for different android device classifications

Droidcon NL 2013 | Develop for different device classifications | Hasan Hosgel

project-folder/res/values/layouts.xml<resources>

<bool name="is_phone_small"> </bool>

</resources>

Page 36: Droidcon nl 2013 best practices to develop for different android device classifications

Droidcon NL 2013 | Develop for different device classifications | Hasan Hosgel

project-folder/res/values/layouts.xml<resources>

<bool name="is_phone_small">true</bool>

</resources>

Page 37: Droidcon nl 2013 best practices to develop for different android device classifications

Droidcon NL 2013 | Develop for different device classifications | Hasan Hosgel

project-folder/res/values/layouts.xml<resources>

<bool name="is_phone_small">true</bool>

<bool name="is_phone_other">false</bool>

<bool name="is_tablet_7">false</bool><bool name="is_tablet_10">false</bool>

</resources>

Page 38: Droidcon nl 2013 best practices to develop for different android device classifications

Droidcon NL 2013 | Develop for different device classifications | Hasan Hosgel

project-folder/res/values/layouts.xml<resources><bool name="is_phone_small">true</bool><bool name="is_phone_other">false</bool><bool name="is_tablet_7">false</bool><bool name="is_tablet_10">false</bool></resources>

Usage in code: getResources().getBoolean(R.bool.is_phone_small)

Page 39: Droidcon nl 2013 best practices to develop for different android device classifications

Current Layout File Structure

Droidcon NL 2013 | Develop for different device classifications | Hasan Hosgel

project-folder/res/layout/main.xmllayout-v11/main.xmllayout-v13/main.xmllayout-sw320dp/main.xmllayout-sw600dp/main.xmllayout-sw720dp/main.xml

Page 40: Droidcon nl 2013 best practices to develop for different android device classifications

Current Layout File Structure

Droidcon NL 2013 | Develop for different device classifications | Hasan Hosgel

project-folder/res/layout/main.xmllayout-v11/main.xmllayout-v13/main.xmllayout-sw320dp/main.xmllayout-sw600dp/main.xmllayout-sw720dp/main.xml

Fixing one bug in the 10“ layout has to be done in two files.

Page 41: Droidcon nl 2013 best practices to develop for different android device classifications

Current Layout File Structure

Droidcon NL 2013 | Develop for different device classifications | Hasan Hosgel

project-folder/res/layout/main.xmllayout-v11/main.xmllayout-v13/main.xmllayout-sw320dp/main.xmllayout-sw600dp/main.xmllayout-sw720dp/main.xml

Fixing one bug in the 10“ layout has to be done in two files. error prone

Page 42: Droidcon nl 2013 best practices to develop for different android device classifications

Droidcon NL 2013 | Develop for different device classifications | Hasan Hosgel

Resource Alias

Put your layout files in the default folder.

project-folder/res/layout/main_phone_small.xmllayout/main_phone_other.xmllayout/main_tablet_7.xmllayout/main_tablet_10.xml

Page 43: Droidcon nl 2013 best practices to develop for different android device classifications

Droidcon NL 2013 | Develop for different device classifications | Hasan Hosgel

Resource Alias

2. Create an item with the needed classification in the previously defined values folder.

project-folder/res/values-sw720dp/layouts.xml<resources>

</resources>

Page 44: Droidcon nl 2013 best practices to develop for different android device classifications

Droidcon NL 2013 | Develop for different device classifications | Hasan Hosgel

Resource Alias

2. Create an item with the needed classification in the previously defined values folder.

project-folder/res/values-sw720dp/layouts.xml<resources>

<item >

</item></resources>

Page 45: Droidcon nl 2013 best practices to develop for different android device classifications

Droidcon NL 2013 | Develop for different device classifications | Hasan Hosgel

Resource Alias

2. Create an item with the needed classification in the previously defined values folder.

project-folder/res/values-sw720dp/layouts.xml<resources>

<item name="main" >

</item></resources>

Page 46: Droidcon nl 2013 best practices to develop for different android device classifications

Droidcon NL 2013 | Develop for different device classifications | Hasan Hosgel

Resource Alias

2. Create an item with the needed classification in the previously defined values folder.

project-folder/res/values-sw720dp/layouts.xml<resources>

<item name="main" type="layout">

</item></resources>

Page 47: Droidcon nl 2013 best practices to develop for different android device classifications

Droidcon NL 2013 | Develop for different device classifications | Hasan Hosgel

Resource Alias

2. Create an item with the needed classification in the previously defined values folder.

project-folder/res/values-sw720dp/layouts.xml<resources>

<item name="main" type="layout">

@layout/main_tablet_10.xml</item></resources>

Page 48: Droidcon nl 2013 best practices to develop for different android device classifications

Droidcon NL 2013 | Develop for different device classifications | Hasan Hosgel

Sample Screen

Page 49: Droidcon nl 2013 best practices to develop for different android device classifications

Droidcon NL 2013 | Develop for different device classifications | Hasan Hosgel

Sample Screen

Page 50: Droidcon nl 2013 best practices to develop for different android device classifications

Droidcon NL 2013 | Develop for different device classifications | Hasan Hosgel

Sample Screen

Use <includes>

Page 51: Droidcon nl 2013 best practices to develop for different android device classifications

Droidcon NL 2013 | Develop for different device classifications | Hasan Hosgel

Usage Includes

<LinearLayout … >… …

</LinearLayout>

Page 52: Droidcon nl 2013 best practices to develop for different android device classifications

Droidcon NL 2013 | Develop for different device classifications | Hasan Hosgel

Usage Includes

<LinearLayout … >…<include />…

</LinearLayout>

Page 53: Droidcon nl 2013 best practices to develop for different android device classifications

Droidcon NL 2013 | Develop for different device classifications | Hasan Hosgel

Usage Includes

<LinearLayout … >…<include layout="@layout/footer"/>…

</LinearLayout>

Page 54: Droidcon nl 2013 best practices to develop for different android device classifications

Droidcon NL 2013 | Develop for different device classifications | Hasan Hosgel

Sample Screen

Use <includes>

Page 55: Droidcon nl 2013 best practices to develop for different android device classifications

Droidcon NL 2013 | Develop for different device classifications | Hasan Hosgel

Sample Screen

Use <includes>

Page 56: Droidcon nl 2013 best practices to develop for different android device classifications

Droidcon NL 2013 | Develop for different device classifications | Hasan Hosgel

Sample Screen

Use <includes>

Create custom view

Page 57: Droidcon nl 2013 best practices to develop for different android device classifications

Droidcon NL 2013 | Develop for different device classifications | Hasan Hosgel

Custom View

public class CustomView extends LinearLayout {…

public CustomView(Context context, AttributeSet attrs) {…addView(createTextView(context, "label"), lp);addView(createTextView(context, "desc"), lp);if(getResources().getBoolean(R.bool.is_phone)){

setOrientation(VERTICAL);} else {

setOrientation(HORIZONTAL);}

}…}

Page 58: Droidcon nl 2013 best practices to develop for different android device classifications

Droidcon NL 2013 | Develop for different device classifications | Hasan Hosgel

Usage CustomView in layout file

<LinearLayout … >…

</LinearLayout>

Page 59: Droidcon nl 2013 best practices to develop for different android device classifications

Droidcon NL 2013 | Develop for different device classifications | Hasan Hosgel

Usage CustomView in layout file

<LinearLayout … >…<de.alosdev.CustomView

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

…</LinearLayout>

Page 60: Droidcon nl 2013 best practices to develop for different android device classifications

Droidcon NL 2013 | Develop for different device classifications | Hasan Hosgel

Sample Screen

Use <includes>

Create custom view

Page 61: Droidcon nl 2013 best practices to develop for different android device classifications

Custom XML Attribute

Droidcon NL 2013 | Develop for different device classifications | Hasan Hosgel

<resources> <declare-styleable > </declare-styleable><resources>

Page 62: Droidcon nl 2013 best practices to develop for different android device classifications

Custom XML Attribute

Droidcon NL 2013 | Develop for different device classifications | Hasan Hosgel

<resources> <declare-styleable name=”CustomView"> </declare-styleable><resources>

Page 63: Droidcon nl 2013 best practices to develop for different android device classifications

Custom XML Attribute

Droidcon NL 2013 | Develop for different device classifications | Hasan Hosgel

<resources> <declare-styleable name=”CustomView"> <attr /> </declare-styleable><resources>

Page 64: Droidcon nl 2013 best practices to develop for different android device classifications

Custom XML Attribute

Droidcon NL 2013 | Develop for different device classifications | Hasan Hosgel

<resources> <declare-styleable name=”CustomView"> <attr name="label" /> </declare-styleable><resources>

Page 65: Droidcon nl 2013 best practices to develop for different android device classifications

Custom XML Attribute

Droidcon NL 2013 | Develop for different device classifications | Hasan Hosgel

<resources> <declare-styleable name=”CustomView"> <attr name="label" format="reference|string"/> </declare-styleable><resources>

Page 66: Droidcon nl 2013 best practices to develop for different android device classifications

Custom XML Attribute

Droidcon NL 2013 | Develop for different device classifications | Hasan Hosgel

<resources> <declare-styleable name=”CustomView"> <attr name="label" format="reference|string"/> <attr name="value" format="reference|string"/> <attr name="orientation" format="enum"> <enum name="horizontal" value="0"/> <enum name="vertical" value="1"/> </attr> </declare-styleable><resources>

Page 67: Droidcon nl 2013 best practices to develop for different android device classifications

Droidcon NL 2013 | Develop for different device classifications | Hasan Hosgel

Usage in layout resource

1. Add to root XML nodexmlns:app="http://schemas.android.com/apk/res-auto"2. Usage in custom view<de.alosdev.CustomView android:id="@+id/customView" android:layout_width="wrap_content" android:layout_height="wrap_content" app:label="label 1" app:orientation="vertical" app:value="value 1" />

Page 68: Droidcon nl 2013 best practices to develop for different android device classifications

Droidcon NL 2013 | Develop for different device classifications | Hasan Hosgel

Usage in code

public class CustomView extends LinearLayout { static final int[] ORIENTATION = new int[] { HORIZONTAL, VERTICAL }; public CustomView(Context context, AttributeSet attrs) { super(context, attrs); … TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CustomView); try { setOrientation(ORIENTATION[ a.getInt(R.styleable.CustomView_orientation, 0)]); } finally { a.recycle(); } } …}

Page 69: Droidcon nl 2013 best practices to develop for different android device classifications

Droidcon NL 2013 | Develop for different device classifications | Hasan Hosgel

Custom XML Attribute

Page 70: Droidcon nl 2013 best practices to develop for different android device classifications

Droidcon NL 2013 | Develop for different device classifications | Hasan Hosgel

Custom XML Attribute

If custom view has much more business logic and need lifecycles Create a Fragment

Page 71: Droidcon nl 2013 best practices to develop for different android device classifications

Droidcon NL 2013 | Develop for different device classifications | Hasan Hosgel

Code

Page 72: Droidcon nl 2013 best practices to develop for different android device classifications

Droidcon NL 2013 | Develop for different device classifications | Hasan Hosgel

Best Practices

●You have already an applicationRemove orientation fixation and suppressing of orientation change from manifest to avoid long bug analyzing.

●You start from the scratchFocus on main classification for faster time to marketBut create an overall concept for better modularization

Page 73: Droidcon nl 2013 best practices to develop for different android device classifications

Droidcon NL 2013 | Develop for different device classifications | Hasan Hosgel

Best Practices

●If you support both orientations, save the instance state while orientation changes for more responsiveness

Especially for states, that need a long computation for creation.Make the state object Parcelable for faster write & read and also to have a smaller memory footprint

Page 74: Droidcon nl 2013 best practices to develop for different android device classifications

Droidcon NL 2013 | Develop for different device classifications | Hasan Hosgel

Developer Hints

●You can start an activity for result from a fragment, so the response can be handled in the fragment.

●If you want to register a special service on every onCreate method of an activity give the ActivityLivecycleCallbacks a try. You can register them in the onCreate method of the application. (minSDK -v14)

Page 75: Droidcon nl 2013 best practices to develop for different android device classifications

Droidcon NL 2013 | Develop for different device classifications | Hasan Hosgel

●If you get a BadParcelableException with the cause ClassNotFound-Exception, the source can be a NullPointerException during the read or write of the Parcelable. Exceptions are hidden during the parcel process.

Page 76: Droidcon nl 2013 best practices to develop for different android device classifications

Droidcon NL 2013 | Develop for different device classifications | Hasan Hosgel

If you want to use “match_parent” or “wrap_content” in a dimension alias, you should use “-1dp” or “-2dp”

project-folder/res/values/dimen.xml<resources> <dimen name="my_dimen>@dimen/match_parent</dimen> <dimen name="match_parent">-1dp</dimen> <dimen name="wrap_content">-2dp</dimen></resources>

project-folder/res/values-sw600dp/layout.xml<resources> <dimen name="my_dimen>300dp</dimen></resources>

Page 77: Droidcon nl 2013 best practices to develop for different android device classifications

Droidcon NL 2013 | Develop for different device classifications | Hasan Hosgel

Listener Hell

If you have to many listeners or you think the programming model is old school like the “goto statements”. Give message/ event/ service bus a try. For Android:Otto from SquareEventBus from greenrobot

See also: Callbacks as our Generations' Go To Statement

Page 80: Droidcon nl 2013 best practices to develop for different android device classifications

Holo Color Generator

Page 81: Droidcon nl 2013 best practices to develop for different android device classifications
Page 82: Droidcon nl 2013 best practices to develop for different android device classifications

Droidcon NL 2013 | Develop for different device classifications | Hasan Hosgel

Mission Accomplished ?

http://www.flickr.com/photos/ianaberle/5729561934/

Page 83: Droidcon nl 2013 best practices to develop for different android device classifications

Droidcon NL 2013 | Develop for different device classifications | Hasan Hosgel

Mission Accomplished

http://www.flickr.com/photos/ianaberle/5729561934/

cleared

Page 84: Droidcon nl 2013 best practices to develop for different android device classifications

Q & A

Source: http://www.flickr.com/photos/21496790@N06/5065834411/Page 84

Page 85: Droidcon nl 2013 best practices to develop for different android device classifications

www.immobilienscout24.dewww.immobilienscout24.de

Thanks for your attention!

Contact:

+HasanHosgelTwitter: @alosdevGithub: alosdev

Best Practices to develop for different Android Device Classificationshttps://github.com/alosdev/multidevice-nightmare-demohttp://www.slideshare.net/hosgel/droidcon-nl-2013-best-practices-to-develop-for-different-android-device-classifications