androidcodehunter
androidcodehunter

Reputation: 21937

How to check screen resolution inside Fragment?

I tried this code in Fragment to get the window size :

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

But it did not return me the actual screen resolution. I want to check the ldpi, hdpi, mdpi screen resolution dynamically inside fragment class. How can I achieve this. Please help me.

Upvotes: 1

Views: 1096

Answers (1)

Blackbelt
Blackbelt

Reputation: 157457

int density = getActivity().getResources().getDisplayMetrics().densityDpi

then you can compare with DisplayMetrics.DENSITY_LOW, DisplayMetrics.DENSITY_MEDIUM and the others

if (density == DisplayMetrics.DENSITY_LOW) {

} else if (density == DisplayMetrics.DENSITY_MEDIUM) {
}

and so on...

Upvotes: 1

Related Questions