Theo - 2 months ago 24

Android Question

Let us suppose that we have a tablet with the following characteristics:

**7 inches**

**1280 * 800 px**

**213dpi**.

From this we can calculate the resolution of the device in dp.

So:

1280 = dp * (213 / 160)

dp = 1280 * 160 / 213 = 961.50 ~= 960

and

800 = dp * (213 / 160)

dp = 800 * 160 / 213 = 600.94 ~= 600

hence its resolution is: **960 x 600dp**. Thus we use the **layout-sw600dp**.

Now let us suppose we have a mobile device ie Samsung Galaxy 4.

**5.0 inches**

**1080 x 1920 px**

**441 dpi**.

We do the same calculations.

1080 = dp * (441 / 160)

dp = 1080 * 160/ 441= 391.83 ~= 390

and

1920 = dp * (441 / 160)

dp = 1920 * 160/ 441 = 696.59 ~= 696

So in this case **390 x 696 dp**. Are we going to use **layout -sw480dp** or **layout -sw320dp** for the mobile device?

Thanks,

Theo

Answer

'sw' means smallest width, so it should be layout-sw320dp.

The device's smallestWidth is the shortest of the screen's available height and width (you may also think of it as the "smallest possible width" for the screen). You can use this qualifier to ensure that, regardless of the screen's current orientation, your application's has at least dps of width available for its UI.

For example, if your layout requires that its smallest dimension of screen area be at least 600 dp at all times, then you can use this qualifier to create the layout resources, res/layout-sw600dp/. The system will use these resources only when the smallest dimension of available screen is at least 600dp, regardless of whether the 600dp side is the user-perceived height or width. The smallestWidth is a fixed screen size characteristic of the device; the device's smallestWidth does not change when the screen's orientation changes.

(Above taken from documentation). Hope it helps !