9

I know that there is a lot of gigabytes of information about my problem, I've almost read all of it - official documentation on developer.android.com, posts on XDA-Developers and here, and many other BUT STILL have problem with it and I hope you can explain it to me. So I've an app with dashboard icons on a main-screen, and I need to make that this screen will be looked the same on all existing screens, here is a portrait and land standarts:

MAIN_PORTRAIT MAIN_LAND

To make it I've added to my project a next folders:

enter image description here

And all sizes such as images size, text size and margins I've setted in dimens.xml for each screen resolution, and I was thinking that's enough to see the same picture on different devices. But I was wrong, and in the same resolution but different screen size it's looks different. Here is a 3.2" and 5.1" screens with mdpi:

Screen 3.2" Screen 5.1"

I didn't get should I add a folders like "values-mdpi-sw320dp" and so on or there is something another way I should know to see same picture on all screens? Is it a right way to set an all sizes in dimens? Please explain it to me. REGARDS.

whizzzkey
  • 926
  • 3
  • 21
  • 52
  • Check this link- http://stackoverflow.com/questions/19065701/applying-resolution-for-different-devices/19065842#19065842 – Kanwaljit Singh Dec 20 '13 at 09:26
  • @KanwaljitSingh I've read your answer in that link, it seems to be you didn't understand what I've asked. Yeah, you are completely right to use a "Device resolution" dimens, but what you'll do if you get a devices with same screen resolution but differs screen size? it is complicated by the fact that I've used a dimens for all sizes(images,buttons,etc). – whizzzkey Dec 20 '13 at 10:36
  • ok check this one http://stackoverflow.com/questions/15506786/how-do-i-create-different-layouts-for-4-and-10-displays-at-same-resolution – Kanwaljit Singh Dec 20 '13 at 10:43
  • @KanwaljitSingh still useles for me, cause i don't want to create a many layouts like layout-hdpi-sw320dp layout-hdpi-sw480dp etc, i think beter way to use a one layout but differs dimension for it. It seems to be that nobody on stackOverflow knows how to properly make a support of multiply screens. – whizzzkey Dec 20 '13 at 15:24

6 Answers6

4

You don't need to specify all these different sizes to achieve a screen as simple as that one. Simply play a bit around with layouts. For example let's take your first screen. You could use a TableLayout or even a vertical LinearLayout with N LinearLayout inside. At this point simply use weights! Example: want a grid of two rows and three square images for each row:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <ImageView
        android:layout_width="0px"
        android:layout_height="wrap_content"
        android:layout_weight=".33" />

    <ImageView
        android:layout_width="0px"
        android:layout_height="wrap_content"
        android:layout_weight=".33" />

    <ImageView
        android:layout_width="0px"
        android:layout_height="wrap_content"
        android:layout_weight=".33" />
</LinearLayout>

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <ImageView
        android:layout_width="0px"
        android:layout_height="wrap_content"
        android:layout_weight=".33" />

    <ImageView
        android:layout_width="0px"
        android:layout_height="wrap_content"
        android:layout_weight=".33" />

    <ImageView
        android:layout_width="0px"
        android:layout_height="wrap_content"
        android:layout_weight=".33" />
</LinearLayout>

Now unfortunately that's still not enough for you. Another thing you might want to accomplish is to still have your images have a square ratio. To achieve that simply extend image view like this:

public class SquaredImageView extends ImageView {

public SquaredImageView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

@Override 
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
             int width = MeasureSpec.getSize(widthMeasureSpec);
             int height = width;
             setMeasuredDimension(width, height);

}

}

Now simply provide images in the right densities and everything will look exactly as you want

Pasquale Anatriello
  • 2,355
  • 1
  • 16
  • 16
  • thank you for your answer, but still be problem with a diferent screen's picture in 3.2" and 5.1" mdpi screens cause an icons(from drawable-mdpi) are to small for 5.1" and when i increasing it sizes it looks very bad and ugly(lot of pixels are seen on screen) – whizzzkey Dec 26 '13 at 05:38
  • for this problem you can use different drawables (here is where the different folders really help) simply put a bigger mdpi image into a folder called drawables-swXXdp-mdpi substituting the XX with the smallest size in dp from which that drawable should be used – Pasquale Anatriello Dec 27 '13 at 02:47
