0
votes

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?

1
I had a same problem for last few months. And i did follow this. stackoverflow.com/a/2025541/1602281rahultheOne
You may want to try this converter: labs.rampinteractive.co.uk/android_dp_px_calculator Or check out this stackoverflow post: stackoverflow.com/questions/4605527/converting-pixels-to-dpesme_louise
Follow the 0.75:1:1.5:2:3:4 rule for scale factors. 1 being mdpiPhantômaxx
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
You are confusing resolution with densityPhantômaxx

1 Answers

1
votes
private int pxToDp(int px)
{
    float density = getApplicationContext().getResources().getDisplayMetrics().density;
    return Math.round((float)px / density);
}