-1

I have been trying to figure out how to find the height and width of the screen in android. i have found a way to get it but I dont know how to call the getScreenWidth and getScreenHeight to put values into my int height and int width?

public class Player {

public int across;
public int upDown;

public static int getScreenWidth(Context c) {
    DisplayMetrics dmetrics = new DisplayMetrics();
    ((Activity) c).getWindowManager().getDefaultDisplay()
            .getMetrics(dmetrics);
    return dmetrics.widthPixels;
}

// Get screen height
public static int getScreenHeight(Context c) {
    DisplayMetrics dmetrics = new DisplayMetrics();
    ((Activity) c).getWindowManager().getDefaultDisplay()
            .getMetrics(dmetrics);
    return dmetrics.heightPixels;
}

public Player() {

    int width = dmetrics.widthPixels;
    int height = dmetrics.heightPixels;
    int rectSide = 1000;

    //across = startPosX;
    //upDown = startPosY;

}

i am trying to get it by using int width = dmetric.widthPixels; but this obviously isnt the way to call the getScreenWidth. Im pretty bad at this stuff, so any help would be great.

Ciaran
  • 697
  • 1
  • 12
  • 35
  • See http://stackoverflow.com/questions/30929519/android-how-to-find-width-and-height-of-screen?rq=1 – candied_orange Jun 21 '15 at 03:36
  • See also your other question of the same topic http://stackoverflow.com/questions/30930340/how-to-find-the-height-and-width-of-screen-in-android?rq=1 : P – candied_orange Jun 21 '15 at 03:45
  • possible duplicate of [Get screen dimensions in pixels](http://stackoverflow.com/questions/1016896/get-screen-dimensions-in-pixels) – Matt Urtnowski Jun 21 '15 at 03:48
  • I know, really what im asking is how do i call this get method i have created to set values for width and height? – Ciaran Jun 21 '15 at 03:48
  • possible duplicate of [Get Screen width and height](http://stackoverflow.com/questions/4743116/get-screen-width-and-height) – Dhanuka Jun 21 '15 at 03:49

2 Answers2

3

You can refer to Here or Here for your answer. Ps - sorry , I can't comment as I am a beginner so I wrote the answer which should rather be a comment.

Community
  • 1
  • 1
0

I know, really what im asking is how do i call this get method i have created to set values for width and height? – Phill

If you are in an Activity when you wish to call these static methods you could call them this way:

int width = Player.getScreenWidth(this);
int height = Player.getScreenHeight(this);

If you're not in an activity you'll need to know one and pass it instead of this.

Also be aware that, because of the cast, if you pass something that is a Context but not an Activity it won't work and you won't know it until run time.

candied_orange
  • 7,036
  • 2
  • 28
  • 62