Android AlertDialog Styling .

Alert dialog is a subclass of dialog that can show a title, up to three buttons, a list of selectable items, or a custom layout.

In the previous tutorial we have seen basic example of Android AlertDialog , In this tutorial we will see styling of AlertDialog .


 AlertDialog.Builder class has single and double paramterized constructor .

 1.  AlertDialog.Builder(Context context)
 2.  AlertDialog.Builder(Context context, @StyleRes int themeResId) : The themeResId is used to set the theme of the Dialog .

1. Light Theme


Pass the Theme_AppCompat_Light_Dialog_Alert Theme to AlertDialog.Builder constructor .

val builder: AlertDialog.Builder =
   AlertDialog.Builder(this@MainActivity, R.style.Theme_AppCompat_Light_Dialog_Alert)


2. Dark Theme


Pass the R.style.Theme_AppCompat_Dialog_Alert Theme to AlertDialog.Builder constructor .

val builder: AlertDialog.Builder =
   AlertDialog.Builder(this@MainActivity, R.style.Theme_AppCompat_Dialog_Alert)



3. Custom Theme


<style name="CustomAlertDialog" parent="Theme.AppCompat.Dialog.Alert">
<item name="android:background">#FFEB3B</item>
<item name="android:textColor">#9C27B0</item>
<item name="android:textColorPrimary">#E91E63</item>
</style>

Pass the above defined style to AlertDialog.Builder constructor .

val builder: AlertDialog.Builder =
AlertDialog.Builder(this@MainActivity, R.style.CustomAlertDialog)


No comments:

Post a Comment