11
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Androidで横向きか縦向きかを判定する

Last updated at Posted at 2015-05-22

<activity android:configChanges="orientation|screenSize">を使って縦向き・横向きの変更をコード内で処理することができますが、Activity起動時など、onConfigurationChangedが呼ばれる前にどっち向きかを判定するには下記のコードを使います。

int orientation = getResources().getConfiguration().orientation;

if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
  // 横向きの場合
}

if (orientation == Configuration.ORIENTATION_PORTRAIT) {
  // 縦向きの場合
}

実際には↑のように二つのifを使わずにif,elseを使えば済むと思いますが、どちらをifに書くべきかは状況によるので念のため両方書いてます。
orientationintなので、switchで分岐しても良いです。

11
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?