728x90
gradle
implementation 'com.google.android.material:material:1.1.0'
MainActivity
class MainActivity : AppCompatActivity() {
private lateinit var bottomSheetBtn: Button
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
bottomSheetBtn = findViewById(R.id.bottom_sheet_btn)
val bottomSheetDialogFragment = TestBottomSheetDialogFragment(this)
bottomSheetBtn.setOnClickListener {
bottomSheetDialogFragment.show(supportFragmentManager,bottomSheetDialogFragment.tag)
}
}
}
TestBottomSheetDialogFragment
class TestBottomSheetDialogFragment(context : Context) : BottomSheetDialogFragment() {
private val mContext: Context = context
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view = inflater.inflate(R.layout.fragment_test_bottom_sheet_dialog, container, false)
val btnOK: Button = view.findViewById(R.id.success_btn)
val btnClose: Button = view.findViewById(R.id.failure_btn)
btnOK.setOnClickListener {
Toast.makeText(mContext, "확인", Toast.LENGTH_SHORT).show()
dismiss()
}
btnClose.setOnClickListener {
Toast.makeText(mContext, "닫기", Toast.LENGTH_SHORT).show()
dismiss()
}
return view
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/bottom_sheet_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bottom Sheet Dialog"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
fragment_test_bottom_sheet_dialog
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<Button
android:id="@+id/success_btn"
android:text="확인"
android:layout_margin="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/failure_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="취소"
android:layout_margin="10dp"
app:layout_constraintEnd_toEndOf="parent" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
'안드로이드 코틀린' 카테고리의 다른 글
[kotlin][Android] 안드로이드에서 긴 글 가져오기 (0) | 2022.07.07 |
---|---|
[kotlin][Android] Activity 한번에 종료하기 (0) | 2022.06.28 |
[Kotlin][Android] 디바이스 전체 화면 길이 구하기 (0) | 2022.06.18 |
[Kotlin][Android] Lambda 사용해서 계산 기능 만들기 (0) | 2022.06.13 |
[Java][Android] circle-menu 만들기 (0) | 2022.06.12 |