티스토리 뷰
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.java
package com.jwsoft.javaproject;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
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;
public class MainActivity extends AppCompatActivity {
Button btnClick;
Button btnOK;
Button btnClose;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnClick = findViewById(R.id.btnClick);
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.bottom_sheet, null, false);
final BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(getApplicationContext());
bottomSheetDialog.setContentView(view);
btnClick.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
bottomSheetDialog.show();
}
});
btnOK = view.findViewById(R.id.btnOK);
btnOK.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "확인", Toast.LENGTH_SHORT).show();
bottomSheetDialog.dismiss();
}
});
btnClose = view.findViewById(R.id.btnClose);
btnClose.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "닫기", Toast.LENGTH_SHORT).show();
bottomSheetDialog.dismiss();
}
});
}
}
val bottomSheetDialog = BottomSheetDialog(applicationContext) -> 실행 후 버튼 클릭 시 에러 발생.

MainActivity.java
package com.jwsoft.javaproject;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
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;
public class MainActivity extends AppCompatActivity {
Button btnClick;
Button btnOK;
Button btnClose;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnClick = findViewById(R.id.btnClick);
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.bottom_sheet, null, false);
final BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(this);
bottomSheetDialog.setContentView(view);
btnClick.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
bottomSheetDialog.show();
}
});
btnOK = view.findViewById(R.id.btnOK);
btnOK.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "확인", Toast.LENGTH_SHORT).show();
bottomSheetDialog.dismiss();
}
});
btnClose = view.findViewById(R.id.btnClose);
btnClose.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "닫기", Toast.LENGTH_SHORT).show();
bottomSheetDialog.dismiss();
}
});
}
}
val bottomSheetDialog = BottomSheetDialog(this) -> 실행 후 버튼 클릭 시 정상




'Android > Java' 카테고리의 다른 글
[Java] Fragment (0) | 2020.07.02 |
---|---|
[Java] BottomSheetDialogFragment (0) | 2020.07.01 |
[Java] SharedPreferences + Singleton Pattern (0) | 2020.06.30 |
[Java] Handler vs runOnUiThread vs AsyncTask (0) | 2020.06.29 |
[Java] AsyncTask (0) | 2020.06.29 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- James Kim
- DataBinding
- TabLayout
- coroutine
- Livedata
- 자바
- View
- JSONObject
- Kotlin
- java
- handler
- Architecture Pattern
- ArrayList
- activity
- Intent
- Vue.js #Vue.js + javascript
- fragment
- 코틀린
- 안드로이드 #코틀린 #Android #Kotlin
- 안드로이드
- MVVM
- Android
- ViewPager2
- Design Pattern
- recyclerview
- CoordinatorLayout
- ViewModel
- JSONArray
- XML
- 혀가 길지 않은 개발자
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함