Home » Android » Android change resource qualifier programmatically

Android change resource qualifier programmatically

Posted by: admin May 14, 2020 Leave a comment

Questions:

Is it possible programmatically change resource configuration qualifier?
I mean, make system for some hdpi devices took resources from xhdpi folder instead of hdpi.

How to&Answers:

In my project i have ldpi, mdpi, hdpi and xhdpi
image resources. For Kindle fire(1024 x 600 – 169 dpi) android system
takes resources from mdpi folder and application looks very bad. I
want to make android took resources from hdpi folder.

Related to your comment I usually provide resources only for hdpi screen and all other densities work fine. Testing at 2.1+ or 2.2+ devices which I usually target!

I understand the risk that is not documented, so it will not supposed to work although it does! In that way I avoid the some KB from my final apk.

But I do not suggest using xhdpi instead of hdpi as xhdpi works only in 3+ or something devices.

Just for testing different densities on a single device you can change AVD by:

am display-density 480 

See android doc for more info: http://developer.android.com/tools/help/adb.html#shellcommands

Answer:

I’m not sure if you can. Don’t get me wrong: I personally would like to know the answer to this as well. What exactly would you like to do?

If you are going to do things programatically anyways, it might be better to put the asset into straight drawable, and then apply scaling. You can get the density multiplier pretty easily

Answer:

Kindle has large screen but with medium density

It takes the layout from res/layout-large/your.xml

But the images from res/drawable/mdpi so just create a folder called res/drawable/mdpi-large

hope this will work for you but the images must be as per the kindle’s resolution

Answer:

Try this code. Hope it helps.

    import android.util.DisplayMetrics;

    private int result = 0;  //Activity member

    public void determineDeviceResolution {

    switch(metrics.densityDpi)
    {      
        case DisplayMetrics.DENSITY_LOW: 
        {
            result = DisplayMetrics.DENSITY_LOW;
            break;
        }

        case DisplayMetrics.DENSITY_MEDIUM: 
        {
            result = DisplayMetrics.DENSITY_MEDIUM;
            break;
        }

        case DisplayMetrics.DENSITY_HIGH: 
        {
            result = DisplayMetrics.DENSITY_HIGH;
            break;  
        }

        default:
            break;          
    } 

   }


    private Drawable getDrawable(String icon) { 

    //Pass file name of the resource file 
    //while invoking this method for e.g. play_icon.png 


    //You can specify the folder from which resource
    //needs to be picked for e.g. drawable_hd,drawable_xhdpi etc..

   Drawable drawable = null;

    if(result == DisplayMetrics.DENSITY_HIGH)
    {

                drawable=create_drawable("/drawable_hd/" + icon);

    }

    else
    {
            drawable=create_drawable("/drawable/" + icon);
    }

    return drawable;
}



    Drawable create_drawable(String absolute_filepath)
{
    InputStream in = null; 

    Bitmap bMap = null; 

    BufferedInputStream buf = null;

            Drawable d = null;

    try 
    {
        in  = getClass().getResourceAsStream(absolute_filepath);
        buf = new BufferedInputStream(in);
        bMap = BitmapFactory.decodeStream(buf);

        if(in == null || buf == null || bMap== null)
        {
            return null;
        }

        if (in != null) 
        {
            in.close();
        }

        if (buf != null) 
        {
            buf.close();
        }

    } 

    catch (Exception e) 
    {
       e.printStackTrace();
    }


    d  = new BitmapDrawable(bMap);

    return d; 

}

Finally you can pass the drawable to the UI element’s setBackgroundDrawable() method.

Answer:

You can also customize the resources for the width and other properties of devices. Read this: http://developer.android.com/guide/practices/screens_support.html