KotlinでAndroidアプリの画面の向きの変化を検出する方法
Androidアプリ開発では、端末を回転させることで画面の向きが「縦向き(ポートレート)」と「横向き(ランドスケープ)」の間で切り替わります。本記事では、Kotlinを使ってこの向きの変化を検出し、Toastで現在の向きを通知するシンプルなサンプルアプリの作成手順を解説します。
手順1:新しいプロジェクトを作成する
Android Studioを起動し、メニューから「File」→「New Project」を選択して、必要な項目を入力しながら新しいプロジェクトを作成します。テンプレートは「Empty Activity」を選んでおけば問題ありません。
手順2:レイアウトファイル(res/layout/activity_main.xml)を編集する
以下のコードを res/layout/activity_main.xml に追加します。画面上部にアプリ名、中央に「Hello World!」を表示する、2つのTextViewを持つシンプルな構成です。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:app="https://schemas.android.com/apk/res-auto"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:text="Tutorials Point"
android:textAlignment="center"
android:textColor="@android:color/holo_green_dark"
android:textSize="32sp"
android:textStyle="bold" />
<TextView
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:textSize="24sp"
android:textStyle="bold" />
</RelativeLayout>
手順3:MainActivity.ktを実装する
src/MainActivity.kt に以下のコードを記述します。ポイントは onConfigurationChanged() のオーバーライドです。このコールバックは画面の向きなどのシステム設定が変更されたタイミングで呼び出されるため、ここで Configuration.ORIENTATION_LANDSCAPE と ORIENTATION_PORTRAIT を判定すれば、向きの変化を検出できます。
import android.content.res.Configuration
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
title = "KotlinApp"
}
override fun onConfigurationChanged(newConfig: Configuration) {
super.onConfigurationChanged(newConfig)
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Toast.makeText(baseContext, "Landscape Mode", Toast.LENGTH_SHORT).show()
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
Toast.makeText(baseContext, "Portrait Mode", Toast.LENGTH_SHORT).show()
}
}
}
手順4:AndroidManifest.xmlを編集する
AndroidManifest.xml に以下のコードを追加します。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="app.com.q11">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:configChanges="orientation|screenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
注意:onConfigurationChanged() を確実に受け取るには、activityタグに android:configChanges="orientation|screenSize" を指定することが重要です。この属性を指定しない場合、向きが変わるとActivityが一度破棄されて再生成されるため、コールバックが呼び出されません。
アプリを実行して動作を確認する
それではアプリを実行してみましょう。ここでは、実機のAndroidスマートフォンをPCに接続しているものとして説明します。Android Studioからアプリを起動するには、プロジェクト内のアクティビティファイルを開き、ツールバーの実行アイコン
をクリックしてください。デバイスの選択画面で接続したスマートフォンを指定すると、実機にアプリの初期画面が表示されます。


端末を横向きに回転させると「Landscape Mode」、縦向きに戻すと「Portrait Mode」というToastメッセージが表示されれば、画面の向きの変化検出は成功です。
-
【Android】ボタン操作で画面の向き(縦向き・横向き)をプログラム的に切り替える方法
この記事では、Androidアプリでボタンをタップするだけで画面の向き(縦向き・横向き)をプログラム的に切り替える方法を解説します。画面の向きを切り替える仕組み画面の向きの変更には、ActivityクラスのsetRequestedOrientation()メソッドを使用します。引数にActivityInfo.SCREEN_ORIENTATION_PORTRAIT(縦向き)またはActivityInfo.SCREEN_ORIENTATION_LANDSCAPE(横向き)を指定することで、任意のタイミングで画面の向きを固定できます。ステップ1:新規プロジェクトを作成するAndroid Studio
-
【Android開発】画面の向きの変更を無効にして固定する方法を解説
このチュートリアルでは、Androidアプリで画面の向き(オリエンテーション)の変更を無効にし、特定の向きに固定する方法を解説します。向きの固定には、Javaコードから制御する方法と、AndroidManifest.xmlで宣言する方法の2通りがあり、本記事では両方の手法をステップごとに紹介します。 ステップ1:新しいプロジェクトを作成する Android Studioを起動し、メニューから「File」⇒「New Project」を選択します。必要な項目をすべて入力して、新しいプロジェクトを作成しましょう。 ステップ2:レイアウトファイルを編集する res/layout/activity_ma