1. How to deal with Fragmentation
on Android
Sittiphol Phanvilai
Managing Director
Hua Lampong Co.,Ltd.
2. Who am I
• Sittiphol Phanvilai
• nuuneoi
• Cross Platform Mobile
Application Developer
• Managing Director at
Hua Lampong Co.,Ltd.
• Founder of DroidSans.com
the Android Community
• Founder of MOLOME™
3. MOLOME ™
• Share our experience how
we beat fragmentation on
Android
• Who should concern?
– Designer
– Developer
26. That's why …
• That’s why
– There are a lot of junk apps on Google Play
– UI of Android Application is not so beautiful (in
average)
27.
28.
29.
30. Deal with Software Fragmentation
Build application based on Android 2.2 Froyo
No need to support Android 2.1 Eclair
31. API Level
• FACT: You could compile with higher API level
and run in lower OS version
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="14" />
32. How to run new API on old OS
• Sorry you couldn’t but you could do the trick
if (android.os.Build.VERSION.SDK_INT > 8)
{
camera = Camera.open(camera_id);
}
else
{
camera = Camera.open();
}
33. Deal with Screen Fragmentation
320x480 540x960 720x1280 600x1024 800x1280
34. Deal with Screen Fragmentation
Recommended lowest supported screen:
normal-mdpi
Care a bit for lower but no need
Hard-hearted you need to be
35. Multiple drawable
• Android provides mechanic to
use different resource for
different dpi / screen size
• I suggest you to go for
drawable-hdpi only, others
will be scaled automatically
• As same as layout
• Unless you have to
36. Designing Step
• Always snap each component to screen or
another component
• Use
– LinearLayout
– RelativeLayout
– FrameLayout
• Don’t fix the position
37. Snap Component to Screen
Snap to Top of Screen Snap to the right
Snap to the left
Snap to the left
Snap to Bottom of Screen
43. Never trust in “Pixel”
• Actual Pixel on Screen
– px Don’t use it!
• Physical Size of the Screen
– dp, dip (Relative to 160dpi) Recommended for UI
– sp Recommended for font
– pt (1/72 inch)
– in
– mm
45. Designing Step: Component Size
Fixed dp Screen Width-Related px
– Use for the “Exact Size” – To make the design fit
component for example any screen
button (since your Finger – To please designer
is always in the same
size)
– This will make final
product different from
design
– To please developer
52. Screen Width-Related px
• Inherit View or Layout and set the size on
initializing
• Override onLayout if you wish to manage the
children’s position manually
• Don’t override onMeasure if not neccessary
60. Common Problem with ImageView
How to show this image
fitting the screen in
aspect ratio?
61. Common Problem with ImageView
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="@drawable/img"
/>
62. Common Problem with ImageView
// Inherits ImageView
protected void onMeasure(int widthMeasureSpec,
int heightMeasureSpec) {
if (getDrawable() == null)
{
setMeasuredDimension(0, 0);
return;
}
int width = MeasureSpec.getSize(widthMeasureSpec);
setMeasuredDimension(width, width *
getDrawable().getIntrinsicHeight() /
getDrawable().getIntrinsicWidth());
}
// xml
<com.hlpth.fragmentation.AspectRatioImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="@drawable/img"
/>
63. More Common Problem
• Please note that you will see this kind of
problem a lot on Android Application
Development
• You need to check one by one unavoidably
64. Deal with CPU Fragmentation
• Directly effect Native Code (NDK)
• Don’t use any platform-specific code or
assembly code
• Compile for every single CPU architecture
// Application.mk
APP_ABI := armeabi armeabi-v7a x86
APP_PLATFORM := android-9
67. RAM
• Each phone has different amount of free RAM
• Always use AdapterView. It will help you
reduce memory consumption
– ListView
– Gallery
– GridView
68. Memory Leak
• First reason of app crashing in large-scale
application
• Don’t 100% trust in Garbage Collector
• Use Context wisely, it holds all of UI
component inside. If there is still some
variable point to context, say hello to memory
leak
• Use Heap monitoring tools comes with ADT
plugin. Your life will be easier.
69. Bitmap.recycle()
• If you fully take control the Bitmap object,
don’t forget to recycle() it when you don’t
need it anymore.
• Not necessary, just recommended
70. Nested Layout
• Don’t implement more than 6 levels of nested
Layout
• There is always the way to reduce level of
nested Layout. Just need to put more effort.
• LayoutInflater and <merge> is one of the key
to implement complicated UI with high
performance
71. Caching
• Normally there is so limited amount of
Storage
• But some has so many
• If you wish to cache anything, limit cache size
to 2-4 MB
72. OpenGL
• OpenGL could limit number of target devices,
don’t use it unless you have to.
• Better use OpenGL 1.1 over OpenGL 2.0
73. Service
• If you need to do some background task, go
for Service
• Launch when needed only and terminate once
the task has finished
74. Multiple APK
• Google Play Store provides feature to use
multiple apk
• Don’t use it. It will ruin your life in long run.
75. Target Devices
• You can’t please everybody. If you have to
scope down the number of target devices, do
it with hard-hearted.
• To make your application fully compatible with
3000 THB phone might costs your time double
76. Custom ROM
• Don’t care a lot about phone with Custom
ROM. There is always a problem.
• Care only major Custom ROM.
77. Message to Designer
• Don’t expect what you design and the final
product will be 100% exactly
• You could expect that on iOS but not on
Android
• Designer DO need to know Android’s UI
guideline before design or it might not be able
to implement