개요
이 튜토리얼에서는 안드로이드 앱에서 바텀시트(Bottom Sheet) 위젯을 구현하는 방법을 단계별로 알아봅니다. 바텀시트는 화면 하단에서 위로 슬라이드되어 올라오는 UI 요소로, 주문 내역 확인, 필터 옵션 선택 등 다양한 상황에서 활용됩니다.
구현 단계
1단계: 새 프로젝트 생성
Android Studio에서 File ⇒ New Project를 선택하고, 새 프로젝트 생성에 필요한 모든 정보를 입력합니다.
2단계: activity_main.xml 작성
res/layout/activity_main.xml 파일에 아래 코드를 추가합니다. 메인 콘텐츠 뒤에 바텀시트 레이아웃을 포함(include)시키는 구조입니다.
<?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.CoordinatorLayout 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"> <android.support.design.widget.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:popupTheme="@style/Widget.Support.CoordinatorLayout" /> </android.support.design.widget.AppBarLayout> <include layout="@layout/content" /> <!-- Adding bottom sheet after main content --> <include layout="@layout/bottomsheet" /> </android.support.design.widget.CoordinatorLayout>
3단계: build.gradle 의존성 추가
build.gradle(Module: app) 파일을 열고 다음 의존성을 추가합니다.
implementation 'com.android.support:design:28.0.0' implementation 'com.jakewharton:butterknife:8.8.1' annotationProcessor 'com.jakewharton:butterknifecompiler:8.8.1'
4단계: bottomsheet.xml 레이아웃 생성
바텀시트로 표시될 레이아웃 파일(bottomsheet.xml)을 생성하고 아래 코드를 작성합니다. 핵심은 app:layout_behavior 속성으로 BottomSheetBehavior를 지정하는 것이며, behavior_peekHeight로 접힌 상태의 높이를, behavior_hideable로 숨김 가능 여부를 설정할 수 있습니다.
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:app="https://schemas.android.com/apk/res-auto" android:id="@+id/bottomSheet" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:padding="4dp" app:behavior_hideable="true" app:behavior_peekHeight="10dp" app:layout_behavior="android.support.design.widget.BottomSheetBehavior"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_gravity="center_vertical" android:weightSum="3"> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="2" android:text="Order Details" android:textColor="#444" android:textSize="18dp" android:textStyle="bold" /> <TextView android:layout_width="0dp" android:gravity="right" android:layout_height="wrap_content" android:layout_weight="1" android:textStyle="bold" android:textSize="15dp" android:text="₹435.00"></TextView> </LinearLayout> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Chicken BBQ" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Chicken Biriyani" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Delivery Address" android:textColor="#444" android:textStyle="bold" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Flat No 404, India, Chennai" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="30dp" android:background="#000" android:text="PROCEED PAYMENT" android:textColor="#fff" /> </LinearLayout>
5단계: content.xml 레이아웃 생성
메인 화면에 표시될 콘텐츠 레이아웃(content.xml)을 생성하고, 바텀시트를 여닫을 버튼을 추가합니다.
<?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:orientation="vertical" tools:context="MainActivity" tools:showIn="@layout/activity_main"> <Button android:id="@+id/btnBottomSheet" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Show Bottom Sheet" android:layout_centerInParent="true"/> </RelativeLayout>
6단계: MainActivity.java 작성
src/MainActivity.java에 다음 코드를 추가합니다. ButterKnife로 뷰를 바인딩하고, BottomSheetBehavior의 상태 변화(펼침·접힘 등)에 따라 버튼 텍스트가 변경되도록 콜백을 등록합니다.
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomSheetBehavior;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
public class MainActivity extends AppCompatActivity {
@BindView(R.id.btnBottomSheet)
Button btnBottomSheet;
@BindView(R.id.bottomSheet)
LinearLayout layoutBottomSheet;
BottomSheetBehavior sheetBehavior;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
Toolbar toolbar = findViewById(R.id.toolbar);
//setSupportActionBar(toolbar);
sheetBehavior = BottomSheetBehavior.from(layoutBottomSheet);
sheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
@Override
public void onStateChanged(@NonNull View bottomSheet, int newState) {
switch (newState) {
case BottomSheetBehavior.STATE_HIDDEN:
break;
case BottomSheetBehavior.STATE_EXPANDED: {
btnBottomSheet.setText("Close Sheet");
}
break;
case BottomSheetBehavior.STATE_COLLAPSED: {
btnBottomSheet.setText("Expand Sheet");
}
break;
case BottomSheetBehavior.STATE_DRAGGING:
break;
case BottomSheetBehavior.STATE_SETTLING:
break;
}
}
@Override
public void onSlide(@NonNull View bottomSheet, float slideOffset) { }
});
}
@OnClick(R.id.btnBottomSheet)
public void toggleBottomSheet() {
if (sheetBehavior.getState() !=
BottomSheetBehavior.STATE_EXPANDED) {
sheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
btnBottomSheet.setText("Close sheet");
} else {
sheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
btnBottomSheet.setText("Expand sheet");
}
}
}7단계: AndroidManifest.xml 설정
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 Studio에서 프로젝트의 액티비티 파일 중 하나를 연 뒤 툴바의 Run 아이콘을 클릭하고, 목록에서 자신의 모바일 기기를 선택합니다. 그러면 기기 화면에 아래와 같은 기본 화면이 표시됩니다.


참고: 최신 환경(AndroidX)에서의 적용
위 예제는 구버전 Support Library(com.android.support:design:28.0.0)를 기준으로 작성되었습니다. 최신 프로젝트에서는 AndroidX 마이그레이션이 권장되므로, Material Components 라이브러리(com.google.android.material:material)를 추가하고 CoordinatorLayout은 androidx.coordinatorlayout.widget.CoordinatorLayout, 바텀시트 동작은 com.google.android.material.bottomsheet.BottomSheetBehavior를 사용하면 동일하게 구현할 수 있습니다.