android dev tips

Post on 28-Jan-2015

111 Views

Category:

Technology

3 Downloads

Preview:

Click to see full reader

DESCRIPTION

Android dev tips

TRANSCRIPT

Android Dev Tips Kanda Runapongsa Saikaew

Agenda 1.  Love Relative Layout 2.  Use Hierarchy Viewer 3.  Use Eclipse Effectively 4.  Use LogCat 5.  Publish Application

1. Love Relative Layout •  Most of the tutorials use LinearLayout, but you will find

that RelativeLayout is truly useful •  A common example is the abuse of LinearLayout, which

leads to a proliferation of views in the view hierarchy •  Every view, or worse every layout manager, you add to

your application comes at a cost: initialization, layout and drawing become slower

Use LinearLayout

Use RelativeLayout

2. Use Hierarchy Viewer •  The Android SDK tools include a tool called Hierarchy

Viewer that allows you to analyze your layout while your application is running

•  Hierarchy Viewer works by allowing you to select running processes on a connected device or emulator, then display the layout tree

•  The traffic lights on each block represent its Measure, Layout and Draw performance, helping you identify potential issues.

Using HierarchyViewer •  The hierarchyviewer tool is available in <sdk>/tools/ •  When opened, the Hierarchy Viewer shows a list of

available devices and its running components

Using Hierarchy Viewer •  Click Load View Hierarchy to view the

layout hierarchy of the selected component

Using Hierarchy Viewer -  A small bitmap image on the left -  Two stacked items of text on the right

Using LinearLayout

Using LinearLayout •  There is a 3-level hierarchy with some

problems laying out the text items •  The timings for rendering a complete list

item using this layout are •  Measure: 0.977ms •  Layout: 0.167ms •  Draw: 2.717ms

Using RelativeLayout

Using RelativeLayout •  Because the layout performance above slows down due to

a nested LinearLayout •  The performance might improve by flattening the layout—

make the layout shallow and wide, rather than narrow and deep

•  Now rendering a list item takes •  Measure: 0.598ms •  Layout: 0.110ms •  Draw: 2.146ms

3. Use Eclipse Effectively •  You should try to keep your hands on

keyboard •  The less you touch the mouse, the more

code you can write •  I am trying to keep the mouse laying still and

control the IDE completely using keyboard.

Eclipse Short Cut Keys •  Ctrl + D Delete row •  Ctrl + 1 Activates the quick fix •  Ctrl + Shift + O Organize imports •  Ctrl + Shift + F Format codes •  Ctrl + Shift + L Shows you a list of your

currently defined shortcut keys

4. Use LogCat •  It can be difficult in Android to figure out

“what went wrong”. •  LogCat will show cause of the problems.

o  Error message with red string. o  Problems that cause by …. (something) with line of

that code.

How to Use LogCat •  To use LogCat, first import android.util.Log

into your project •  Now you can call the static class Log from

your project to start logging •  Logcat has different levels of logging

Different Levels of Logging V — Verbose (lowest priority) D — Debug I — Info W — Warning E — Error F — Fatal S — Silent (highest priority, on which nothing is ever printed)

Setting Different Colors for Different Levels

•  Go to Preferences > LogCat > Colors

Example of Using Log Class

How to View LogCat •  Open LogCat view by clicking the LogCat icon at the

bottom right corner (1 in the figure) •  Filter LogCat level (2 in the figure) •  Search for some keyword (3 in the figure)

5. Publish Application •  Prepare the application for release •  Release the application to users

Configure Your Application for Release

•  Choose a good package •  The package name cannot start with com.example

•  Turn off logging and debugging •  Remove Log calls •  Remove android:debuggable attribute from your

manifest file •  Remove all Debug tracing calls such as

startMethodTracing()

Configure Your Application for Release

•  Clean up your directory •  Review the contents of your jni/, lib/, and src/ directories

•  The jni/ directory should contain only source files associated with the Android NDK, such as .c, .cpp, .h, and .mk files

•  The lib/ directory should contain only third-party library files or private library files, including prebuilt shared and static libraries

•  The src/ directory should not contain any .jar files.

Configure Your Application for Release

•  Review and update your manifest settings •  <uses-permission> element

•  You should specify only those permissions that are relevant and required for application

•  android:icon and android:label attributes •  You must specify values for these attributes, which are

located in the <application> element •  android:versionCode and android:versionName attributes.

• We recommend that you specify values for these attributes

