88
Hasan Hosgel | ImmobilienScout24 Best Practices to develop for different Android Device Classifications

Mtc spring 2014 best practices to develop for different android device classifications

Embed Size (px)

Citation preview

Page 1: Mtc spring 2014 best practices to develop for different android device classifications

Hasan Hosgel | ImmobilienScout24

Best Practices to develop for different Android Device Classifications

Page 3: Mtc spring 2014 best practices to develop for different android device classifications

ImmobilienScout24

Germany’s largest real estate listing company.

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

> 3 Mio. Android> 50% mobile traffic

Page 4: Mtc spring 2014 best practices to develop for different android device classifications

Fragmentation

Page 5: Mtc spring 2014 best practices to develop for different android device classifications

Fragmentation

> 5000 Android Devices

Page 6: Mtc spring 2014 best practices to develop for different android device classifications

Here comes the Nightmare

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

Page 7: Mtc spring 2014 best practices to develop for different android device classifications

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

Here comes the Nightmare

For developers

Page 8: Mtc spring 2014 best practices to develop for different android device classifications

Let’s build the fundament

Image source:http://www.flickr.com/photos/hertenberger/1434191066/

Page 9: Mtc spring 2014 best practices to develop for different android device classifications

Device Classification

Image source:https://play.google.com/store/devices

Page 10: Mtc spring 2014 best practices to develop for different android device classifications

Image source:https://play.google.com/store/deviceshttp://www.htc.com

Page 11: Mtc spring 2014 best practices to develop for different android device classifications

Image source:http://www.sony.de/hub/google-tv

Page 12: Mtc spring 2014 best practices to develop for different android device classifications

Image source:https://developer.ford.com/

Page 13: Mtc spring 2014 best practices to develop for different android device classifications

Open Automotive AllianceStarting 2014 several companies plan to bring Android platform to the cars.• Audi• GM• Google• Honda• Hyundai• Nvidiahttp://www.openautoalliance.net/

Page 14: Mtc spring 2014 best practices to develop for different android device classifications

Wearables

Page 15: Mtc spring 2014 best practices to develop for different android device classifications

Android Wear SDK

Page 16: Mtc spring 2014 best practices to develop for different android device classifications

Google Glass

Page 17: Mtc spring 2014 best practices to develop for different android device classifications

Hard Work Ahead

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

Page 18: Mtc spring 2014 best practices to develop for different android device classifications

Resource Folders

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

Page 19: Mtc spring 2014 best practices to develop for different android device classifications

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

Page 20: Mtc spring 2014 best practices to develop for different android device classifications

Best Matching Resource Wins1. res/values/strings.xml2. res/values-en-rUS/strings.xml3. res/values-large/strings.xml4. res/values-sw600dp/strings.xml

Page 21: Mtc spring 2014 best practices to develop for different android device classifications

Best Matching Resource Wins1. 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 22: Mtc spring 2014 best practices to develop for different android device classifications

Image 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 23: Mtc spring 2014 best practices to develop for different android device classifications

Classifications for Layouts

Platform version at least v13 (Honeycomb MR2)

Page 24: Mtc spring 2014 best practices to develop for different android device classifications

Classifications for Layouts

Platform version at least v13 (Honeycomb MR2)

project-folder/res/

layout/

Page 25: Mtc spring 2014 best practices to develop for different android device classifications

Classifications for Layouts

Platform version at least v13 (Honeycomb MR2)

project-folder/res/

layout/ small phones

Page 26: Mtc spring 2014 best practices to develop for different android device classifications

Classifications for Layouts

Platform version at least v13 (Honeycomb MR2)

project-folder/res/

layout/ small phones

layout-sw320dp/ other phones

Page 27: Mtc spring 2014 best practices to develop for different android device classifications

Classifications for Layouts

Platform version at least v13 (Honeycomb MR2)

project-folder/res/

layout/ small phones

layout-sw320dp/ other phones

layout-sw600dp/ tablets 7”

Page 28: Mtc spring 2014 best practices to develop for different android device classifications

Classifications for Layouts

Platform version at least v13 (Honeycomb MR2)

project-folder/res/

layout/ small phones

layout-sw320dp/ other phones

layout-sw600dp/ tablets 7”

layout-sw720dp/ tablets 10”

* You should also use the orientation qualifier

Page 29: Mtc spring 2014 best practices to develop for different android device classifications

Platform version lower v11

project-folder/res/

layout/

layout-sw320dp/ other phones

layout-sw600dp/ tablets 7”

layout-sw720dp/ tablets 10”

Page 30: Mtc spring 2014 best practices to develop for different android device classifications

Platform version lower v11

project-folder/res/

layout/ phones

layout-sw320dp/ other phones

layout-sw600dp/ tablets 7”

layout-sw720dp/ tablets 10”

Page 31: Mtc spring 2014 best practices to develop for different android device classifications

Platform version lower v11

project-folder/res/

layout/ phones

layout-v11/ tablets 10”

layout-sw320dp/ other phones

layout-sw600dp/ tablets 7”

layout-sw720dp/ tablets 10”

Page 32: Mtc spring 2014 best practices to develop for different android device classifications

Platform version lower v11

project-folder/res/

layout/ phones

layout-v11/ tablets 10”

layout-v13/ small phones

layout-sw320dp/ other phones

layout-sw600dp/ tablets 7”

layout-sw720dp/ tablets 10”

Page 33: Mtc spring 2014 best practices to develop for different android device classifications

Hint

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

Page 34: Mtc spring 2014 best practices to develop for different android device classifications

How to Classify In Code• Read configuration from the device

Page 35: Mtc spring 2014 best practices to develop for different android device classifications

How to Classify In Code• Read configuration from the device• Smarter approach is to use boolean

resources