1

I think this topic is a lot of trouble too. As you said you should use the values-sw320dp-hdpi, values-sw320dp-xhdpi, values-sw320dp-xxhdpi and so on in your application.

I recently did the same on an application I was working on and I found out that the Galaxy S2 read the values-sw320dp-hdpi, the Galaxy S4 took the values-sw320dp-xxhdpi and the Galaxy Note took values-sw320dp-xhdpi. So yes, declaring these things is a necessity.

You could refer to this as a quick read if you want to. And this application is really helpful as it reveals most of the devices well kept secrets.

Rakeeb Rajbhandari
  • 5,043
  • 6
  • 43
  • 74
1
  1. The smallest-width qualifier is available only on Android 3.2 and above, if your 5.1" screen is running os version earlier than 3.2, it will not match the right layout.

  2. if both the android version is >= 3.2, the layout_sw600dp or layout_sw720dp 's layout file is not "truly" optimized for it. From the screen shot you provided, the layout's size of some ui element is not optimized right.

Wubao Li
  • 1,728
  • 10
  • 13
1

First thing first, it doesn't matter your screen size in inch at all. You have to figure out your screen density e.g. 240+dpi for my Sony Xperia Z. Afterwards, you can match them to the closet bucket defined by Android e.g. 240dpi - hdpi.

In your case, the bucket is mdpi. Only 1 set of drawable in drawable-mdpi will apply to both of your devices.

What you are seeing is the correct behaviour. You might want to re-layout your stuffs to fit the bigger screen e.g. having 5 rows instead of 3. Alternatively, you can draw bigger frames (160dp for each icon instead of 100dp; you need to use DisplayMetrics and do some calculation) AND use higher-resolution images to fit those frames (these images need to be in the SAME folder drawable-mdpi though!)

ericn
  • 12,476
  • 16
  • 84
  • 127
  • 1
    Thanks, but problem is that icons is looks good in 3.2" mdpi but too small for 5.1" mdpi, and when i trying to increase it sizes it became looks very bad and ugly(i've seen a lot of pixels). – whizzzkey Dec 26 '13 at 05:48
  • Ok, if they are both mdpi, from your screenshots and from the huge difference between 3.2" and 5.1" I can conclude that the width and height are very different for 2 devices e.g. 300dp width for one and 500dp with for another. So, you really have to go with the precision calculation of width and height here e.g. `icon width = screen width/3` which is 100dp for the 3.2" device and 160dp for 5.1". Just remember to use images with highest possible resolution – ericn Dec 26 '13 at 06:38
  • please tell me did i understand you right - instead of many drawable-dpi folders i should use an one "drawabale" with images with highest possible resolution, right? – whizzzkey Dec 26 '13 at 08:09
  • 1st, only 1 set of drawable (mdpi) will apply to your case! 2ndly, by those calculations, I was talking about the frames/ boxes in which you put your images as well – ericn Dec 26 '13 at 08:19
0

Create layout folders in your res/ and name them as layout, layout-large. Now they should work for different screen sizes

Akshat Agarwal
  • 2,837
  • 5
  • 29
  • 49
  • Man i've already created this folders, but its unuseful for me cause i have all layout-items sizes in dimensions(values-xxxdpi->dimens.xml) – whizzzkey Dec 26 '13 at 05:41
0

If weighting will be not enough for you, consider to set dimensions dynamically in code. You can get current screen diagonal by this:

    Display display = ((WindowManager) c.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
    DisplayMetrics displayMetrics = new DisplayMetrics();
    display.getMetrics(displayMetrics);
    screenRect = new Rect(0, 0, width, height);
    screenDiagonal = Math.sqrt(width * width + height * height) / displayMetrics.densityDpi;

Then you can dynamically create LayoutParams for your views and apply sizes according to this value. I know it's a bit hacky and approach with weights is more native, but this might work for you.

Dmide
  • 6,422
  • 3
  • 24
  • 31
  • Thanks, but I think your solution is to hard and difficult, bu your answer I should add this code in all my dashboard-activities, I think it will be excess and confuse. – whizzzkey Dec 26 '13 at 05:44
  • No problem. And by the way, you could calculate it once and store in some singleton class. – Dmide Dec 26 '13 at 06:56