ダイアログをオリジナルのデザインで使いたい時に必要になるのがこのCustomDialogになります。
デフォルトのDialogのイメージ
デフォルトのDialog | オリジナルのDialog |
---|---|
作り方
MainActivity.kt
package com.example.myapplication
import android.os.Bundle
import android.widget.TextView
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val dialogView = findViewById<TextView>(R.id.dialog)
val customDialogView = findViewById<TextView>(R.id.custom_dialog)
dialogView.setOnClickListener {
AlertDialog.Builder(this)
.setTitle("ダイアログ表示")
.setMessage("表示成功")
.setPositiveButton("OK", { dialog, which ->
})
.show()
}
customDialogView.setOnClickListener {
customDialog()
}
}
fun customDialog() {
// customDialogFragmentを呼ぶ
val dialog = CustomDialog()
dialog.show(supportFragmentManager,"custom_dialog")
}
}
package com.example.myapplication
import android.app.Dialog
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.Window
import android.view.WindowManager
import android.widget.Button
import androidx.fragment.app.DialogFragment
class CustomDialog : DialogFragment() {
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val dialog: Dialog
dialog = Dialog(context!!)
val dw = dialog.window
dw?.let {
it.requestFeature(Window.FEATURE_NO_TITLE)
it.setFlags(
WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
)
it.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT));
}
dialog.setContentView(R.layout.custom_dialog_item)
dialog.findViewById<Button>(R.id.button).setOnClickListener {
dialog.dismiss()
}
return dialog
}
}
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">
<TextView
android:id="@+id/dialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="デフォルトのダイアログ"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@+id/custom_dialog"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
<TextView
android:id="@+id/custom_dialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="カスタムのダイアログ"
app:layout_constraintTop_toBottomOf="@+id/dialog"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/custom_dialog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="40dp"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingBottom="20dp"
android:background="@color/white"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent">
<TextView
android:id="@+id/modal_title"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Custom Dialog"
android:gravity="center"
android:textSize="17dp"
android:textColor="@color/black"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<TextView
android:id="@+id/modal_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Dialog表示成功!"
android:textColor="@color/black"
android:gravity="center"
android:textFontWeight="500"
android:textSize="14dp"
android:layout_marginTop="20dp"
app:layout_constraintTop_toBottomOf="@id/modal_title"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<Button
android:id="@+id/button"
android:layout_width="250dp"
android:layout_height="48dp"
android:gravity="center"
android:textColor="@color/white"
android:text="OK"
android:textSize="14dp"
android:layout_marginTop="16dp"
app:layout_constraintTop_toBottomOf="@+id/modal_text"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>