Android/Java
안드로이드 Bottom Sheet Dialog
easy-1
2021. 6. 10. 17:00
<개요>
하단에서 올라오는 다이얼로그 안내창 생성
<적용 방법>
1. Bottom Sheet Dialog 사용을 위한 라이브러리 추가
implementation 'com.google.android.material:material:1.1.0'
2. Bottom Sheet Dialog 레이아웃 생성
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable
name="dialog"
type="com.android.example.BSDialog" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/titleTv"
style="@style/BSDialogTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="안내"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/contentTv"
style="@style/BSDialogContent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/title_tv" />
<View
android:id="@+id/view"
android:layout_width="match_parent"
android:layout_height="0.1dp"
android:layout_marginTop="20dp"
android:background="#8A8A8A"
app:layout_constraintTop_toBottomOf="@+id/contentTv" />
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/button_layout"
android:layout_width="match_parent"
android:layout_height="50dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@+id/view">
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:padding="12dp"
android:src="@drawable/ic_submit"
app:layout_constraintBottom_toBottomOf="@+id/accept_btn"
app:layout_constraintEnd_toEndOf="@+id/accept_btn"
app:layout_constraintStart_toStartOf="@+id/accept_btn"
app:layout_constraintTop_toTopOf="@+id/accept_btn" />
<Button
android:id="@+id/acceptBtn"
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="?attr/selectableItemBackground"
android:textColor="@color/BlueText"
android:textSize="14sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
3. BottomSheetDialogFragment 클래스 생성
package com.android.example;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import com.comms.user.shift.R;
import com.comms.user.shift.databinding.BsdialogBtn1Binding;
import com.google.android.material.bottomsheet.BottomSheetDialogFragment;
public class BSDialog extends BottomSheetDialogFragment {
private BsdialogBinding binding;
private Context context;
private String content;
private View.OnClickListener cl;
public BSDialog(Context context,
String content,
View.OnClickListener cl
) {
this.context = context;
this.content = content;
this.cl = cl;
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
binding = DataBindingUtil.inflate(inflater, R.layout.bsdialog, container, false);
// 다이얼로그 내용
binding.contentTv.setText(content);
// 확인 버튼
binding.acceptBtn.setOnClickListener(cl);
return binding.getRoot();
}
}