이 튜토리얼에서는 Configuration 클래스를 활용해 Android 기기의 화면 크기 범주(small, normal, large, xlarge)를 프로그래밍 방식으로 확인하는 방법을 단계별로 살펴봅니다. 추가로 화면 밀도(density)까지 함께 조회하는 예제도 포함되어 있습니다.
1단계 — 새 프로젝트 만들기
Android Studio를 실행한 뒤 File ⇒ New Project 메뉴로 이동하고, 새 프로젝트 생성에 필요한 모든 항목을 입력해 프로젝트를 만듭니다.
2단계 — 레이아웃 파일 작성
res/layout/activity_main.xml 파일에 아래 코드를 추가합니다. 버튼 두 개와 안내용 TextView로 구성된 간단한 레이아웃입니다.
<?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"
android:padding="16dp"
tools:context=".MainActivity">
<Button
android:id="@+id/btnGetScreenSize"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:onClick="GetScreenSize"
android:text="Get ScreenSize" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/btnGetScreenSize"
android:layout_centerInParent="true"
android:layout_marginTop="10dp"
android:onClick="GetScreenDensity"
android:text="Get Screen Density" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:text="Android device screen size category (small, normal, large, xlarge)"
android:textAlignment="center"
android:textSize="24sp"
android:textStyle="bold|italic" />
</RelativeLayout>3단계 — MainActivity 구현
src/MainActivity.java에 아래 코드를 추가합니다. 핵심은 getResources().getConfiguration().screenLayout 값에 SCREENLAYOUT_SIZE_MASK를 AND 연산하여 화면 크기를 판별하는 부분입니다.
import androidx.appcompat.app.AppCompatActivity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void GetScreenSize(View view) {
if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE) {
Toast.makeText(this, "Large screen", Toast.LENGTH_LONG).show();
}
else if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_NORMAL) {
Toast.makeText(this, "Normal sized screen", Toast.LENGTH_LONG).show();
}
else if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_SMALL) {
Toast.makeText(this, "Small sized screen", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, "Screen size is neither large, normal or small", Toast.LENGTH_LONG).show();
}
}
public void GetScreenDensity(View view) {
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int density = metrics.densityDpi;
if (density == DisplayMetrics.DENSITY_HIGH) {
Toast.makeText(this, "DENSITY_HIGH... Density is " + (density), Toast.LENGTH_LONG).show();
}
else if (density == DisplayMetrics.DENSITY_MEDIUM) {
Toast.makeText(this, "DENSITY_MEDIUM... Density is " + (density), Toast.LENGTH_LONG).show();
}
else if (density == DisplayMetrics.DENSITY_LOW) {
Toast.makeText(this, "DENSITY_LOW... Density is " + (density), Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(this, "Density is neither HIGH, MEDIUM OR LOW. Density is " + (density), Toast.LENGTH_LONG).show();
}
}
}위 코드에서 GetScreenSize() 메서드는 현재 기기가 large, normal, small 중 어느 범주에 속하는지 판별하며, 어느 것에도 해당하지 않으면(예: xlarge) 마지막 분기가 실행됩니다. 참고로 xlarge 여부를 명시적으로 확인하려면 Configuration.SCREENLAYOUT_SIZE_XLARGE 상수를 비교 조건에 추가하면 됩니다.
GetScreenDensity() 메서드는 DisplayMetrics를 통해 화면 밀도(DPI)를 가져와 HIGH(240dpi), MEDIUM(160dpi), LOW(120dpi) 중 어느 등급인지 출력합니다.
4단계 — 매니페스트 설정
androidManifest.xml에 아래 코드를 추가합니다.
<?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 아이콘을 클릭하세요. 실행 옵션에서 연결된 모바일 기기를 선택하면, 기기에 아래와 같은 기본 화면이 표시됩니다.

버튼을 누르면 현재 기기의 화면 크기 범주와 밀도가 Toast 메시지로 표시되어, 리소스를 기기별로 최적화하거나 반응형 UI를 설계할 때 유용하게 활용할 수 있습니다.