| Scott Main | 65e62f4 | 2010-09-20 12:46:34 -0700 | [diff] [blame] | 1 | page.title=Best Practices for Web Apps |
| 2 | @jd:body |
| 3 | |
| 4 | <style> |
| 5 | .bold li { |
| 6 | font-weight:bold; |
| 7 | } |
| 8 | .bold li * { |
| 9 | font-weight:normal; |
| 10 | } |
| 11 | </style> |
| 12 | |
| 13 | <p>Developing web pages and web applications for mobile devices presents a different set of |
| 14 | challenges compared to developing a web page for the typical |
| 15 | desktop web browser. To help you get started, the following is a list of practices you should |
| 16 | follow in order to provide the most effective web application for Android and other mobile |
| 17 | devices.</p> |
| 18 | |
| 19 | <ol class="bold"> |
| 20 | |
| 21 | <li>Redirect mobile devices to a dedicated mobile version of your web site |
| 22 | <p>There are several ways you can redirect requests to the mobile version of your web site, using |
| 23 | server-side redirects. Most often, this is done by "sniffing" the User Agent |
| 24 | string provided by the web browser. To determine whether to serve a mobile version of your site, you |
| 25 | should simply look for the "mobile" string in the User Agent, which matches a wide variety of mobile |
| 26 | devices. If necessary, you can also identify the specific operating system in the User Agent string |
| 27 | (such as "Android 2.1").</p> |
| Scott Main | 2458f2a | 2011-02-02 11:24:56 -0800 | [diff] [blame] | 28 | <p class="note"><strong>Note:</strong> Large screen Android-powered devices that should be served |
| 29 | full-size web sites (such as tablets) do <em>not</em> include the "mobile" string in the user agent, |
| 30 | while the rest of the user agent string is mostly the same. As such, it's important that you deliver |
| 31 | the mobile version of your web site based on whether the "mobile" string exists in the user |
| 32 | agent.</p> |
| Scott Main | 65e62f4 | 2010-09-20 12:46:34 -0700 | [diff] [blame] | 33 | </li> |
| 34 | |
| 35 | |
| 36 | <li>Use a valid markup DOCTYPE that's appropriate for mobile devices |
| 37 | <p>The most common markup language used for mobile web sites is <a |
| 38 | href="http://www.w3.org/TR/2008/REC-xhtml-basic-20080729/">XHTML Basic</a>. This standard |
| 39 | ensures specific markup for your web site that works best on mobile devices. For instance, it does |
| 40 | not allow HTML frames or nested tables, which perform poorly on mobile devices. Along with the |
| 41 | DOCTYPE, be sure to declare the appropriate character encoding for the document (such as |
| 42 | UTF-8).</p> |
| 43 | <p>For example:</p> |
| 44 | <pre> |
| 45 | <?xml version="1.0" encoding="UTF-8"?> |
| 46 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML Basic 1.1//EN" |
| 47 | "http://www.w3.org/TR/xhtml-basic/xhtml-basic11.dtd"> |
| 48 | </pre> |
| 49 | |
| 50 | <p>Also be sure that your web page markup is valid against the declared DOCTYPE. Use a |
| 51 | validator, such as the one available at |
| 52 | <a href="http://validator.w3.org/">http://validator.w3.org</a>.</p> |
| 53 | </li> |
| 54 | |
| 55 | |
| 56 | <li>Use viewport meta data to properly resize your web page |
| 57 | <p>In your document {@code <head>}, you should provide meta data that specifies how you |
| 58 | want the browser's viewport to render your web page. For example, your viewport meta data can |
| 59 | specify the height and width for the browser's viewport, the initial web page scale and even the |
| 60 | target screen density.</p> |
| 61 | <p>For example:</p> |
| 62 | <pre> |
| 63 | <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> |
| 64 | </pre> |
| 65 | <p>For more information about how to use viewport meta data for Android-powered devices, read <a |
| Scott Main | 08810efc | 2010-10-11 15:37:14 -0700 | [diff] [blame] | 66 | href="{@docRoot}guide/webapps/targeting.html">Targeting Screens from Web Apps</a>.</p> |
| Scott Main | 65e62f4 | 2010-09-20 12:46:34 -0700 | [diff] [blame] | 67 | </li> |
| 68 | |
| 69 | |
| 70 | <li>Avoid multiple file requests |
| 71 | <p>Because mobile devices typically have a connection speed far slower than a desktop |
| 72 | computer, you should make your web pages load as fast as possible. One way to speed it up is to |
| 73 | avoid loading extra files such as stylesheets and script files in the {@code |
| 74 | <head>}. Instead, provide your CSS and JavaScript directly in the <head> (or |
| 75 | at the end of the <body>, for scripts that you don't need until the page is loaded). |
| 76 | Alternatively, you should optimize the size and speed of your files by compressing them with tools |
| 77 | like <a href="http://code.google.com/p/minify/">Minify</a>.</p> |
| 78 | </li> |
| 79 | |
| 80 | |
| 81 | <li>Use a vertical linear layout |
| 82 | <p>Avoid the need for the user to scroll left and right while navigating your web |
| 83 | page. Scrolling up and down is easier for the user and makes your web page simpler.</p> |
| 84 | </li> |
| 85 | |
| 86 | </ol> |
| 87 | |
| 88 | <p>For a more thorough guide to creating great mobile web applications, see the W3C's <a |
| 89 | href="http://www.w3.org/TR/mobile-bp/">Mobile Web Best Practices</a>. For other guidance on |
| 90 | improving the speed of your web site (for mobile and desktop), see Yahoo!'s guide to <a |
| 91 | href="http://developer.yahoo.com/performance/index.html#rules">Exceptional Performance</a> and |
| 92 | Google's speed tutorials in <a href="http://code.google.com/speed/articles/">Let's make the web |
| 93 | faster</a>.</p> |
| 94 | |
| 95 | |