0

How I can get screen width and height for use this value on device with android version 2.2(API lv8). I use this code:

public static Point Maximum(Context context)
       {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay();
        int width = display.getWidth();  // deprecated
            int height = display.getHeight();  // deprecated
            Point temp=new Point();     
            return temp;
       }

And when i want check by print Max point to textview:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    TextView txt= (TextView)findViewById(R.id.text1);
    txt.setText(Math.Maximum(this).toString()); 
}

Point is (0,0). What problem?

thefriend
  • 339
  • 1
  • 6
  • 16
  • possible duplicate of [Get screen dimensions in pixels](http://stackoverflow.com/questions/1016896/get-screen-dimensions-in-pixels) – Gerald Schneider Nov 10 '14 at 08:58

4 Answers4

1

Try this code. But this API is deprecated in the new SDK versions you can use this.

Display display = activity.getWindowManager().getDefaultDisplay();
int width  = display.getWidth();
int height = display.getHeight();
Tushski
  • 242
  • 2
  • 12
0

Try this code:

DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int height = displaymetrics.heightPixels;
int width = displaymetrics.widthPixels;
arielhad
  • 1,753
  • 15
  • 12
  • It does not work. Error:The method getWindowManager() is undefined. – thefriend Nov 12 '14 at 00:43
  • Read this please: http://stackoverflow.com/questions/8833825/error-getting-window-size-on-android-the-method-getwindowmanager-is-undefined – arielhad Nov 12 '14 at 09:48
0

Try this for getting your screen width. Similarly you can get screen height or can customize the same for returning both in a single function.

 @SuppressWarnings("deprecation")
 @SuppressLint("NewApi")
 private int getScreenWidth() {
    int width = 400;
    int height = 600; // just intialising it to default value in case if there is any problem getting screen width
    WindowManager wm = (WindowManager) activity // activity is the context if you are using this method in adapter. Otherwise in Activity you can simply ignore this
            .getSystemService(Context.WINDOW_SERVICE); 
    Display display = wm.getDefaultDisplay();
    if (android.os.Build.VERSION.SDK_INT >= 13) {
        Point size = new Point();
        display.getSize(size);
        width = size.x;
        height = size.y;
    } else {
        width = display.getWidth();
        height= display.getHeight();
    }
    return width;//or height
}
berserk
  • 2,690
  • 3
  • 32
  • 63
Mukesh Rana
  • 4,051
  • 3
  • 27
  • 39
0

Get Device Width and Height, return as point. Hope u like this.

public static Point Maximum(){
    Display display = activity.getWindowManager().getDefaultDisplay();
    int width  = display.getWidth();
    int height = display.getHeight();
    Point temp=new Point();
    temp.set(width, height);     
    return temp;
}
Sakthivel Appavu
  • 565
  • 5
  • 24