Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Best Practices to develop for different Android...

Best Practices to develop for different Android Device Classifications - DroidCon Turin 2014

This Session will give a best practices from the Immobilienscout24 Development for the different Android device classifications. During this presentation you can experience the learnings, which the presenter got during development of the ImmobilienScout 24 Android application. The presentation doesn’t cover Design, UX or Fragments, but it gives an introduction and samples of using different resources, in special layouts. These folders can be used for filtering specific configurations, referencing other resources and using layout aliases. This session aims at people with knowledge of coding for Android. Conclusion: The developers should loose their fear of developing for different screensizes in one application.

Hasan Hosgel

February 07, 2014
Tweet

More Decks by Hasan Hosgel

Other Decks in Technology

Transcript

  1. About me +HasanHosgel Twitter: @alosdev Github: alosdev Slideshare: hosgel CO-Organizer

    @ GDG Android in Berlin & community events developer @ ImmobilienScout24 Droidcon IT 2014 | Develop for different device classifications | Hasan Hosgel
  2. 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 ~ 7.5 Mio. App downloads > 3 Mio. Android > 50% mobile traffic Droidcon IT 2014 | Develop for different device classifications | Hasan Hosgel
  3. Here comes The Nightmare Droidcon IT 2014 | Develop for

    different device classifications | Hasan Hosgel Image source: http://www.flickr.com/photos/boogeyman13/4553188509/
  4. Here comes The Nightmare Droidcon IT 2014 | Develop for

    different device classifications | Hasan Hosgel For developers Image source: http://www.flickr.com/photos/boogeyman13/4553188509/
  5. Let’s build the fundament Droidcon IT 2014 | Develop for

    different device classifications | Hasan Hosgel Image source: http://www.flickr.com/photos/hertenberger/1434191066/
  6. Device Classification Droidcon IT 2014 | Develop for different device

    classifications | Hasan Hosgel Images sources: https://play.google.com/store/devices
  7. Device Classification Droidcon IT 2014 | Develop for different device

    classifications | Hasan Hosgel Images sources: https://play.google.com/store/devices http://www.htc.com/de/
  8. Device Classification Droidcon IT 2014 | Develop for different device

    classifications | Hasan Hosgel Images sources: http://www.sony.de/hub/google-tv
  9. Device Classification Droidcon IT 2014 | Develop for different device

    classifications | Hasan Hosgel Images Sources https://developer.ford.com/
  10. Open Automotive Alliance Starting 2014 several companies plan to bring

    Android platform to the cars. • Audi • GM • Google • Honda • Hyundai • Nvidia http://www.openautoalliance.net/ Droidcon IT 2014 | Develop for different device classifications | Hasan Hosgel
  11. Hard Work Ahead Droidcon IT 2014 | Develop for different

    device classifications | Hasan Hosgel Source: http://www.flickr.com/photos/16210667@N02/9172895225
  12. Resource Folders Droidcon IT 2014 | Develop for different device

    classifications | Hasan Hosgel You can use several qualifiers in the resource folders name for serving the best matching resource.
  13. 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) Droidcon IT 2014 | Develop for different device classifications | Hasan Hosgel
  14. Best Matching Resources Win 1.res/values/strings.xml 2.res/values-en-rUS/strings.xml 3.res/values-large/strings.xml 4.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. Droidcon IT 2014 | Develop for different device classifications | Hasan Hosgel
  15. 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) Droidcon IT 2014 | Develop for different device classifications | Hasan Hosgel
  16. Classifications for Layouts Platform version at least v13 (Honeycomb MR2)

    Droidcon IT 2014 | Develop for different device classifications | Hasan Hosgel
  17. Classifications for Layouts Platform version at least v13 (Honeycomb MR2)

    project-folder/res/ layout/ Droidcon IT 2014 | Develop for different device classifications | Hasan Hosgel
  18. Classifications for Layouts Platform version at least v13 (Honeycomb MR2)

    project-folder/res/ layout/  small phones Droidcon IT 2014 | Develop for different device classifications | Hasan Hosgel
  19. Classifications for Layouts Platform version at least v13 (Honeycomb MR2)

    project-folder/res/ layout/  small phones layout-sw320dp/  other phones Droidcon IT 2014 | Develop for different device classifications | Hasan Hosgel
  20. Classifications for Layouts Platform version at least v13 (Honeycomb MR2)

    project-folder/res/ layout/  small phones layout-sw320dp/  other phones layout-sw600dp/  tablets 7” Droidcon IT 2014 | Develop for different device classifications | Hasan Hosgel
  21. 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 Droidcon IT 2014 | Develop for different device classifications | Hasan Hosgel
  22. Platform version at lower v11 project-folder/res/ layout/  phones layout-sw320dp/

     other phones layout-sw600dp/  tablets 7” layout-sw720dp/  tablets 10” Droidcon IT 2014 | Develop for different device classifications | Hasan Hosgel
  23. Platform version at lower v11 project-folder/res/ layout/  phones layout-v11/

     tablets 10” layout-sw320dp/  other phones layout-sw600dp/  tablets 7” layout-sw720dp/  tablets 10” Droidcon IT 2014 | Develop for different device classifications | Hasan Hosgel
  24. Platform version at 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” Droidcon IT 2014 | Develop for different device classifications | Hasan Hosgel
  25. Hint The smallest width qualifier gets automatically platform version ”-v13”

    through the packager, for avoiding problems with the number of matching qualifiers. Droidcon IT 2014 | Develop for different device classifications | Hasan Hosgel
  26. Howto Classify In Code • Read configuration from the device

    Droidcon IT 2014 | Develop for different device classifications | Hasan Hosgel
  27. Howto Classify In Code • Read configuration from the device

    • Smarter approach is to use boolean resources Droidcon IT 2014 | Develop for different device classifications | Hasan Hosgel
  28. 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) Droidcon IT 2014 | Develop for different device classifications | Hasan Hosgel
  29. Current Layout File Structure Droidcon IT 2014 | Develop for

    different device classifications | Hasan Hosgel project-folder/res/ layout/main.xml layout-v11/main.xml layout-v13/main.xml layout-sw320dp/main.xml layout-sw600dp/main.xml layout-sw720dp/main.xml
  30. Current Layout File Structure Droidcon IT 2014 | Develop for

    different device classifications | Hasan Hosgel project-folder/res/ layout/main.xml layout-v11/main.xml layout-v13/main.xml layout-sw320dp/main.xml layout-sw600dp/main.xml layout-sw720dp/main.xml Fixing one bug in the 10“ layout has to be done in two files.
  31. Current Layout File Structure Droidcon IT 2014 | Develop for

    different device classifications | Hasan Hosgel project-folder/res/ layout/main.xml layout-v11/main.xml layout-v13/main.xml layout-sw320dp/main.xml layout-sw600dp/main.xml layout-sw720dp/main.xml Fixing one bug in the 10“ layout has to be done in two files.  error prone
  32. 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 Droidcon IT 2014 | Develop for different device classifications | Hasan Hosgel
  33. 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> Droidcon IT 2014 | Develop for different device classifications | Hasan Hosgel
  34. 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> Droidcon IT 2014 | Develop for different device classifications | Hasan Hosgel
  35. 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> Droidcon IT 2014 | Develop for different device classifications | Hasan Hosgel
  36. 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> Droidcon IT 2014 | Develop for different device classifications | Hasan Hosgel
  37. 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> Droidcon IT 2014 | Develop for different device classifications | Hasan Hosgel
  38. Sample Screen Use <includes> Droidcon IT 2014 | Develop for

    different device classifications | Hasan Hosgel
  39. Usage Includes <LinearLayout … > … … </LinearLayout> Droidcon IT

    2014 | Develop for different device classifications | Hasan Hosgel
  40. Usage Includes <LinearLayout … > … <include /> … </LinearLayout>

    Droidcon IT 2014 | Develop for different device classifications | Hasan Hosgel
  41. Usage Includes <LinearLayout … > … <include layout="@layout/footer"/> … </LinearLayout>

    Droidcon IT 2014 | Develop for different device classifications | Hasan Hosgel
  42. Sample Screen Use <includes> Droidcon IT 2014 | Develop for

    different device classifications | Hasan Hosgel
  43. Sample Screen Use <includes> Droidcon IT 2014 | Develop for

    different device classifications | Hasan Hosgel
  44. Sample Screen Use <includes> Create custom view Droidcon IT 2014

    | Develop for different device classifications | Hasan Hosgel
  45. 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); } } … } Droidcon IT 2014 | Develop for different device classifications | Hasan Hosgel
  46. Usage CustomView in layout file <LinearLayout … > … …

    </LinearLayout> Droidcon IT 2014 | Develop for different device classifications | Hasan Hosgel
  47. Usage CustomView in layout file <LinearLayout … > … <de.alosdev.CustomView

    android:layout_width="wrap_content" android:layout_height="wrap_content"/> … </LinearLayout> Droidcon IT 2014 | Develop for different device classifications | Hasan Hosgel
  48. Sample Screen Use <includes> Create custom view Droidcon IT 2014

    | Develop for different device classifications | Hasan Hosgel
  49. Custom XML Attribute Droidcon IT 2014 | Develop for different

    device classifications | Hasan Hosgel <resources> <declare-styleable > </declare-styleable> <resources>
  50. Custom XML Attribute Droidcon IT 2014 | Develop for different

    device classifications | Hasan Hosgel <resources> <declare-styleable name=”CustomView"> </declare-styleable> <resources>
  51. Custom XML Attribute Droidcon IT 2014 | Develop for different

    device classifications | Hasan Hosgel <resources> <declare-styleable name=”CustomView"> <attr /> </declare-styleable> <resources>
  52. Custom XML Attribute Droidcon IT 2014 | Develop for different

    device classifications | Hasan Hosgel <resources> <declare-styleable name=”CustomView"> <attr name="label" /> </declare-styleable> <resources>
  53. Custom XML Attribute Droidcon IT 2014 | Develop for different

    device classifications | Hasan Hosgel <resources> <declare-styleable name=”CustomView"> <attr name="label" format="reference|string"/> </declare-styleable> <resources>
  54. Custom XML Attribute Droidcon IT 2014 | 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>
  55. Usage in layout resource 1. Add to root XML node

    xmlns: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" /> Droidcon IT 2014 | Develop for different device classifications | Hasan Hosgel
  56. 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(); } } … } Droidcon IT 2014 | Develop for different device classifications | Hasan Hosgel
  57. Custom XML Attribute Droidcon IT 2014 | Develop for different

    device classifications | Hasan Hosgel If custom view has much more business logic and need lifecycles  Create a Fragment
  58. 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 Droidcon IT 2014 | Develop for different device classifications | Hasan Hosgel
  59. 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 Droidcon IT 2014 | Develop for different device classifications | Hasan Hosgel
  60. 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) Droidcon IT 2014 | Develop for different device classifications | Hasan Hosgel
  61. •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. Droidcon IT 2014 | Develop for different device classifications | Hasan Hosgel
  62. 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> Droidcon IT 2014 | Develop for different device classifications | Hasan Hosgel
  63. 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 Square EventBus from greenrobot See also: Callbacks as our Generations' Go To Statement Droidcon IT 2014 | Develop for different device classifications | Hasan Hosgel
  64. Custom Theme & Style Android Ui Utils Droidcon IT 2014

    | Develop for different device classifications | Hasan Hosgel
  65. Mission Accomplished ? Droidcon IT 2014 | Develop for different

    device classifications | Hasan Hosgel http://www.flickr.com/photos/ianaberle/5729561934/
  66. Mission Accomplished Droidcon IT 2014 | Develop for different device

    classifications | Hasan Hosgel http://www.flickr.com/photos/ianaberle/5729561934/
  67. Q & A Source: http://www.flickr.com/photos/21496790@N06/5065834411/ Page 85 Droidcon IT 2014

    | Develop for different device classifications | Hasan Hosgel
  68. www.immobilienscout24.de www.immobilienscout24.de Thanks for your attention! Contact: +HasanHosgel Twitter: @alosdev

    Github: alosdev Best Practices to develop for different Android Device Classifications https://github.com/alosdev/multidevice-nightmare-demo http://www.slideshare.net/hosgel/droidcon-it-2014-best-practices-to-develop-for- different-android-device-classifications