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