0

I have developed an Android app with an Emulator (5.4") from Eclipse.

I have tested this app and everything works well, but I want to adapt my app for different smartphone's sizes and for different tablet's sizes. I have created some folders 'values' with differents suffixes like '-ldpi' '-mdpi' '-hdpi' '-xhdpi' which contain a 'dimens.xml' file with the amounts in 'dp' for each element from layouts.

Moreover, I have an pdf with the amounts in pixels which I must use for each element, but I don't know how use them. This design that I must use is in 1280x720 px but I don't know how adapt those amounts in pixels for each 'dimens.xml' file (ldpi, mdpi, hdpi, xhdpi) in my app.

For example, if the design has 720 px for width and a button has 50 px, how many 'dp' I must put in each 'dimens' file according to the density?

I have found some webs app that it converts me the amounts in dp from one density to other, but I don't know how to adapt the pixels to dp and what density I must use like reference ...

I hope you can help me!

Thanks xD

EDIT:

I managed to get a good result for my smartphone is XHDPI. Now for the emulator is MDPI I put in 'values-mdpi/dimens.xml' the dp (x2) respect to XHPDI following this rule:

Density:

ldpi | mdpi | tvdpi | hdpi | xhdpi | xxhdpi | xxxhdpi

0.75 | 1 | 1.33 | 1.5 | 2 | 3 | 4

float dp = somePxValue / density;

but the visual result is not the same, is not correct. Why¿?

I've been testing, and to get the same aspect of the app in the emulator MDPI than in my smartphone XHDPI must follow the following relationship:

dp (MDPI) = dp (XHDPI) x 1.35

Can anyone explain to me why this is so?

KryNaC
  • 379
  • 4
  • 21
  • I had a same problem for last few months. And i did follow this. http://stackoverflow.com/a/2025541/1602281 – rahultheOne Mar 02 '15 at 16:48
  • You may want to try this converter: http://labs.rampinteractive.co.uk/android_dp_px_calculator/ Or check out this stackoverflow post: http://stackoverflow.com/questions/4605527/converting-pixels-to-dp – esme_louise Mar 02 '15 at 16:49
  • Follow the `0.75:1:1.5:2:3:4` rule for scale factors. 1 being **mdpi** – Phantômaxx Mar 02 '15 at 16:52
  • I already know all this ... I want to know how to adapt the pixels to dp in the example that I mentioned. If I have a button with 72 px for width and screen has 720 px in my design ... if I put 72 dp in MDPI, the button has not 10% of width screen in my emulator =( – KryNaC Mar 02 '15 at 17:02
  • You are confusing `resolution` with `density` – Phantômaxx Mar 02 '15 at 17:21
  • Probably but, it is not true that in MDPI pixel = dp ?? Then, a button with 72 dp in mdpi it would must have the same aspect ratio that my design not? 10% of width screen ... – KryNaC Mar 02 '15 at 17:55
  • Anyone can give me some light in this subject ... =( – KryNaC Mar 05 '15 at 18:11

1 Answers1

1
private int pxToDp(int px)
{
    float density = getApplicationContext().getResources().getDisplayMetrics().density;
    return Math.round((float)px / density);
}
malkomich
  • 311
  • 4
  • 13