Android에서 화면 방향 변경 감지하기
이 튜토리얼에서는 Android 앱에서 레이아웃의 방향(세로 모드·가로 모드) 변경을 감지하고, 이에 맞춰 동작을 처리하는 방법을 단계별로 알아봅니다.
핵심 원리는 간단합니다. onConfigurationChanged() 콜백 메서드를 오버라이드하여 새로운 설정 값의 방향을 확인하고, 매니페스트 파일에 android:configChanges 속성을 지정하면 됩니다. 그럼 전체 구현 과정을 살펴보겠습니다.
1단계 — 새 프로젝트 생성
Android Studio를 실행한 뒤 File ⇒ New Project 메뉴로 이동하여 새 프로젝트를 만들고, 프로젝트 생성에 필요한 모든 세부 정보를 입력합니다.
2단계 — activity_main.xml 작성
res/layout/activity_main.xml 파일에 아래 코드를 추가합니다.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:textSize="24sp"
android:textStyle="bold"/>
</LinearLayout>
3단계 — MainActivity.java 작성
src/MainActivity.java 파일에 다음 코드를 추가합니다. 여기서 핵심은 onConfigurationChanged() 메서드를 오버라이드하여, 전달된 Configuration 객체의 방향 값이 가로(ORIENTATION_LANDSCAPE)인지 세로(ORIENTATION_PORTRAIT)인지 판별하는 것입니다.
import androidx.appcompat.app.AppCompatActivity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Toast.makeText(MainActivity.this, "Landscape Mode", Toast.LENGTH_SHORT).show();
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
Toast.makeText(MainActivity.this, "Portrait Mode", Toast.LENGTH_SHORT).show();
}
}
}
4단계 — AndroidManifest.xml 수정
androidManifest.xml 파일의 액티비티 태그에 android:configChanges="orientation|screenSize" 속성을 추가합니다. 이 속성이 선언되어야 화면이 회전할 때 액티비티가 재시작되지 않고, 대신 onConfigurationChanged() 콜백이 호출됩니다.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="app.com.sample">
<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>
앱 실행 및 결과 확인
이제 애플리케이션을 실행해 보겠습니다. 실제 Android 기기가 컴퓨터에 연결되어 있다고 가정합니다. Android Studio에서 프로젝트의 액티비티 파일 중 하나를 연 후, 툴바의 Run
아이콘을 클릭합니다. 목록에서 본인의 모바일 기기를 선택하면, 기기 화면에 기본 화면이 표시됩니다.


기기를 가로로 회전하면 'Landscape Mode' 토스트 메시지가, 다시 세로로 돌리면 'Portrait Mode' 토스트 메시지가 표시되는 것을 확인할 수 있습니다. 이처럼 onConfigurationChanged()를 활용하면 화면 방향 변화에 유연하게 대응하는 앱을 만들 수 있습니다.