Configure Your Application for Release

•  Address compatibility issues •  Add support for multiple screen configurations. •  Optimize your application for Android tablet devices.

•  If your application is designed for devices older than Android 3.0, make it compatible with Android 3.0 devices

•  Consider using the Support Library. •  If your application is designed for devices running Android 3.x,

make your application compatible with older versions of Android

Support Different Devices •  Support different languages •  Support different screens

•  Different layouts •  Different bitmaps •  Different text sizes

Support Different Languages •  Create the resource subdirectories and string resource

files •  Example MyProject/ res/ values/ strings.xml values-es/ strings.xml

Support Different Languages English (default locale), /values/strings.xml: <?xml version="1.0" encoding="utf-8"?> <resources> <string name="title">My Application</string> <string name="hello_world">Hello World!</string> </resources> Spanish, /values-es/strings.xml: <?xml version="1.0" encoding="utf-8"?> <resources> <string name="title">Mi Aplicación</string> <string name="hello_world">Hola Mundo!</string> </resources>

Support Different Screens •  Android categorizes device screens using two

general properties: size and density •  There are four generalized sizes: small,

normal, large, xlarge •  Four generalized densities: low (ldpi), medium

(mdpi), high (hdpi), extra high (xhdpi)

Support Different Layouts MyProject/ res/ layout/ # default (portrait) main.xml layout-land/ # landscape main.xml layout-large/ # large (portrait) main.xml layout-large-land/ # large landscape main.xml

Support Different Bitmaps •  To generate these images, you should start with your raw resource

in vector format and generate the images for each density using the following size scale: •  xhdpi: 2.0 •  hdpi: 1.5 •  mdpi: 1.0 (baseline) •  ldpi: 0.75

•  This means that if you generate a 200x200 image for xhdpi devices, you should generate the same resource in 150x150 for hdpi, 100x100 for mdpi, and 75x75 for ldpi devices.

Support Different Bitmaps Then, place the files in the appropriate drawable resource directory: MyProject/ res/ drawable-xhdpi/ awesomeimage.png drawable-hdpi/ awesomeimage.png drawable-mdpi/ awesomeimage.png Any time you reference @drawable/awesomeimage, the system selects the

appropriate bitmap based on the screen's density.

Support Different Text Sizes •  You should use the resource folders such as values-ldpi values-mdpi values-hdpi •  Write the text size in 'dimensions.xml' file for each range Sample dimensions.xml <?xml version="1.0" encoding="utf-8"?> <resources> <dimen name="textsize">15sp</dimen> </resources> •  In Java code, textView.setTextSize(getResources().getDimension(R.dimen.textsize));

What to Test •  Change in orientation

•  Is the screen re-drawn correctly? •  Does the application maintain its state?

•  Change in configuration •  A situation that is more general than a change in

orientation is a change in the device's configuration, such as a change in the availability of a keyboard or a change in system language

What to Test •  Battery Life

•  You need to write your application to minimize battery usage, you need to test its battery performance, and you need to test the methods that manage battery usage.

•  Techniques for minimizing battery usage were presented at the 2010 Google I/O conference in the presentation Coding for Life -- Battery Life, That Is.

What to Test •  Dependence on external resources

•  If your application depends on network access, SMS, Bluetooth, or GPS, then you should test what happens when the resource or resources are not available

•  For example, if your application uses the network, it can notify the user if access is unavailable, or disable network-related features, or do both

References •  http://stackoverflow.com/questions/2961049/effective-android-programming-

techniques

•  http://www.curious-creature.org/2009/02/22/android-layout-tricks-1/ •  http://www.curious-creature.org/2012/12/01/android-performance-case-study/ •  http://developer.android.com/training/improving-layouts/optimizing-layout.html •  http://eclipse.dzone.com/news/effective-eclipse-shortcut-key •  http://developer.android.com/tools/testing/what_to_test.html •  http://developer.android.com/tools/publishing/preparing.html •  http://stackoverflow.com/questions/9494037/how-to-set-text-size-of-textview-

dynamically-for-diffrent-screens •  http://developer.android.com/training/basics/supporting-devices/screens.html

Thank you Kanda Runapongsa Saikaew •  Khon Kaen University, Thailand

•  Assistant Professor of Department of Computer Engineering •  Associate Director for Administration of Computer Center

•  krunapon@kku.ac.th •  Twitter: @krunapon •  G+: https://plus.google.com/u/0/118244887738724224199

top related