Monday, March 19, 2012

Efficient android programming Tips


General Tips:
  1. Add Two Numbers: Your first non-tutorial application should be to take two numbers and add them together. It sounds too simple. You will spend some hours getting the layouts, the callbacks, and onPause/onResume to work correctly. Do it.
  2. It is Java: You work in Java for most of your Android programming. Don't spend time praising it. Don't spend time complaining about it. Just work with it.
  3. Love RelativeLayout: Most of the tutorials use LinearLayout, but you will find that RelativeLayout is truly useful. Many layouts, like the GridLayout aren't used much at all. Play around with RelativeLayout. See an example from this question.
  4. Use fill_parent with a top level RelativeLayout: A surprisingly common and hard to find problem is putting a wrap_content in a top level RelativeLayout and then wondering why unrelated fields far down in the hierarchy are rendering strangely.
  5. Use empty layout items: You will often use empty items in your layouts just for positioning other layouts. For example, you might use an empty TextField, of width=0 and height=0 and centerInParent='True' just to anchor things relative to the middle of the screen. Also, you might have an empty TextField or LinearLayout so that you can give a layout_weight=1 to it and have it take up more screen space.
  6. Set a layout background color: If you are having trouble figuring out your layout, try setting the background colors on some objects. It can highlight your mistakes faster than other tools, and shows some surprises that the IDE red box doesn't always help.
  7. Download Apps-For-Android: This is a big chunk of useful source code for a half dozen applications. It can supplement the sample applications nicely and show different coding style solutions. Grab it using svn co http://apps-for-android.googlecode.com/svn/trunk/ apps-for-android-read-only
  8. Download the source: You need the Android source to solve some problems or, more likely, get past holes in the documentation. Your copy does not need to be perfect or kept up to date. You can learn to use the repo command, or just visit http://android.git.kernel.org/ for a snapshot.
  9. Learn to search your source: The fastest solution to many problems is to find where a particular parameter is used in some other source. Put a copy or link to the sample applications, apps-for-android applications, and any other source you have under one directory tree. Use "grep -ir funky_parameter sample_code/" or your favorite searching routine to quickly find some code that uses that parameter.
  10. Use Eclipse: Even you have a favorite editor or IDE you have used for years, use Eclipse for Android development. It is good enough as an IDE and is really part of the development tools suite. Any time you spend trying to jury-rig your IDE to work is time you didn't code.
  11. Learn Eclipse: Learn a few new tricks with Eclipse every day. Some of my favorite commands were found reading this list and this question.
  12. Get help when starting out: The quantity of readable material can be overwhelming. Setting up the environment can be tricky. Going to an Android Meet-up or users group can help get over the initial hump.
  13. Code every day: Android code will be frustrating. Don't allow yourself to stop because you get stuck. Play around with the tools, step through a sample application, read one of the articles, or read a blog to ease the frustration. Then write some more code.
  14. Lurk on IRC: Connect your favorite IRC reader to irc.freenode.net's #android-dev channel. Leave it up in the background. Only ask questions after you have spent ten minutes trying to figure it out on your own.
  15. Use two monitors: Working on your laptop in the coffee shop will slow you down. You need to spread out windows. At very least, you will want a full screen Eclipse session, the emulator, and a browser with the tutorial to all be easily visible. Three screens may work even better.
  16. Reformat XML files: The layout editor makes a hash of the XML files. Use the 'source/format' command to put them in a reasonable form. You will want to check the "Eclipse/Windows/Preferences/XML/XML Files/Editor/Formatting/Split XML attributes each on a new line" check box. Then use shift-ctrl-F to format it.
  17. Edit XML files with the text editor: After the first couple of layout screens, stop using the slow moving 'properties' gui to change properties. Drop the items using the gui. Use the up/down arrows on the far right outline to get the hierarchy of views and layouts correct, then edit the XML file directly and use the ctrl-space shortcut to bring up possible completions and explanations of the properties.
  18. Plan on Piracy in the MarketPlace: Google has not made the MarketPlace a Happy Place. Apps are copied and reposted with changed names to funnel money around. Lots of scammers are trying to game the system in many ways. Don't plan on making a living solely from AppStore revenues nor plan on Google being responsive.
  19. Use LogCat: It can be difficult in Android to figure out 'what went wrong'. Run the application in the debugger and look at the logcat window. I found it worthwhile to add a new perspective with just a maximized logcat window. If you like to have a colored LogCat output in a separate window try this tool: Colored LogCat
  20. Explore the tools directory: There a lot helpful tools in the sdk's tools directory, such as hierarchyviewer and layoutopt. Each is helpful and there is no shortcut to learning about each tool one at a time.
Important Tips:
  1. Don't forget to free resources after use.
    Lot of resources like Cursors are overlooked. Free them too.
  2. Don't Use magic Numbers.
    values[0] is meaningless. The framework provides very useful accessors like
    values[SensorManager.DATA_X]
  3. "Make use of onPause()/onResume to save or close what does not need to be opened the whole time."
protected void onResume() {
mSensorManager.registerListener(...);
}
protected void onStop() {
  mSensorManager.unregisterListener(...);
  super.onStop();
}

4.       Make your Android UI Fast and Efficient from the Google I/O has a lot of useful UI Performance tips.
  • Optimize Judiciously
  • Avoid Creating Objects
  • Performance Myths
  • Prefer Static Over Virtual
  • Avoid Internal Getters/Setters
  • Use Static Final For Constants
  • Use Enhanced For Loop Syntax
  • Avoid Enums Where You Only Need Ints
  • Use Package Scope with Inner Classes
  • Use Floating-Point Judiciously
  • Know And Use The Libraries
  • Use Native Methods Judiciously
Best Practices for User Interfaces:
·         1 Read the UI guidelines
·         2 Understand and design for touch mode
·         3 But, support multiple interaction modes
·         4 Use notifications and the window shade
·         5 Support interaction between applications
·         6 Keep your UI fast and responsive
·         7 Use widgets and live folders
·         8 Handle screen orientation changes
·         9 Use images wisely
·         10 Use layouts that adapt to multiple devices
·         And from SO:



No comments:

Post a Comment