Android開発 画面の向きの取得 画面の向きの固定

■画面の向きを取得 画面の向きによる処理の分岐
・クラスに記載

Resources res = getResources();
Configuration conf = res.getConfiguration();
if(conf.orientation == Configuration.ORIENTATION_PORTRAIT) {
  //縦画面の時の処理
}else if(conf.orientation == Configuration.ORIENTATION_LANDSCAPE){
   //横画面の時の処理
}

■画面の向きの固定
・ManifestのActivity属性に記載する場合

縦画面固定

android:screenOrientation="portrait"

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

横画面固定

android:screenOrientation="landscape"

・クラスに記載する場合

縦画面固定

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

横画面固定

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);