0

I am working on an android application. i have used different layout folder. Like layout,layout-large,layout-xlarge. So that it can adjust for all resolutions. But i am setting images dynamically in activity. How can i change them according to screen resolution? How too big image will replace smaller if we change resolution?

Chintan Rathod
  • 25,864
  • 13
  • 83
  • 93
user2147892
  • 61
  • 1
  • 1
  • 6

1 Answers1

1

Android works with density buckets, which go from l(low)dpi to xx(extra extra)h(high)dpi.

You want to create different versions of your images in folders as

  • drawable-ldpi
  • drawable-mdpi
  • drawable-hdpi
  • drawable-xhdpi
  • and drawable-xxhdpi if you want to support the Nexus 10.

That's kind of loose from the layout-large folders, which enable you to define different layouts for different sizes.

2 pretty different things, which you can read much more about at screen practices in Android.

======= Edit; Seems this wasn't exactly the question.

If you're doing it 'the right way' the Android system will choose the correct image for you, even when adding them dynamically (you can still call R.drawable.my_image from java code).

If for some reason you do have to choose, you can simply check for the current density with something like (a little outdated);

public String getDensity() {
    String density;
    DisplayMetrics dm = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(dm);
    // will either be DENSITY_LOW (120) , DENSITY_MEDIUM (160) or
    // DENSITY_HIGH (240)
    switch (dm.densityDpi) {
    case 120:
        density = "ldpi";
        break;
    case 160:
        density = "mdpi";
        break;
    case 240:
        density = "hdpi";
        break;
    // use hdpi as default, because flurry shows this will be suited for
    // most of our users.
    default:
        density = "hdpi";
        break;
    }
    return density;
}
Stefan de Bruijn
  • 6,289
  • 2
  • 23
  • 31
  • I am asking that how to replace images dynamically according screen resolution. if i uses the screen size 480*800 . then image should replace by biger one. – user2147892 Mar 12 '13 at 09:13