user1526659
user1526659

Reputation:

Defining layout.xml files based on screen density (v.2.3.3+)

Afaik resolution is concerned with density (correct me if I'm wrong) and my goal is to use one of three layouts according to device's density. What're my options with 2.3.3 (most popular atm) sdk version ? Thank you.

Upvotes: 0

Views: 2500

Answers (3)

AndroidNoob
AndroidNoob

Reputation: 2821

Take a look at the android documentation about supporting multiple screens - the gist is that you can have multiple folders with different naming conventions that target devices based on a whole array of things (screen density, screen size, portrait/landscape etc) and they can be as vague or as detailed as you want. For example if you wanted to target all xhdpi devices you make a layout folder called 'layout-xhdpi', make a layout inside it and all xhdpi devices will use that over any other. If you wanted to be more specific and for example target the Galaxy Nexus specifically you can create a folder named 'layout-w360dp-port-xhdpi'.

Another way which I prefer to do is have a single layout file and have multiple 'values' xml files targeting the different screen sizes, and in the values files change the values taken in for padding/heights and point to these values in your layout. e.g. in 'values/dimensions.xml'

<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="webview_height">53dip</dimen>
<resources>

then in your layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:orientation="vertical" >
<WebView 
    android:layout_height="@dimen/webview_height"
    android:id="@+id/mainWebViewMobile"
    android:layout_width="match_parent"
    ></WebView>
</LinearLayout>

and you can create multiple dimensions.xml in separate folders targeting different devices (e.g. 'values-xhdpi/dimensions.xml' or 'values-w360dp-port-xhdpi/dimensions.xml') and alter the value of 'webview_height'.

Upvotes: 1

Ollie C
Ollie C

Reputation: 28519

Not really.

There are two primary aspects to consider:

  1. Screen density. ldpi, mdpi, hdpi, xhdpi. This is the number of pixels per inch. It relates to the size of the pixels (not the screen).
  2. Screen size. small, normal, large, x-large - or specified by pixel size. This relates to the actual physical size.

How you work with them depends on your project's needs. Typically you would provide images in various densities (so icons etc look good on all devices), but if you're building an app to work well on devices of very different sizes (small phones through to 10" tablets) you should also provide layouts for the various sizes of screen - perhaps one for phones, one for 7" tablets, and one for 10" tablets.

http://developer.android.com/guide/practices/screens_support.html

Upvotes: 1

julian
julian

Reputation: 381

Just create a different layout for each density and place it in appropriate folder

See this from android docs: http://developer.android.com/guide/practices/screens_support.html#qualifiers

res/layout/my_layout.xml             // layout for normal screen size ("default")
res/layout-small/my_layout.xml       // layout for small screen size
res/layout-large/my_layout.xml       // layout for large screen size
res/layout-xlarge/my_layout.xml      // layout for extra large screen size
res/layout-xlarge-land/my_layout.xml // layout for extra large in landscape orientation

res/drawable-mdpi/my_icon.png        // bitmap for medium density
res/drawable-hdpi/my_icon.png        // bitmap for high density
res/drawable-xhdpi/my_icon.png       // bitmap for extra high density

Upvotes: 0

Related Questions