Stack Overflow archive
3 scoreaccepted

I want to be able to get the screen height and width in Android

score
3
question views
842
license
CC BY-SA 3.0

You are calling getResources() before the super.onCreate(Bundle) is called. You must call getResources() after onCreate(). Move your code to onCreate or onStart:

java
@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    DisplayMetrics display = getResources().getDisplayMetrics();
    int width = display.widthPixels;
    int height = display.heightPixels;
}

Originally posted on Stack Overflow. Public user contributions are licensed under Creative Commons Attribution-ShareAlike.