// 1. Create a style in styles.xml for the custom AlertDialog
<style name="CustomAlertDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
<!-- Customize your AlertDialog here -->
<item name="android:windowBackground">@drawable/dialog_background</item>
<item name="android:windowAnimationStyle">@style/DialogAnimation</item>
<!-- Add more customizations as needed -->
</style>
// 2. Define custom animations for the AlertDialog (if needed)
<style name="DialogAnimation">
<!-- Define your animation resources -->
<item name="android:windowEnterAnimation">@anim/dialog_enter</item>
<item name="android:windowExitAnimation">@anim/dialog_exit</item>
</style>
// 3. Create drawable resources for dialog_background (background shape)
// Place the required shape or drawable for the AlertDialog background
// Example: dialog_background.xml in res/drawable
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<!-- Define background properties -->
<solid android:color="@color/your_dialog_background_color" />
<corners android:radius="8dp" />
</shape>
// 4. Create animation resources for dialog_enter and dialog_exit (if needed)
// Example: dialog_enter.xml and dialog_exit.xml in res/anim
// Define enter and exit animations using translate, alpha, scale, or other animations
<set xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Define enter animation -->
<alpha android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="300" />
<!-- Define exit animation -->
<alpha android:fromAlpha="1.0" android:toAlpha="0.0" android:duration="300" />
</set>
// 5. Apply the CustomAlertDialogTheme to your AlertDialog.Builder
val alertDialogBuilder = AlertDialog.Builder(context, R.style.CustomAlertDialogTheme)
// Customize your AlertDialog using alertDialogBuilder methods
alertDialogBuilder.setTitle("Title")
alertDialogBuilder.setMessage("Message")
alertDialogBuilder.setPositiveButton("OK") { dialog, _ ->
// Handle positive button click
dialog.dismiss()
}
alertDialogBuilder.setNegativeButton("Cancel") { dialog, _ ->
// Handle negative button click
dialog.dismiss()
}
val alertDialog = alertDialogBuilder.create()
alertDialog.show()