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:

  • Loader doesn't start after calling initLoader()?
  • What is Android's file system??
  • Starting Android service already running?
  • How do I programmatically set the background color gradient on a Custom Title Bar?
  • New Android Design Library bug with AppBarLayout and Toolbar
  • Android Custom Event Listener
  • 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;

  • Android SDK Manager - Do you need to install all android platforms?
  • Dialog activity to wrap content
  • Converting from JSONArray to String then back again
  • BitmapFactory.decodeStream returning null when options are set
  • Tablet not appearing in ADB
  • Modifying the color of an android drawable
  • 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.