{"id":353028,"date":"2010-02-23T09:00:14","date_gmt":"2010-02-23T14:00:14","guid":{"rendered":"http:\/\/www.androidguys.com\/?p=13624"},"modified":"2010-02-23T09:00:14","modified_gmt":"2010-02-23T14:00:14","slug":"handling-multiple-screen-sizes-part-three","status":"publish","type":"post","link":"https:\/\/mereja.media\/index\/353028","title":{"rendered":"Handling Multiple Screen Sizes, Part Three"},"content":{"rendered":"<div class=\"KonaBody\">\n<p class=\"first-child \"><em><a rel=\"attachment wp-att-10675\" href=\"http:\/\/www.androidguys.com\/2009\/11\/09\/return-of-the-fancy-listviews\/buildingdroids\/\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-full wp-image-10675\" title=\"BuildingDroids\" src=\"http:\/\/www.androidguys.com\/wp-content\/uploads\/BuildingDroids.jpg\" alt=\"\" width=\"225\" height=\"150\" \/><\/a>This is the first part of a several part series on handling multiple screen sizes in your Android projects. This material is adapted from a chapter in <a href=\"http:\/\/commonsware.com\/Android\/\">The Busy Coder&#8217;s Guide to Android Development, Version 3.0<\/a>.<\/em><\/p>\n<p>When designing your app, there will be times when you want to have different looks     or behaviors based upon screen size or density. Android has ways for you     to switch out resources or code blocks based on the environment in which     your application runs. When properly used in combination with the techniques described in the <a href=\"http:\/\/www.androidguys.com\/2010\/02\/18\/handling-multiple-screen-sizes-part-2\/\">preceding post<\/a>, achieving screen size- and density-independence is eminently     possible, at least for devices running Android 1.6 and newer.<\/p>\n<h2>&lt;supports-screens&gt;<\/h2>\n<p>The first step to proactively supporting screen sizes is to add       the <span style=\"font-family: Consolas,courier;\">&lt;supports-screens&gt;<\/span> element to your       <span style=\"font-family: Consolas,courier;\">AndroidManifest.xml<\/span> file. This specifies which screen sizes       you explicitly support and which you do not. Those that you do not will       be handled by the automatic &#8220;compatibility mode&#8221;.<\/p>\n<p>Here is a manifest containing a       <span style=\"font-family: Consolas,courier;\">&lt;supports-screens&gt;<\/span> element:<\/p>\n<p><a rel=\"attachment wp-att-13626\" href=\"http:\/\/www.androidguys.com\/2010\/02\/23\/handling-multiple-screen-sizes-part-3\/src1\/\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-13626\" title=\"Sample Source Code\" src=\"http:\/\/www.androidguys.com\/wp-content\/uploads\/2010\/02\/src1.jpg\" alt=\"\" width=\"439\" height=\"288\" \/><\/a>Three of these attributes are almost self-explanatory:       <span style=\"font-family: Consolas,courier;\">android:smallScreens<\/span>, <span style=\"font-family: Consolas,courier;\">android:normalScreens<\/span>,       and <span style=\"font-family: Consolas,courier;\">android:largeScreens<\/span> each take a boolean value       indicating if your application explicitly supports those screens       (<span style=\"font-family: Consolas,courier;\">true<\/span>) or requires &#8220;compatibility mode&#8221; assistance       (<span style=\"font-family: Consolas,courier;\">false<\/span>).<\/p>\n<p>The <span style=\"font-family: Consolas,courier;\">android:anyDensity<\/span> attribute indicates whether       you are taking density into account in your calculations       (<span style=\"font-family: Consolas,courier;\">true<\/span>) or not (<span style=\"font-family: Consolas,courier;\">false<\/span>). If <span style=\"font-family: Consolas,courier;\">false<\/span>,       Android will pretend as though all of your dimensions (e.g.,       <span style=\"font-family: Consolas,courier;\">4px<\/span>) are for a normal-density (160dpi) screen. If your       application is running on a screen with lower or higher density, Android       will scale your dimensions accordingly. If you indicate that       <span style=\"font-family: Consolas,courier;\">android:anyDensity = &#8220;true&#8221;<\/span>, you are telling Android not to       do that, putting the onus on you to use density-independent units, such       as <span style=\"font-family: Consolas,courier;\">dip<\/span>, <span style=\"font-family: Consolas,courier;\">mm<\/span>, or <span style=\"font-family: Consolas,courier;\">in<\/span>.<\/p>\n<h2>Resources and Resource Sets<\/h2>\n<p>The primary way to &#8220;toggle&#8221; different things based on screen size       or density is to create resource       sets. By creating resource sets that are specific to different       device characteristics, you teach Android how to render each, with       Android switching among those sets automatically.<\/p>\n<h3>Default Scaling<\/h3>\n<p>By default, Android will scale all drawable resources. Those         that are intrinsically scalable, as described in the previous section, will scale nicely.         Ordinary bitmaps will be scaled just using a normal scaling algorithm,         which may or may not give you great results. It also may slow things         down a bit. If you wish to avoid this, you will need to set up         separate resource sets containing your non-scalable bitmaps.<\/p>\n<h3>Density-Based Sets<\/h3>\n<p>If you wish to have different layouts, dimensions, or the like         based upon different screen densities, you can use the         <span style=\"font-family: Consolas,courier;\">-ldpi<\/span>, <span style=\"font-family: Consolas,courier;\">-mdpi<\/span>, and <span style=\"font-family: Consolas,courier;\">-hdpi<\/span> resource set labels. For example,         <span style=\"font-family: Consolas,courier;\">res\/values-hdpi\/dimens.xml<\/span> would contain dimensions used         in high-density devices.<\/p>\n<h3>Size-Based Sets<\/h3>\n<p>Similarly, if you wish to have different resource sets based         upon screen size, Android offers <span style=\"font-family: Consolas,courier;\">-small<\/span>,         <span style=\"font-family: Consolas,courier;\">-normal<\/span>, and <span style=\"font-family: Consolas,courier;\">-large<\/span> resource set labels.         Creating <span style=\"font-family: Consolas,courier;\">res\/layout-large-land\/<\/span> would indicate layouts to         use on large screens (e.g., WVGA) in landscape orientation.<\/p>\n<h3>Version-Based Sets<\/h3>\n<p>There may be times when earlier versions of Android get confused         by newer resource set labels. To help with that, you can include a         version label to your resource set, of the form <span style=\"font-family: Consolas,courier;\">-vN<\/span>,         where <span style=\"font-family: Consolas,courier;\">N<\/span> is an API         level. Hence, <span style=\"font-family: Consolas,courier;\">res\/drawable-large-v4\/<\/span> indicates         these drawables should be used on large screens at API level 4         (Android 1.6) and newer.<\/p>\n<p>Apparently, Android has had the ability to filter on version         from early on, and so this technique will work going back to Android         1.5 (and, perhaps, earlier).<\/p>\n<p>So, if you find that Android 1.5 emulators or devices are         grabbing the wrong resource sets, consider adding <span style=\"font-family: Consolas,courier;\">-v4<\/span> to         their resource set names to filter them out.<\/p>\n<h2>Finding Your Size<\/h2>\n<p>If you need to take different actions in your Java code based on       screen size or density, you have a few options.<\/p>\n<p>If there is something distinctive in your resource sets, you can       &#8220;sniff&#8221; on that and branch accordingly in your code. For example, as       will be seen in the code sample at the end of this chapter, you can have       extra widgets in some layouts (e.g.,       <span style=\"font-family: Consolas,courier;\">res\/layout-large\/main.xml<\/span>) \u2014 simply seeing if an extra       widget exists will tell you if you are running a &#8220;large&#8221; screen or       not.<\/p>\n<p>You can also find out your screen size class via a       <span style=\"font-family: Consolas,courier;\">Configuration<\/span> object, typically obtained by an       <span style=\"font-family: Consolas,courier;\">Activity<\/span> via       <span style=\"font-family: Consolas,courier;\">getResources().getConfiguration()<\/span>. A       <span style=\"font-family: Consolas,courier;\">Configuration<\/span> object has a public field named       <span style=\"font-family: Consolas,courier;\">screenLayout<\/span> that is a bitmask indicating the type of       screen the application is running on. You can test to see if your screen       is small, normal, or large, or if it is &#8220;long&#8221; or not (where &#8220;long&#8221;       indicates a 16:9 or similar aspect ratio, compared to 4:3). For example,       here we test to see if we are running on a large screen:<\/p>\n<p><a rel=\"attachment wp-att-13627\" href=\"http:\/\/www.androidguys.com\/2010\/02\/23\/handling-multiple-screen-sizes-part-3\/src2\/\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-13627\" title=\"Source Code Sample #2\" src=\"http:\/\/www.androidguys.com\/wp-content\/uploads\/2010\/02\/src2.jpg\" alt=\"\" width=\"310\" height=\"112\" \/><\/a><\/p>\n<p>There does not appear to be an easy way to find out your screen       density in a similar fashion. If you absolutely need to know that, a       &#8220;hack&#8221; would be to create <span style=\"font-family: Consolas,courier;\">res\/values-ldpi\/<\/span>,       <span style=\"font-family: Consolas,courier;\">res\/values-mdpi\/<\/span>, and <span style=\"font-family: Consolas,courier;\">res\/values-hdpi\/<\/span> directories in your project, and add a <span style=\"font-family: Consolas,courier;\">strings.xml<\/span> file to       each. Put a string resource in <span style=\"font-family: Consolas,courier;\">strings.xml<\/span> that is has a       common name across all three resource sets and has a distinctive value       (e.g., name it <span style=\"font-family: Consolas,courier;\">density<\/span>, with values of <span style=\"font-family: Consolas,courier;\">ldpi<\/span>,       <span style=\"font-family: Consolas,courier;\">mdpi<\/span>, and <span style=\"font-family: Consolas,courier;\">hdpi<\/span>, respectively). Then, test the       value of the string resource at runtime. This is inelegant but should       work.<\/p>\n<p>The next post in the series will look at ways to help you better use the emulator, and real devices, for testing these different screen sizes and densities.<\/p>\n<\/div>\n<h3  class=\"related_post_title\">Might We Suggest&#8230;<\/h3>\n<ul class=\"related_post\">\n<li><a href=\"http:\/\/www.androidguys.com\/2010\/02\/18\/handling-multiple-screen-sizes-part-2\/\" title=\"Handling Multiple Screen Sizes, Part Two\">Handling Multiple Screen Sizes, Part Two<\/a><br \/><small>Learn how to handle multiple screen sizes within your Android project, in today&#8217;s part of an ongoing series of posts, in today&#8217;s episode of Building &#8216;Droids!&#8230;<\/small><\/li>\n<\/ul>\n<p><a href=\"http:\/\/feedads.g.doubleclick.net\/~a\/XmgpNge4TH9-AEEn_yCxQGSur-U\/0\/da\"><img decoding=\"async\" src=\"http:\/\/feedads.g.doubleclick.net\/~a\/XmgpNge4TH9-AEEn_yCxQGSur-U\/0\/di\" border=\"0\" ismap=\"true\"><\/img><\/a><br \/>\n<a href=\"http:\/\/feedads.g.doubleclick.net\/~a\/XmgpNge4TH9-AEEn_yCxQGSur-U\/1\/da\"><img decoding=\"async\" src=\"http:\/\/feedads.g.doubleclick.net\/~a\/XmgpNge4TH9-AEEn_yCxQGSur-U\/1\/di\" border=\"0\" ismap=\"true\"><\/img><\/a><\/p>\n<div class=\"feedflare\">\n<a href=\"http:\/\/feeds.feedburner.com\/~ff\/androidguyscom?a=KO9RK3fywks:ztTCy-yFTKc:D7DqB2pKExk\"><img decoding=\"async\" src=\"http:\/\/feeds.feedburner.com\/~ff\/androidguyscom?i=KO9RK3fywks:ztTCy-yFTKc:D7DqB2pKExk\" border=\"0\"><\/img><\/a> <a href=\"http:\/\/feeds.feedburner.com\/~ff\/androidguyscom?a=KO9RK3fywks:ztTCy-yFTKc:gIN9vFwOqvQ\"><img decoding=\"async\" src=\"http:\/\/feeds.feedburner.com\/~ff\/androidguyscom?i=KO9RK3fywks:ztTCy-yFTKc:gIN9vFwOqvQ\" border=\"0\"><\/img><\/a> <a href=\"http:\/\/feeds.feedburner.com\/~ff\/androidguyscom?a=KO9RK3fywks:ztTCy-yFTKc:yIl2AUoC8zA\"><img decoding=\"async\" src=\"http:\/\/feeds.feedburner.com\/~ff\/androidguyscom?d=yIl2AUoC8zA\" border=\"0\"><\/img><\/a> <a href=\"http:\/\/feeds.feedburner.com\/~ff\/androidguyscom?a=KO9RK3fywks:ztTCy-yFTKc:qj6IDK7rITs\"><img decoding=\"async\" src=\"http:\/\/feeds.feedburner.com\/~ff\/androidguyscom?d=qj6IDK7rITs\" border=\"0\"><\/img><\/a> <a href=\"http:\/\/feeds.feedburner.com\/~ff\/androidguyscom?a=KO9RK3fywks:ztTCy-yFTKc:6swKnRFnXgo\"><img decoding=\"async\" src=\"http:\/\/feeds.feedburner.com\/~ff\/androidguyscom?i=KO9RK3fywks:ztTCy-yFTKc:6swKnRFnXgo\" border=\"0\"><\/img><\/a>\n<\/div>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/feeds.feedburner.com\/~r\/androidguyscom\/~4\/KO9RK3fywks\" height=\"1\" width=\"1\"\/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is the first part of a several part series on handling multiple screen sizes in your Android projects. This material is adapted from a chapter in The Busy Coder&#8217;s Guide to Android Development, Version 3.0. When designing your app, there will be times when you want to have different looks or behaviors based upon [&hellip;]<\/p>\n","protected":false},"author":703,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-353028","post","type-post","status-publish","format-standard","hentry","category-news"],"_links":{"self":[{"href":"https:\/\/mereja.media\/index\/wp-json\/wp\/v2\/posts\/353028","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mereja.media\/index\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mereja.media\/index\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mereja.media\/index\/wp-json\/wp\/v2\/users\/703"}],"replies":[{"embeddable":true,"href":"https:\/\/mereja.media\/index\/wp-json\/wp\/v2\/comments?post=353028"}],"version-history":[{"count":0,"href":"https:\/\/mereja.media\/index\/wp-json\/wp\/v2\/posts\/353028\/revisions"}],"wp:attachment":[{"href":"https:\/\/mereja.media\/index\/wp-json\/wp\/v2\/media?parent=353028"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mereja.media\/index\/wp-json\/wp\/v2\/categories?post=353028"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mereja.media\/index\/wp-json\/wp\/v2\/tags?post=353028"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}