Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding a support multiply screens

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_PORTRAITMAIN_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.

like image 471
whizzzkey Avatar asked Dec 17 '13 02:12

whizzzkey


4 Answers

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

like image 81
Pasquale Anatriello Avatar answered Oct 16 '22 10:10

Pasquale Anatriello


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.

like image 31
Rakeeb Rajbhandari Avatar answered Oct 16 '22 11:10

Rakeeb Rajbhandari


  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.

like image 39
Wubao Li Avatar answered Oct 16 '22 10:10

Wubao Li


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!)

like image 33
ericn Avatar answered Oct 16 '22 10:10

ericn