djunod
djunod

Reputation: 4976

Android: programmatically get res drawable- name

Is there a way to programmatically determine which drawable-dpi directory is used by a device?

Upvotes: 0

Views: 867

Answers (3)

djunod
djunod

Reputation: 4976

Going off of DisplayMetrics

I wrote this to display the name of the drawable directory used by the current device:

private String getDisplayDirectory() {
    String prefix = "drawable";
    DisplayMetrics metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);
    if (metrics.densityDpi == DisplayMetrics.DENSITY_LOW)
        return prefix+"-ldpi";
    if (metrics.densityDpi == DisplayMetrics.DENSITY_MEDIUM)
        return prefix+"-mdpi";
    if (metrics.densityDpi == DisplayMetrics.DENSITY_TV)
        return prefix+"-tv";
    if (metrics.densityDpi == DisplayMetrics.DENSITY_HIGH)
        return prefix+"-hdpi";
    if (metrics.densityDpi == DisplayMetrics.DENSITY_XHIGH)
        return prefix+"-xhdpi";
    if (metrics.densityDpi == DisplayMetrics.DENSITY_XXHIGH)
        return prefix+"-xxhdpi";
    if (metrics.densityDpi == DisplayMetrics.DENSITY_XXXHIGH)
        return prefix+"-xxxhdpi";
    return prefix;
}

Upvotes: 1

don
don

Reputation: 121

You can use DisplayMetrics to determine that.

DisplayMetrics metrics = getResources().getDisplayMetrics()
switch (metrics.densityDpi) {
   case DisplayMetrics.DENSITY_HIGH: {
       // Do your stuff if density is high
       break;
   }
   case DisplayMetrics.DENSITY_MEDIUM: {
       // Do your stuff if density is medium
       break;
   }
   ...
}

Alternatively, you can also use the following to obtain the metrics:

DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);

Upvotes: 0

dst
dst

Reputation: 3337

You should be able to create a Resources object as shown in this question about localization, using an modified DisplayMetrics object.


Reading your question again, if you only wish to get the type of the screen, and not the Ressources itself, you can fill your DisplayMetrics object using getWindowManager().getDefaultDisplay().getMetrics(metrics); as shown in the linked example, then evaluate the density instance variable.

Upvotes: 1

Related Questions