Boaz K
Boaz K

Reputation: 515

DisplayMetrics is returning zero

I want to get the dpi of the device's screen but when I use:

          DisplayMetrics m = getResources().getDisplayMetrics();
                float den = m.density;

I get the full DisplayMetrics object as zero (I saw it in the debugging). It was the same also in my emulator (android 2.3) and on my phone (Htc Legend android 2.1 update 1). The code is located in my activity class(extends Activity) and then

WebView.setWebChromeClient(new MyWebChromeClient() {

        public void onProgressChanged(WebView view, int progress) {

              DisplayMetrics m = getResources().getDisplayMetrics();
                float den = m.density;

}

How can I fix It?

Thank You,

Boaz

Upvotes: 2

Views: 1258

Answers (1)

g00dy
g00dy

Reputation: 6768

Taken from here:

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;
}

Try it out and get back here with the results :)

Cheers

Upvotes: 4

Related Questions