page.title=Supporting Different Screens in Web Apps @jd:body
Because Android is available on devices with a variety of screen sizes and pixel densities, you should account for these factors in your web design so your web pages always appear at the appropriate size.
When targeting your web pages for Android devices, there are two major factors that you should account for:
The viewport is the area in which your web page is drawn. Although the viewport's total visible area matches the size of the screen when zoomed all the way out, the viewport has its own pixel dimensions that it makes available to a web page. For example, although a device screen might have physical a width of 480 pixels, the viewport can have a width of 800 pixels. This allows a web page designed at 800 pixels wide to be completely visible on the screen when the viewport scale is 1.0. Most web browsers on Android (including Chrome) set the viewport to a large size by default (known as "wide viewport mode" at about 980px wide). Many browsers also zoom out as far as possible, by default, to show the full viewport width (known as "overview mode").
Note: When your page is rendered in a {@link android.webkit.WebView}, it does not use wide viewport mode (the page appears at full zoom) by default. You can enable wide viewport mode with {@link android.webkit.WebSettings#setUseWideViewPort setUseWideViewPort()}.
You can define properties of the viewport for your web page, such as the width and initial zoom level, using the {@code <meta name="viewport" ...>} tag in your document {@code <head>}.
The following syntax shows all of the supported viewport properties and the types of values accepted by each one:
<meta name="viewport" content=" height = [pixel_value | "device-height"] , width = [pixel_value | "device-width"] , initial-scale = float_value , minimum-scale = float_value , maximum-scale = float_value , user-scalable = ["yes" | "no"] " />
For example, the following {@code <meta>} tag specifies that the viewport width should exactly match the device screen's width and that the ability to zoom should be disabled:
<head> <title>Example</title> <meta name="viewport" content="width=device-width, user-scalable=no" /> </head>
When optimizing your site for mobile devices, you should usually set the width to {@code "device-width"} so the size fits exactly on all devices, then use CSS media queries to flexibly adapt layouts to suit different screen sizes.
Note: You should disable user scaling only when you're certain that your web page layout is flexible and the content will fit the width of small screens.
The Android Browser and {@link android.webkit.WebView} support a CSS media feature that allows
you to create styles for specific
screen densities—the -webkit-device-pixel-ratio
CSS media feature. The
value you apply to this feature should be either
"0.75", "1", or "1.5", to indicate that the styles are for devices with low density, medium density,
or high density screens, respectively.
For example, you can create separate stylesheets for each density:
<link rel="stylesheet" media="screen and (-webkit-device-pixel-ratio: 1.5)" href="hdpi.css" /> <link rel="stylesheet" media="screen and (-webkit-device-pixel-ratio: 1.0)" href="mdpi.css" />
Or, specify the different styles in one stylesheet:
#header { background:url(medium-density-image.png); } @media screen and (-webkit-device-pixel-ratio: 1.5) { /* CSS for high-density screens */ #header { background:url(high-density-image.png); } } @media screen and (-webkit-device-pixel-ratio: 0.75) { /* CSS for low-density screens */ #header { background:url(low-density-image.png); } }
For more information about handling different screen densities, especially images, see High DPI Images for Variable Pixel Densities.
The Android Browser and {@link android.webkit.WebView} support a DOM property that allows you to
query the density of the current
device—the window.devicePixelRatio
DOM property. The value of this property
specifies the scaling factor used for the current device. For example, if the value
of window.devicePixelRatio
is "1.0", then the device is considered a medium density
device and no scaling is applied by default; if the value is "1.5", then the device is
considered a high density device and the page is scaled 1.5x by default; if the value
is "0.75", then the device is considered a low density device and the page is scaled
0.75x by default. Of course, the scaling that the Android Browser and {@link android.webkit.WebView}
apply is based on the web page's
target density—as described in the section about Defining the
viewport target density, the default target is medium-density, but you can change the
target to affect how your web page is scaled for different screen densities.
For example, here's how you can query the device density with JavaScript:
if (window.devicePixelRatio == 1.5) { alert("This is a high-density screen"); } else if (window.devicePixelRatio == 0.75) { alert("This is a low-density screen"); }