jhobbie
jhobbie

Reputation: 1016

Android Compatibility with multiple devices

Ok, so I know that if I want to make images and text and buttons not get too squished in tiny devices and not be absolutely tiny in giant tablets I can create resource files that are distinct for each device and each orientation.

Is there a more concise way to do this? I.e. is there a way to have a single folder with all my resources multiplied by like a "screen scaling factor" so that every time I want to edit I don't have to go back and change 30 different resources and then check to make sure they all fit?

Thanks

Upvotes: 2

Views: 53

Answers (2)

Javier
Javier

Reputation: 1695

Actually its efficient having several images for every device, its not the same loading 500px for a low end device than loading a 100px that its actually you need. so i suggest using scaleType in the ImageView and adjust your UI using dimens inside android_layout_width and android:layout_height example: android:layout_width="@dimen/size" and you can use different sizes based on your device. another way is put the images under drawable-nodpi and scale in runtime so this will always fix the image as you need

public static int calculateInSampleSize(
            BitmapFactory.Options options, int reqWidth, int reqHeight) {
    // Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {

        final int halfHeight = height / 2;
        final int halfWidth = width / 2;

        // Calculate the largest inSampleSize value that is a power of 2 and keeps both
        // height and width larger than the requested height and width.
        while ((halfHeight / inSampleSize) > reqHeight
                && (halfWidth / inSampleSize) > reqWidth) {
            inSampleSize *= 2;
        }
    }

    return inSampleSize;
}

you can look in android developer guide for further help scaling bitmaps Efficiently http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

Upvotes: 0

G.T.
G.T.

Reputation: 1557

You do not have to create different sizes for each widget.

For Button, TextView, ImageView (and any other Views) you can set the android:layout_width and android:layout_height properties in dp (Density-independent pixels).

This unit is - as we can guess from its name - independent from the density of the screen. It means that the result will be the same on every screen.

The only thing that you might have to create in multiple sizes is the icons. For that you have to follow the rules provided by Android about the ratio between the different densities. For more information, you can take a look at this.

You can also create your icon in a XML file. This way, it will automatically fits to every screen. The problem is that this can be very difficult to create complex icons.

Upvotes: 2

Related Questions