Search code examples
androiddrawabledpi

Use xxhdpi on xhdpi device


I created my app to support screens from hdpi to xxxdpi and everything was working perfect. Images were shown just like they're supposed to. But, now I have a problem with Xiaomi Mi Max 3. It has 6,9" display with a resolution of only 1080x2160 which gives a ~350 dpi. That falls into xhdpi category (usually 720p screens) and now my images are smaller then they should be. Is there any way to force the use of drawable-xxhdpi folder with code?


Solution

  • Ok, thanks to Md.Abdullah I found a solution. You cannot choose which folder of resources to use but you can use specific resource from a folder with getDrawableForDensity() method. This is what I did:

    int width = Resources.getSystem().getDisplayMetrics().widthPixels;
    int dpi = getResources().getDisplayMetrics().densityDpi;
    int imageID = R.drawable.YOURIMAGE;
    if (width > VALUE && dpi < VALUE) {
        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) {
            Drawable drawable = getResources().getDrawableForDensity(imageId, DisplayMetrics.DENSITY_XXHIGH);
    //you will probably need to adjust width & height of your ImageView, but since the image
    //will keep its aspect ratio, you only have to change width | heigth
            YOURIMAGEVIEW.setAdjustViewBounds(true);
            YOURIMAGEVIEW.setMaxWidth(VALUE);
            YOURIMAGEVIEW.setImageDrawable(drawable);
       }
    }