이 예제는 안드로이드(Android) 앱에서 상태 표시줄(Status Bar)의 높이를 프로그래밍 방식으로 가져오는 방법을 보여줍니다.
상태 표시줄 높이는 기기 제조사와 화면 해상도에 따라 다르기 때문에, 고정된 픽셀 값을 사용하기보다는 시스템 리소스에서 직접 읽어오는 것이 가장 정확하고 안전한 방법입니다.
1단계: 새 프로젝트 생성
Android Studio를 실행하고 File → New Project 메뉴로 이동한 후, 필요한 모든 세부 정보를 입력하여 새 프로젝트를 생성합니다.
2단계: 레이아웃 파일 작성
res/layout/activity_main.xml 파일에 아래 코드를 추가합니다.
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
xmlns:app="https://schemas.android.com/apk/res-auto"
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:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>3단계: 메인 액티비티 코드 작성
src/MainActivity.java 파일에 아래 코드를 추가합니다.
핵심은 getIdentifier() 메서드를 사용해 안드로이드 시스템 내부에 정의된 status_bar_height 리소스 ID를 찾은 뒤, getDimensionPixelSize()로 실제 픽셀 크기를 가져오는 것입니다.
package com.app.sample;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.content.res.Resources;
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);
getStatusBarHeight();
}
private int getStatusBarHeight() {
int height;
Resources myResources = getResources();
int idStatusBarHeight = myResources.getIdentifier( "status_bar_height", "dimen", "android");
if (idStatusBarHeight > 0) {
height = getResources().getDimensionPixelSize(idStatusBarHeight);
Toast.makeText(this, "Status Bar Height = " + height, Toast.LENGTH_LONG).show();
} else {
height = 0;
Toast.makeText(this, "Resources NOT found", Toast.LENGTH_LONG).show();
}
return height;
}
}위 코드가 실행되면 상태 표시줄의 높이가 픽셀(px) 단위로 토스트(Toast) 메시지를 통해 화면에 표시됩니다. 만약 해당 리소스를 찾지 못하면 높이 값으로 0을 반환합니다.
4단계: 매니페스트 설정
Manifests/AndroidManifest.xml 파일에 아래 코드를 추가합니다.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android" package="com.app.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 Studio에서 프로젝트의 액티비티 파일 중 하나를 연 뒤, 툴바의 Run(실행) 아이콘을 클릭하세요. 그다음 목록에서 본인의 모바일 기기를 선택하면, 기기 화면에 상태 표시줄 높이가 토스트 메시지로 나타나는 것을 확인할 수 있습니다.
