이 튜토리얼에서는 Android 앱에서 버튼 클릭 한 번으로 화면 방향(orientation)을 세로(Portrait) 모드 또는 가로(Landscape) 모드로 전환하는 방법을 단계별로 살펴봅니다.
핵심은 setRequestedOrientation() 메서드입니다. 이 메서드에 ActivityInfo 클래스가 제공하는 방향 상수를 인자로 전달하면, 기기의 물리적 회전 상태와 관계없이 원하는 방향으로 화면을 고정하거나 변경할 수 있습니다.
1단계 — 새 프로젝트 생성
Android Studio에서 File ⇒ New Project를 선택하고, 빈 액티비티(Empty Activity) 템플릿을 기준으로 프로젝트 생성에 필요한 정보를 모두 입력해 새 프로젝트를 만듭니다.
2단계 — 레이아웃 작성 (res/layout/activity_main.xml)
화면 방향을 전환할 두 개의 버튼을 배치합니다. 두 번째 버튼에는 layout_below 속성을 추가해 첫 번째 버튼과 겹치지 않도록 정렬했습니다.
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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" tools:context=".MainActivity"> <Button android:id="@+id/buttonSetPortrait" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="62dp" android:text="세로 모드로 변경" android:textStyle="bold" /> <Button android:id="@+id/buttonSetLandscape" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/buttonSetPortrait" android:layout_centerHorizontal="true" android:layout_marginTop="24dp" android:text="가로 모드로 변경" android:textStyle="bold" /> </RelativeLayout>
3단계 — MainActivity.java 작성
각 버튼에 클릭 리스너를 등록하고, 클릭 시 setRequestedOrientation()을 호출해 방향을 전환합니다.
package app.com.sample;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button buttonSetPortrait = findViewById(R.id.buttonSetPortrait);
Button buttonSetLandscape = findViewById(R.id.buttonSetLandscape);
// 세로 모드 버튼
buttonSetPortrait.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
});
// 가로 모드 버튼
buttonSetLandscape.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
});
}
}4단계 — AndroidManifest.xml 설정
매니페스트 파일은 별도의 권한 없이 기본 구성 그대로 사용하면 됩니다. 참고로 여기서는 코드로 방향을 동적으로 제어하므로, activity 태그에 android:screenOrientation 속성을 미리 지정하지 않습니다.
<?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"> <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(실행) 아이콘을 클릭하고, 목록에서 본인의 모바일 기기를 선택하세요. 그러면 기기에 아래와 같은 기본 화면이 표시됩니다.

[세로 모드로 변경] 버튼을 누르면 화면이 세로 방향으로 고정되고, [가로 모드로 변경] 버튼을 누르면 즉시 가로 방향으로 전환됩니다.

추가로 알아두면 좋은 것
- SCREEN_ORIENTATION_LANDSCAPE / PORTRAIT: 각각 가로·세로 방향으로 강제 고정합니다.
- SCREEN_ORIENTATION_UNSPECIFIED: 시스템 기본 동작에 맡깁니다.
- SCREEN_ORIENTATION_SENSOR: 기기 센서 값에 따라 자동 회전합니다.
- SCREEN_ORIENTATION_FULL_SENSOR: 4방향(위아래 포함) 모두 센서 기반으로 회전을 허용합니다.
또한 방향이 바뀌면 액티비티가 재생성(recreate)되므로, 화면 회전 시 데이터를 유지해야 한다면 onSaveInstanceState()나 ViewModel 활용을 함께 검토하는 것이 좋습니다.