Conversion from px to dp for Google nexus 7

nilkash picture nilkash · Oct 8, 2013 · Viewed 11.4k times · Source

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.

Answer

Mike P. picture Mike P. · Oct 8, 2013

Formula: pixels = dips * (density / 160)

The Nexus 7 is 800x1280 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.