Home » Android » android – Conversion from px to dp for Google nexus 7

android – Conversion from px to dp for Google nexus 7

Posted by: admin June 15, 2020 Leave a comment

Questions:

I am new to tablet application development. I am having Google Nexus7 with screen resolution (600 * 960 dip) So I want to know is it fall in to mdip category?
According to that 1px = 1dp (baseline). But If I follow this its not looks good on tablet. I am creating separate layout folder for nexus7 -sw600dp and mention sizes according to mdip but it not working. My images also not looking good. What I want to know what is actual conversion rate for google nexus 7. Need help.

How to&Answers:

Formula: pixels = dips * (density / 160)

The Nexus 7 is 800×1280 with a 213 px density, which means it’s resolution code is tvdpi (which means you can have a folder called drawable-tvdpi).

You can measure available screen size, minus window decorations, with:

this.getResources().getConfiguration().screenWidthDp;
this.getResources().getConfiguration().screenHeightDp;

On my N7, it returns 600 dp w, 888 dp h. Following the above formula, 888 dp height is 1280px – window decoration.

Answer:

The Nexus7 is a unique device, with a somewhat strange dpi structure.

For nexus 7

layout-large-hdpi

Here is a very good explanation (from Dianne Hackborn – an Android engineer at Google): Dianne Hackborn explains the unique resolution of the Nexus7

Note: The app takes images from these folders only if you have not given higher precedence qualifiers. For example if you have given a layout folder like layout-sw360dp the app will take only the images from this folder even if you have given separate layouts like the one I said above. Because in android there is an order of precedence in which you have to give layouts.

Screen Density

Commonly referred to as dpi (dots per inch). Android groups all actual screen densities into four generalized densities: low (120), medium (160), high (240), and extra high (320). A device such as Galaxy Nexus has “extra high” screen density (more specifically, the dpi value is set at 320). The Nexus 7 uses “tvdpi” – i.e. 213 dpi.

Density Independent Pixel

Commonly referred to as dp. This is the virtual pixel unit used when displaying content. The density-independent pixel is equivalent to one physical pixel on a 160 dpi screen. To calculate dp use the following formula:

px = dp * (dpi / 160)

or equivalently:

dp = (px / dpi) * 160

The reason Nexus 7 can show more content than the Galaxy Nexus despite having similar resolutions is this: the dpi of Nexus 7 is lower than Galaxy Nexus.

Galaxy Nexus (320 dpi, 720 pixels wide)

(720 / 320) * 160 = 360 dp

Nexus 7 (213 dpi, 800 pixels wide)

(800 / 213) * 160 = 600 dp

This means that when apps are rendering on the Galaxy Nexus, the width of the screen is actually 360 dp (rendered using 720 pixels). Whereas on the Nexus 7, the width of the screen is 600 dp (rendered using 800 pixels).

Answer:

Try below code, give pixel value to below method it will give you result in dp.

public int convertSizeToDeviceDependent(int value) {
            DisplayMetrics dm = new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getMetrics(dm);
            return ((dm.densityDpi * value) / 160);
        }