Search code examples
androidfragmentscreen-resolution

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.


Solution

  • 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...