Your SlideShare is downloading. ×

Android UI Design Patterns & Best Practices

68,099

Published on

Presentation discussing Android Design Patterns, and Best Practices.

Presentation discussing Android Design Patterns, and Best Practices.

Published in: Technology
4 Comments
61 Likes
Statistics
Notes
No Downloads
Views
Total Views
68,099
On Slideshare
0
From Embeds
0
Number of Embeds
8
Actions
Shares
0
Downloads
1,767
Comments
4
Likes
61
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide
  • http://nanocr.eu/ - Google’s mismanagement of the Android Market
  • Android offers several ways to access the UI thread from other threads. You may already be familiar with some of them but here is a comprehensive list:    * Activity.runOnUiThread(Runnable)    * View.post(Runnable)    * View.postDelayed(Runnable, long)    * Handler AsyncTask is The goal of AsyncTask is to take care of thread management for you. Whenever you first start an Android application, a thread called "main" is automatically created. The main thread, also called the UI thread, is very important because it is in charge of dispatching the events to the appropriate widgets and this includes the drawing events. It is also the thread you interact with Android widgets on. For instance, if you touch the a button on screen, the UI thread dispatches the touch event to the widget which in turn sets its pressed state and posts an invalidate request to the event queue. The UI thread dequeues the request and notifies the widget to redraw itself. This single thread model can yield poor performance in Android applications that do not consider the implications. Since everything happens on a single thread performing long operations, like network access or database queries, on this thread will block the whole user interface. No event can be dispatched, including drawing events, while the long operation is underway. From the user's perspective, the application appears hung. Even worse, if the UI thread is blocked for more than a few seconds (about 5 seconds currently) the user is presented with the infamous "application not responding" (ANR) dialog.
  • Android offers several ways to access the UI thread from other threads. You may already be familiar with some of them but here is a comprehensive list:    * Activity.runOnUiThread(Runnable)    * View.post(Runnable)    * View.postDelayed(Runnable, long)    * Handler AsyncTask is The goal of AsyncTask is to take care of thread management for you. Whenever you first start an Android application, a thread called "main" is automatically created. The main thread, also called the UI thread, is very important because it is in charge of dispatching the events to the appropriate widgets and this includes the drawing events. It is also the thread you interact with Android widgets on. For instance, if you touch the a button on screen, the UI thread dispatches the touch event to the widget which in turn sets its pressed state and posts an invalidate request to the event queue. The UI thread dequeues the request and notifies the widget to redraw itself. This single thread model can yield poor performance in Android applications that do not consider the implications. Since everything happens on a single thread performing long operations, like network access or database queries, on this thread will block the whole user interface. No event can be dispatched, including drawing events, while the long operation is underway. From the user's perspective, the application appears hung. Even worse, if the UI thread is blocked for more than a few seconds (about 5 seconds currently) the user is presented with the infamous "application not responding" (ANR) dialog.
  • AsyncTask must be used by subclassing it. It is also very important to remember that an AsyncTask instance has to be created on the UI thread and can be executed only once. You can specify the type, using generics, of the parameters, the progress values and the final value of the task    * The method doInBackground() executes automatically on a worker thread    * onPreExecute(), onPostExecute() and onProgressUpdate() are all invoked on the UI thread    * The value returned by doInBackground() is sent to onPostExecute()    * You can call publishProgress() at anytime in doInBackground() to execute onProgressUpdate() on the UI thread    * You can cancel the task at any time, from any thread Example: public void onClick(View v) {  new DownloadImageTask().execute(" http://example.com/image.png "); } private class DownloadImageTask extends AsyncTask {     protected Bitmap doInBackground(String... urls) {         return loadImageFromNetwork(urls[0]);     }     protected void onPostExecute(Bitmap result) {         mImageView.setImageBitmap(result);     }  }
  • IntentService is a base class for Service s that handle asynchronous requests (expressed as Intent s) on demand. Clients send requests through startService(Intent) calls; the service is started as needed, handles each Intent in turn using a worker thread, and stops itself when it runs out of work. This "work queue processor" pattern is commonly used to offload tasks from an application's main thread. The IntentService class exists to simplify this pattern and take care of the mechanics. To use it, extend IntentService and implement onHandleIntent(Intent) . IntentService will receive the Intents, launch a worker thread, and stop the service as appropriate. All requests are handled on a single worker thread -- they may take as long as necessary (and will not block the application's main loop), but only one request will be processed at a time.
  • AsyncTask must be used by subclassing it. It is also very important to remember that an AsyncTask instance has to be created on the UI thread and can be executed only once. You can specify the type, using generics, of the parameters, the progress values and the final value of the task    * The method doInBackground() executes automatically on a worker thread    * onPreExecute(), onPostExecute() and onProgressUpdate() are all invoked on the UI thread    * The value returned by doInBackground() is sent to onPostExecute()    * You can call publishProgress() at anytime in doInBackground() to execute onProgressUpdate() on the UI thread    * You can cancel the task at any time, from any thread Example: public void onClick(View v) {  new DownloadImageTask().execute(" http://example.com/image.png "); } private class DownloadImageTask extends AsyncTask {     protected Bitmap doInBackground(String... urls) {         return loadImageFromNetwork(urls[0]);     }     protected void onPostExecute(Bitmap result) {         mImageView.setImageBitmap(result);     }  }
  • AsyncTask must be used by subclassing it. It is also very important to remember that an AsyncTask instance has to be created on the UI thread and can be executed only once. If you don't implement these patterns, it is good to start thinking about them, so you can design your own UIs to closely match the Google recommended patterns (the apps consistent with the general feel of the OS, can feel better, and more integrated).
  • One of the most important intents we added to Twitter for Android was the ability to sync your Twitter contacts into the phone. This integration also allowed us to give Twitter users with an Android phone the use of the QuickContact widget, which gives users a choice of ways to contact their followers. We recommend: Using this activity upon first signing into an application. If your app has no sign-in, showing this screen at first launch to improve discoverability of contact sync. The good news for developers is you get this highly functional contacts feature for free if users choose to sync contact information into your app. QuickContact for Android provides instant access to a contact's information and communication modes. For example, a user can tap a contact photo and with one more tap launch a call, SMS, or email to that person. Other applications such as Email, Messaging, and Calendar can also reveal the QuickContact widget when you touch a contact photo or status icon.
  • The dashboard pattern serves as a home orientation activity for your users. It is meant to include the categories or features of your application. We recommend including an Action bar on this screen as well. The dashboard can be static or dynamic. For example, in the case of our dashboard for Twitter, we used the goodness of Live Wallpapers introduced in 2.1 to create an animated dashboard complete with real-time trend bubbles and the Twitter bird silhouette. We recommend: Using this pattern to showcase the most prominent features of your app. Adding some unexpected delight to this screen, making it engaging without overwhelming the user. Exercising caution - for some apps, the user will want to jump directly into the meat of the application. For others, this sort of welcoming dashboard will be the right starting place.
  • Great place to brand – and use Logo as link to home (consistent with web feel) The Action bar gives your users onscreen access to the most frequently used actions in your application. We recommend you use this pattern if you want to dedicate screen real estate for common actions. Using this pattern replaces the title bar. It works with the Dashboard, as the upper left portion of the Action bar is where we recommend you place a quick link back to the dashboard or other app home screen. We recommend: Placing an Action bar at the top of the screen to house the most common actions for your application that work across all activities. Using no more than 3 onscreen actions for the Action bar. Use the main menu as overflow for actions that are less important. The balance between Action bar and main menu will ensure the richness of interaction that is Android. Making the left-hand region actionable, offering one-touch return to your dashboard or other app home.
  • The companion widget pattern is something we recommend all developers think about deeply. The widget you create should be more than a big button link into your app. Used correctly, it can provide a place on a home screen that personalizes, albeit in a small window, your application. In the case of Twitter for Android, we designed and built small and large-sized widgets to support different types of functionality. Both widgets let a user view his/her tweetstream. However, the smaller widget hands off to the application to create a reply to a tweet from the stream, whereas the larger one gives direct access to the Tweet compose activity. We recommend: Identify your application in the widget with a brand icon Make this more than a button into your app. Give your user an action or view into the app itself. Keep the activity stateful so that on exiting the application and re-entering a user is returned to the same context in the activity, minimizing the impact of using the dashboard if it is used.
  • QuickActions is our newest UI pattern. Currently, it has been implemented as a fast, engaging, popup triggered by an onscreen UI element that identifies it in as minimally disruptive way as possible. We recommend you use this pattern for list views that are data intensive where items inside the list have contextual actions associated with them. QuickActions can be used as a replacement for our traditional dialog invoked by long press. By choosing to use this pattern as part of a list, we made it easier for Twitter users to take action on the information in the list view by keeping the item and associated actions in context. We also took the extra step of making it easier to target links in list views by turning off the list view button element as a secondary component to making this pattern even more usable. This way users of Twitter for Android can view links with one tap and/or see the posted tweet on a map by tapping the tweet meta data directly. We recommend: Creating a UI element that is minimal and recognizable as an action popup; either a frame around an image or an icon in a list item. Only using this pattern in applications where the data is intensive. Placing the action popup below or above the information you wish to allow users to take relevant contextual actions on. This will make it easier to associate the actions with the content in view. Turning off the entire list view button element so that regions can be easily targeted for the user.
  • Transcript

    • 1. UI Design Patterns & Best Practices Mike Wolfson July 22, 2010
    • 2. Best Practices – Why?
      • Important to ensure your App follows the established UI guidelines, and performs well
      • -Otherwise, you will get bad reviews, and not be successful
      • -Apps that follow common design standards, will “fit” better in the Android ecosystem, and rise to the top
      • Read the UI design guidelines! http://developer.android.com/guide/practices/ui_guidelines/index.html
    • 3. Avoid the Jank
      • This occurs when the system locks out user input
      • -Makes the app feel sluggish
      • -Annoys the user
      • 100 to 200ms is the threshold beyond which users will perceive lag (or lack of "snappiness," if you will)
      • Android System Monitors:
      • - No response to an input event - 5 sec
      • - BroadcastReceiver hasn't finished executing - 10 sec
      • Resource: http://code.google.com/events/io/2010/sessions/writing-zippy-android-apps.html
    • 4. Force Close
      • If triggered:
    • 5. What happens next?
      • User hits FC button majority of the time.
      • - Goes away annoyed.
      • - Leaves Bad Market reviews
      • - Complex memory structures will
      • need to be recreated (if user even
      • decides to return to app)
      • -Non-synced data is lost
      • ” This App Sux, Force Closes, 1 star ”
      • “ Doesn’t work, uninstall”
    • 6. Single Threading Model
      • Android applications normally run entirely on a single (i.e. main) thread.
      • -This includes the drawing events
      • -This means, if system is blocked on an operation, the UI thread is blocked (and app is frozen)
      • Android UI toolkit is not thread-safe and must always be manipulated on the UI thread.
      • Potentially long running operations (ie. network or database operations, expensive calculations, etc.) should be done in a child thread
    • 7. Android Threading Rules
      • Cardinal rules of Android UI:
      • Do not block the UI thread
      • Make sure the Android UI toolkit is only accessed on the UI thread.
    • 8. Avoid FC and Jank – accessing UI thread manually
      • Android offers several ways to access the UI thread from other threads:    * Activity.runOnUiThread(Runnable)    * View.post(Runnable)    * View.postDelayed(Runnable, long)    * Handler
      • Problem:
      • -manually handling threads is error prone, and difficult
    • 9. AsyncTask
      • Goal of AsyncTask is to take care of thread management for you.
      • private class DownloadImageTask extends AsyncTask {     protected Bitmap doInBackground(String... urls) {         return loadImageFromNetwork(urls[0]);     }     protected void onPostExecute(Bitmap result) {         mImageView.setImageBitmap(result);
      • Resources: (read source code in: ShelvesActivity.java and read the docs! http://developer.android.com/reference/android/os/AsyncTask.html )
    • 10. IntentService
      • “ Set it and forget it” – places request on Service Queue, which handles action (and “may take as long as necessary”)
      • public class UpdateService extends IntentService { public class UpdateService extends IntentService {
      • To call:
      • Intent selectIntent = new Intent(this, UpdateService.class);         selectIntent.putExtra("userAction",
    • 11. Tips to keep your app “snappy”
      • If your application is doing work in the background, show the user progress is being made
      • - AsyncTask.getStatus()
      • For games specifically, do complex computations in child thread
      • If your app has time-consuming startup, offer a splash screen quickly, then fill in information as it is ready
      • -inform user about progress
      • For data sync, database queries, and other long running
      • processes - use IntentService
    • 12. Design patterns
      • Patterns describe a general solution to a common problem
      • -Natural by-product of software design lifecycle
      • Google created these patterns to establish a common UI language
      • -following them ensures your app “fits” into the Android ecosystem
      • -apps will feel more integrated, and natural to the user
      • Android patterns are designed to follow the natural way to
      • navigate the web
    • 13. Contacts Sync/ Quick Contact
      • Sync your app with Android
      • contacts
      • -do this at startup (first time
      • user runs app)
      • Make use of “QuickContact“
      • -deeper integration of app
      • with phone contacts
    • 14. Dashboard
      • Navigation center for important actions
      • Highlight only most important/commonly
      • used capabilities of application
      • Expose new content to user
      • Use this as a place to:
      • -enhance brand
      • -engage user
      • -centralize navigation
      • Make it fun!
    • 15. Action Bar
      • Dedicated real estate at top of screen
      • to support navigation, and
      • frequently used operations
      • Replaces Title Bar
      • Do use to bring key actions to front
      • Do use to convey sense of place
      • Make consistent between activities
      • -Don’t use for contextual actions
      • Provide link to home (dashboard)
    • 16. Companion Widget
      • Supports the app by displaying its content on the home screen
      • Only use to provide extra value -not just a simple link to start your application
      • Do handoff to app for detailed
      • views or complicated tasks
      • Be space efficient
      • -keep your widget small
    • 17. QuickAction
      • Popup triggered from distinct visual
      • target
      • Minimally disruptive to screen context
      • Straightforward, Fast and Fun
      • Present only most important actions
      • Use when item doesn’t have meaningful
      • detail view
      • Don’t use in contexts that support
      • multiple selection
      • Resource: http://code.google.com/p/simple-quickactions/
    • 18. Conclusion
      • Avoid the Jank (make sure your application is responsive)
      • - Don’t block the UI thread
      • - Offload long running tasks to background threads
      • -Using AsyncTask or ServiceIntent
      • -Keep user notified about progress
      • Ensure your app “fits” in the Android ecosystem by following established UI design patterns
      • Questions?

    ×