Help us understand the problem. What are the problem?

More than 5 years have passed since last update.

posted at


Androidで画面サイズを取得するのに、従来はDisplayクラスのgetWidth()/getHeight()メソッドを使っていたが、API level 13以降では非推奨(Deprecated)になっています。

代わりに、getSize(Point point)メソッドが追加されていて、こちらを使うようにとのことです。ただし、このgetSize()メソッドはAPI level 13より前では存在しません。そのため、API level 13をまたぐコードを書く場合は、注意が必要です。

stacoverflow : Android get Screen Size deprecated?

Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
overrideGetSize(display, size);

void overrideGetSize(Display display, Point outSize) {
    try {
      // test for new method to trigger exception
      Class pointClass = Class.forName("android.graphics.Point");
      Method newGetSize = Display.class.getMethod("getSize", new Class[]{ pointClass });

      // no exception, so new method is available, just use it
      newGetSize.invoke(display, outSize);
    } catch(NoSuchMethodException ex) {
      // new method is not available, use the old ones
      outSize.x = display.getWidth();
      outSize.y = display.getHeight();
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Sign upLogin
Help us understand the problem. What are the problem?