Android dev tips


Published on

Android dev tips

Published in: Technology, Art & Photos
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Android dev tips

  1. 1. Android Dev Tips Kanda Runapongsa Saikaew
  2. 2. Agenda 1.  2.  3.  4.  5.  Love Relative Layout Use Hierarchy Viewer Use Eclipse Effectively Use LogCat Publish Application
  3. 3. 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
  4. 4. Use LinearLayout
  5. 5. Use RelativeLayout
  6. 6. 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.
  7. 7. 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
  8. 8. Using Hierarchy Viewer •  Click Load View Hierarchy to view the layout hierarchy of the selected component
  9. 9. Using Hierarchy Viewer -  A small bitmap image on the left -  Two stacked items of text on the right
  10. 10. Using LinearLayout
  11. 11. 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
  12. 12. Using RelativeLayout
  13. 13. 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
  14. 14. 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.
  15. 15. Eclipse Short Cut Keys •  •  •  •  •  Ctrl + D Ctrl + 1 Ctrl + Shift + O Ctrl + Shift + F Ctrl + Shift + L Delete row Activates the quick fix Organize imports Format codes Shows you a list of your currently defined shortcut keys
  16. 16. 4. Use LogCat •  It can be difficult in Android to figure out •  “what went wrong”. LogCat will show cause of the problems. o  o  Error message with red string. Problems that cause by …. (something) with line of that code.
  17. 17. 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
  18. 18. 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)
  19. 19. Setting Different Colors for Different Levels •  Go to Preferences > LogCat > Colors
  20. 20. Example of Using Log Class
  21. 21. 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)
  22. 22. 5. Publish Application •  Prepare the application for release •  Release the application to users
  23. 23. 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()
  24. 24. 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.
  25. 25. 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
  26. 26. 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
  27. 27. Support Different Devices •  Support different languages •  Support different screens •  Different layouts •  Different bitmaps •  Different text sizes
  28. 28. Support Different Languages •  •  Create the resource subdirectories and string resource files Example MyProject/ res/ values/ strings.xml values-es/ strings.xml
  29. 29. 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>
  30. 30. 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)
  31. 31. Support Different Layouts MyProject/ res/ layout/ main.xml # default (portrait) layout-land/ # landscape main.xml layout-large/ # large (portrait) main.xml layout-large-land/ # large landscape main.xml
  32. 32. 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.
  33. 33. 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.
  34. 34. 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));
  35. 35. 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
  36. 36. 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.
  37. 37. 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
  38. 38. References •  •  •  •  •  •  •  •  •
  39. 39. Thank you Kanda Runapongsa Saikaew •  Khon Kaen University, Thailand •  •  •  •  •  Assistant Professor of Department of Computer Engineering Associate Director for Administration of Computer Center Twitter: @krunapon G+: