Error getting window size on android: “The method getWindowManager is undefined”

I need to get the window height and the weight.
I have this in my code:

DisplayMetrics metrics = new DisplayMetrics();
width1 = metrics.widthPixels; 
height1 = metrics.heightPixels;

but I have an error:

  • eclipse tells me my project has errors, nothing is red-highlighted
  • Android - Testing app on Galaxy S8 or LG G6
  • How to install old version of Android build tools from command line?
  • Can't connect LG G3 to Android Studio
  • Samsung Phones emit a lot of log messages on logcat
  • How to test android referral tracking?
  • The method getWindowManager is undefined

    what i should import for this?
    or i can get the sizes of the screed in different ways?
    i have imported

    import android.view.Display;
    import android.view.Window;
    import android.view.WindowManager;
    import android.content.Context;
    import android.util.DisplayMetrics;

    Related posts:

    Android + Data Binding @style
    How to Avoid Scientific Notation in Double?
    Display a part of the webpage on the webview android
    Clear firebase persistence after logout
    How to count speed based on steps
    use Jelly Bean's Simple Secure Pairing ( Bluetooth ) to pair with NFC
  • Getting Error inflating class
  • Calculate the size of a list view or how to tell it to fully expand
  • Google play services returning a token signed with a non-Google key
  • Android: Mixing multiple AudioTrack instances?
  • Styling the SearchView Widget using support library v21
  • Delete key in Translations Editor
  • 5 Solutions collect form web for “Error getting window size on android: “The method getWindowManager is undefined””

    Where are you using this code?

    The error of the getWindowManager says that cannot find the method because the root of your class doesn’t have it.

    Try this:

    ((Activity) getContext()).getWindowManager().getDefaultDisplay().getMetrics(metrics);


    ((WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE))

    If this doesn’t work, please paste your activity

    Use this:

     display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
     display.getWidth(); // to get width of the screen
     display.getHeight(); // to get height of the Screen

    Enjoy. :))

    You could use this for example if you are using it inside a fragment:


    Use the Context of the activity with getActivityContext()

    DisplayMetrics metrics = new DisplayMetrics();
    width1 = metrics.widthPixels; 
    height1 = metrics.heightPixels;

    Please check if you are calling above method in an activity class, or outside activity.

    For Wallpaper, this method is not availed to you to get height or width of window, you need to get width and height of available window by using surface.

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.