Jon
Jon

Reputation: 121

Finding Screen Dimensions Android

I have read a few posts on this site about finding screen dimensions and most of them reference getWindowManager().getDefaultDisplay;

I tried to this is my code and got an error saying cannot resolve method getWindowManager(). My code is below so you can fully understand:

public class MainActivity {
    public static void main(String args[])throws IOException{

    Display display = getWindowManager().getDefaultDisplay;
    //WindowManager wm = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
    //int displayid = Display.DEFAULT_DISPLAY;
    //Display display = DisplayManager.getDisplay(displayid);
    int tlx = (168/800)*display.getWidth();
    int tly = (136/480)*display.getHeight();
    int brx = (631/800)*display.getWidth();
    int bry = (343/480)*display.getHeight();

    int rotation = display.getRotation();

This is just an excerpt in case somethings don't make sense. Also, I'm using the display dimensions to find screen elements within an application. If I should be using the window dimensions for this please inform me upon how to go about doing that as well, but I still need to get the display right so I can use methods such as the .getRotation() one shown above. Thanks in advance for your help.

Upvotes: 1

Views: 146

Answers (1)

Chris Stillwell
Chris Stillwell

Reputation: 10547

You need to reference getWindowManager from an Activity. Your MainActivity doesn't appear to be extending Activity so you either need to extend Actvity

public class MainActivity extends Activity {

Or pass an activity to that class then do something like this.

Display display = myActivity.getWindowManager().getDefaultDisplay();

Or through a Context

Display display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();

Upvotes: 1

Related Questions