Page 36: Mtc spring 2014 best practices to develop for different android device classifications

project-folder/res/values/layouts.xml

<resources>

</resources>

Page 37: Mtc spring 2014 best practices to develop for different android device classifications

project-folder/res/values/layouts.xml

<resources>

<bool > </bool>

</resources>

Page 38: Mtc spring 2014 best practices to develop for different android device classifications

project-folder/res/values/layouts.xml

<resources>

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

</resources>

Page 39: Mtc spring 2014 best practices to develop for different android device classifications

project-folder/res/values/layouts.xml

<resources>

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

</resources>

Page 40: Mtc spring 2014 best practices to develop for different android device classifications

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 41: Mtc spring 2014 best practices to develop for different android device classifications

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 42: Mtc spring 2014 best practices to develop for different android device classifications

Current Layout File Structureproject-folder/res/

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

Page 43: Mtc spring 2014 best practices to develop for different android device classifications

Current Layout File Structureproject-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 44: Mtc spring 2014 best practices to develop for different android device classifications

Current Layout File Structureproject-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 45: Mtc spring 2014 best practices to develop for different android device classifications

Resource Alias

Put your layout files in the default folder.project-folder/res/

layout/main_phone_small.xml

layout/main_phone_other.xml

layout/main_tablet_7.xml

layout/main_tablet_10.xml

Page 46: Mtc spring 2014 best practices to develop for different android device classifications

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

project-folder/res/values-sw720dp/layouts.xml

<resources>

</resources>

Page 47: Mtc spring 2014 best practices to develop for different android device classifications

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 48: Mtc spring 2014 best practices to develop for different android device classifications

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 49: Mtc spring 2014 best practices to develop for different android device classifications

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 50: Mtc spring 2014 best practices to develop for different android device classifications

Scenario

Page 51: Mtc spring 2014 best practices to develop for different android device classifications
Page 52: Mtc spring 2014 best practices to develop for different android device classifications

Use <includes>

Page 53: Mtc spring 2014 best practices to develop for different android device classifications

Usage include<LinearLayout … >

…< />…

</LinearLayout>

Page 54: Mtc spring 2014 best practices to develop for different android device classifications

Usage include<LinearLayout … >

…<include />…

</LinearLayout>

Page 55: Mtc spring 2014 best practices to develop for different android device classifications

Usage include<LinearLayout … >

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

</LinearLayout>

Page 56: Mtc spring 2014 best practices to develop for different android device classifications

Use <includes>

Page 57: Mtc spring 2014 best practices to develop for different android device classifications

Use <includes>

Page 58: Mtc spring 2014 best practices to develop for different android device classifications

Custom Viewpublic 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 59: Mtc spring 2014 best practices to develop for different android device classifications

Usage In Layout Files<LinearLayout … >

…<de.alosdev.CustomViewandroid:layout_width="wrap_content" android:layout_height="wrap_content"/>…

</LinearLayout>

Page 60: Mtc spring 2014 best practices to develop for different android device classifications

Use <includes>

Create custom view

Page 61: Mtc spring 2014 best practices to develop for different android device classifications

Custom XML Attribute<resources> <resources>

Page 62: Mtc spring 2014 best practices to develop for different android device classifications

Custom XML Attribute<resources> <declare-styleable > </declare-styleable><resources>

Page 63: Mtc spring 2014 best practices to develop for different android device classifications

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

Page 64: Mtc spring 2014 best practices to develop for different android device classifications

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

Page 65: Mtc spring 2014 best practices to develop for different android device classifications

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

Page 66: Mtc spring 2014 best practices to develop for different android device classifications

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

Page 67: Mtc spring 2014 best practices to develop for different android device classifications

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

Page 68: Mtc spring 2014 best practices to develop for different android device classifications

Custom XML Attribute<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 69: Mtc spring 2014 best practices to develop for different android device classifications

Usage In Layout Files

1. Add to root XML nodexmlns:app="http://schemas.android.com/apk/res-auto"

Page 70: Mtc spring 2014 best practices to develop for different android device classifications

Usage In Layout Files

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 71: Mtc spring 2014 best practices to develop for different android device classifications

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 72: Mtc spring 2014 best practices to develop for different android device classifications
Page 73: Mtc spring 2014 best practices to develop for different android device classifications

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

Page 74: Mtc spring 2014 best practices to develop for different android device classifications

Code

Page 75: Mtc spring 2014 best practices to develop for different android device classifications

Best Practices

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

You start from the scratch Focus on main classification for faster time to market But create an overall concept for better modularization

Page 76: Mtc spring 2014 best practices to develop for different android device classifications

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 77: Mtc spring 2014 best practices to develop for different android device classifications

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 78: Mtc spring 2014 best practices to develop for different android device classifications

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 79: Mtc spring 2014 best practices to develop for different android device classifications

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

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

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

Page 80: Mtc spring 2014 best practices to develop for different android device classifications

Listener HellIf 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 Square• EventBus from greenrobot

See also: Callbacks as our Generations' Go To Statement

Page 83: Mtc spring 2014 best practices to develop for different android device classifications

Holo Color Generator

Page 84: Mtc spring 2014 best practices to develop for different android device classifications
Page 85: Mtc spring 2014 best practices to develop for different android device classifications

Mission Accomplished?

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

Page 86: Mtc spring 2014 best practices to develop for different android device classifications

Mission Accomplished

cleared

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

Page 87: Mtc spring 2014 best practices to develop for different android device classifications

Q & A

Image source:http://www.flickr.com/photos/21496790@N06/5065834411/

Page 88: Mtc spring 2014 best practices to develop for different android device classifications

www.immobilienscout24.dewww.immobilienscout24.de

Thanks for your attention!

Contact:+HasanHosgel

@alosdev

alosdev

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