Download presentation
Presentation is loading. Please wait.
1
UI Design Patterns & Best Practices
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!
3
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 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.
4
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. 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.
5
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
6
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! ) 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(" } private class DownloadImageTask extends AsyncTask { protected Bitmap doInBackground(String... urls) { return loadImageFromNetwork(urls[0]); } protected void onPostExecute(Bitmap result) { mImageView.setImageBitmap(result); } }
7
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", IntentService is a base class for Services that handle asynchronous requests (expressed as Intents) 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.
8
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 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(" } private class DownloadImageTask extends AsyncTask { protected Bitmap doInBackground(String... urls) { return loadImageFromNetwork(urls[0]); } protected void onPostExecute(Bitmap result) { mImageView.setImageBitmap(result); } }
9
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 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).
10
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 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 to that person. Other applications such as , Messaging, and Calendar can also reveal the QuickContact widget when you touch a contact photo or status icon.
11
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! 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.
12
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) 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.
13
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 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.
14
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: 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.
15
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?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.