Question

I want to write a function like this

public boolean isThisScreenWithHdpiDensity(){
    return true/false;
}

but I do not know how to calculate this at run time programmatic-ally

Was it helpful?

Solution

Use this inside your function:

DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
switch(metrics.densityDpi){
     case DisplayMetrics.DENSITY_LOW:
                break;
     case DisplayMetrics.DENSITY_MEDIUM:
                 break;
     case DisplayMetrics.DENSITY_HIGH:
                 break;
}

OTHER TIPS

      public boolean isThisScreenWithHdpiDensity()
      {         
         DisplayMetrics metrics = new DisplayMetrics();
         getWindowManager().getDefaultDisplay().getMetrics(metrics);

         if(metrics.density == DisplayMetrics.DENSITY_HIGH)
         {
             return true;
         }

         return false;
      }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top