14

All the different resolutions of the different Android products are driving me nuts.

My first android app that I wrote was designed so it supported the three commonly used resolutions: 240x320 (LDPI), 320x480 (MDPI) and 480x800 (HDPI). The 480x854 didn't do any harm to the layout because it has the same width as 480x800.

I've also bought the following devices to test my android apps on: Samsung Galaxy Europe (LDPI) HTC Desire Z (HDPI)

Luckily my girlfriend has a HTC Wildfire S (MDPI) so I've got most resolutions covered.

But today, my brother downloaded my app on his new HTC Sensation which has yet another resolution 540x960 (HDPI?). Which didn't show my app as it should and probably the most tablets won't show it correctly either.

What I've did with my first app was read out the density and then set the parameters:

public void set_ui_parameters() {
    DisplayMetrics metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);

    if(metrics.densityDpi == DisplayMetrics.DENSITY_HIGH){
        textSize   = 35;    
        timeWidth  = 80;
        dayWidth   = 110;
        moneyWidth = 50;
    } else if(metrics.densityDpi == DisplayMetrics.DENSITY_MEDIUM){
        textSize   = 35;    
        timeWidth  = 53;
        dayWidth   = 73;
        moneyWidth = 33;
    } else if(metrics.densityDpi == DisplayMetrics.DENSITY_LOW){
        textSize   = 28;
        timeWidth  = 40;
        dayWidth   = 55;
        moneyWidth = 25;
    }
}

Besides the parameters I've also created drawables for LDPI, MDPI and HDPI. This works fine for the resolutions described above, but this depends on the screen resolution i.c.w. screen size and fails for, for example the HTC sensatoin with 540x960.

I know that not all the resolutions are used that often, but I would like to support as many as possible. Stats of Screen Sizes and Densities

I've read Supporting Multiple Screens multiple times but didn't found a clear answer to this "problem".

So should I read out the resolution and set the parameters according to the resolutions instead of density? Is this a smart thing to do or how do you cope with this?

Thanks a lot for your information!

alain.janinm
  • 19,951
  • 10
  • 65
  • 112
patrick
  • 1,282
  • 4
  • 21
  • 37

5 Answers5

30

You don't have to do that to support different densities. What you do is create different resources folders:

res/values-ldpi/dimens.xml
res/values-mdpi/dimens.xml
res/values-hdpi/dimens.xml

Then Android will decide which file to use. You can have something like:

<!-- in values-ldpi/dimens.xml -->
<dimen name="textSize">25dip</dimen>

and..

<!-- in values-mdpi/dimens.xml -->
<dimen name="textSize">20dip</dimen>

etc. And you shouldn't care about resolution... there are a lot of different resolutions sizes so it would be a hell to take decisions based on that.

Also, if you use dp instead of pixels, you hardly ever will have to create different dimensions files for each density. Of course, sometimes you have to, but it depends on the app.

Cristian
  • 198,401
  • 62
  • 356
  • 264
  • Thanks the values is very use-full, but this doesn't solve the problem with the 540x960 resolution or am I wrong . Because SQRT(540^2+960^2)/4.3 = 256,15 (HTC Sensation) is about the same as SQRT(480^2+800^2)/3.7 = 252,15 (HTC Desire Z) and both will be indexed as HDPI where as the screen of the Sensation is 60px wider. – patrick Jun 19 '11 at 16:50
  • 2
    Yeah... problem here is that you won't be able to handle 100% of screen sizes. So, for instance... if you want a text view to cover exactly the width of the screens, even if you apply all possible techniques, there will be some devices which will wrap the text or something. My point is, you cannot make you app look exactly as you want on all devices... you have to conform with making it look as better as possible. – Cristian Jun 20 '11 at 03:34
1

Only thing you have to do is, that you set android:minSdkVersion to 7 or higher in your manifest file. Is is possible that some views will appear slightly different but app is applicable and on whole screen.

Roman
  • 11
  • 1
0

Now, i'm just guessing here since the rest of the implementation isn't shown, but I'm assuming you are using those derived measurements as px

Take a look at dp. That essentially does all those things you did, automatically, for any device. (Device Independent Pixel)

Eric
  • 1,953
  • 4
  • 24
  • 33
0

Don't worry about the slight difference in resolution: 540x960 is only slightly bigger than 480x800. The extra vertical space is easy: it gives you more room for your lists. For horizontal space, as long as you're handling your layouts correctly you'll simply have extra padding (30pix ea) on the sides.

Unfortunately, full-width images (480 wide) are a slightly bigger problem. If you want pixel perfect images, you'll want to use scaleType="center" so the image centers but not scales. If you want full width, you can use scaleType="fitCenter" to make it fill. It will be a bit fuzzy... but so many thing on Android are ;-)

radley
  • 3,212
  • 1
  • 22
  • 18
0

I ran into this problem as well, except my app is a game where there is one SurfaceView canvas that I draw background images and sprites to (similar to LunarLander example).

The way you handle it here is to extend your background image to the largest size you are willing to support (540x960) but you keep all the important things (like buttons, text, information, etc) within a smaller rectangle of 480x800. You can extend the image itself, or just add borders. The key is that nothing important is there.

People with 480x800 phones will see your normal app. People with 540x960 phones will see a little extra border.

stipe108
  • 1,640
  • 1
  • 16
  • 20