티스토리 뷰
BottomSheetDialog -> 머티리얼 디자인 -> BottomSheetDialog() 클래스 생성 -> setContentView 설정
build.gradle (Module: app)
dependencies {
implementation 'com.google.android.material:material:1.1.0'
}
BottomSheetDialog 는 머티리얼 디자인이므로 의존성 추가
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/btnClick"
android:layout_width="200dp"
android:layout_height="80dp"
android:text="클릭"
android:textSize="30dp"
android:textColor="#FFFFFF"
android:textStyle="bold"
android:background="#FF7F00"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
bottom_sheet.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"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:gravity="center"
android:padding="4dp"
android:background="#FF7F00">
<TextView
android:layout_width="320dp"
android:layout_height="160dp"
android:text="혀가 길지 않은 개발자"
android:textSize="28dp"
android:textColor="#FFFFFF"
android:textStyle="bold"
android:gravity="center"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
android:padding="20dp">
<Button
android:id="@+id/btnOK"
android:layout_width="160dp"
android:layout_height="wrap_content"
android:text="확인"
android:textSize="16dp"
android:textStyle="italic|bold"
android:textColor="#FF7F00"
android:background="#FFFFFF" />
<Button
android:id="@+id/btnClose"
android:layout_width="160dp"
android:layout_height="wrap_content"
android:text="닫기"
android:textSize="16dp"
android:textStyle="italic|bold"
android:layout_marginLeft="8dp"
android:textColor="#FF7F00"
android:background="#FFFFFF" />
</LinearLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.kt
package com.jwsoft.kotlinproject
import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.widget.Button
import android.widget.Toast
import com.google.android.material.bottomsheet.BottomSheetDialog
import kotlinx.android.synthetic.main.bottom_sheet.view.*
class MainActivity : AppCompatActivity() {
private lateinit var btnClick: Button
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
btnClick = findViewById(R.id.btnClick)
val inflater: LayoutInflater = getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
val view: View = inflater.inflate(R.layout.bottom_sheet, null, false)
val bottomSheetDialog = BottomSheetDialog(applicationContext)
bottomSheetDialog.setContentView(view)
// bottomSheetDialog.setContentView(R.layout.bottom_sheet) 이렇게 사용 가능
btnClick.setOnClickListener {
bottomSheetDialog.show()
}
view.btnOK.setOnClickListener {
Toast.makeText(applicationContext, "확인", Toast.LENGTH_SHORT).show()
bottomSheetDialog.dismiss()
}
view.btnClose.setOnClickListener {
Toast.makeText(applicationContext, "닫기", Toast.LENGTH_SHORT).show()
bottomSheetDialog.dismiss()
}
}
}
val bottomSheetDialog = BottomSheetDialog(applicationContext) -> 실행 후 버튼 클릭 시 에러 발생.
MainActivity.kt
package com.jwsoft.kotlinproject
import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.widget.Button
import android.widget.Toast
import com.google.android.material.bottomsheet.BottomSheetDialog
import kotlinx.android.synthetic.main.bottom_sheet.view.*
class MainActivity : AppCompatActivity() {
private lateinit var btnClick: Button
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
btnClick = findViewById(R.id.btnClick)
val inflater: LayoutInflater = getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
val view: View = inflater.inflate(R.layout.bottom_sheet, null, false)
val bottomSheetDialog = BottomSheetDialog(this)
bottomSheetDialog.setContentView(view)
// bottomSheetDialog.setContentView(R.layout.bottom_sheet) 이렇게 사용 가능
btnClick.setOnClickListener {
bottomSheetDialog.show()
}
view.btnOK.setOnClickListener {
Toast.makeText(applicationContext, "확인", Toast.LENGTH_SHORT).show()
bottomSheetDialog.dismiss()
}
view.btnClose.setOnClickListener {
Toast.makeText(applicationContext, "닫기", Toast.LENGTH_SHORT).show()
bottomSheetDialog.dismiss()
}
}
}
val bottomSheetDialog = BottomSheetDialog(this) -> 실행 후 버튼 클릭 시 정상 작동
'Android > Kotlin' 카테고리의 다른 글
[Kotlin] Fragment (0) | 2020.07.01 |
---|---|
[Kotlin] BottomSheetDialogFragment (0) | 2020.07.01 |
[Kotlin] SharedPreferences + Singleton Pattern (0) | 2020.06.30 |
[Kotlin] Handler vs runOnUiThread vs AsyncTask (0) | 2020.06.29 |
[Kotlin] AsyncTask (0) | 2020.06.29 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- James Kim
- 혀가 길지 않은 개발자
- ViewPager2
- JSONArray
- Architecture Pattern
- Design Pattern
- coroutine
- recyclerview
- Intent
- Livedata
- XML
- 자바
- 코틀린
- JSONObject
- handler
- 안드로이드
- Android
- activity
- fragment
- MVVM
- View
- 안드로이드 #코틀린 #Android #Kotlin
- ArrayList
- DataBinding
- java
- Vue.js #Vue.js + javascript
- Kotlin
- TabLayout
- CoordinatorLayout
- ViewModel
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
글 보관함