How to set theme in AlertDialog in Android
Themes for the AlertDialog.Builder are only available for Android 3.0 and newer (API level 11). It seems you are have set an earlier Android version in your project settings.
The Android references show the API level of all constructors and methods. You can even set a filter to show you only the methods available for your API level.
Read more about API levels here.
Alert Dialog background theme/Color
Create your style in your styles.xml
file as follows.
<style name="AlertDialogCustom" parent="@android:style/Theme.Dialog">
<item name="android:textColor">@color/White</item>
<item name="android:textStyle">bold</item>
<item name="android:headerDividersEnabled">true</item>
<item name="android:typeface">normal</item>
<item name="android:background">@color/colorPrimaryDark</item>
</style>
Then Create Alert Dialog
using Builder as follows
AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(this/getActvity(), R.style.AlertDialogCustom));
Here passing the current class Context and style to the ContextThemeWrapper
class constructor.
How to use and style new AlertDialog from appCompat 22.1 and above
When creating the AlertDialog
you can set a theme to use.
Example - Creating the Dialog
AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.MyAlertDialogStyle);
builder.setTitle("AppCompatDialog");
builder.setMessage("Lorem ipsum dolor...");
builder.setPositiveButton("OK", null);
builder.setNegativeButton("Cancel", null);
builder.show();
styles.xml - Custom style
<style name="MyAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
<!-- Used for the buttons -->
<item name="colorAccent">#FFC107</item>
<!-- Used for the title and text -->
<item name="android:textColorPrimary">#FFFFFF</item>
<!-- Used for the background -->
<item name="android:background">#4CAF50</item>
</style>
Result
Edit
In order to change the Appearance of the Title, you can do the following. First add a new style:
<style name="MyTitleTextStyle">
<item name="android:textColor">#FFEB3B</item>
<item name="android:textAppearance">@style/TextAppearance.AppCompat.Title</item>
</style>
afterwards simply reference this style in your MyAlertDialogStyle
:
<style name="MyAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
...
<item name="android:windowTitleStyle">@style/MyTitleTextStyle</item>
</style>
This way you can define a different textColor
for the message via android:textColorPrimary
and a different for the title via the style.
MaterialComponents theme alert dialog buttons
I figured out what was causing this problem. I need to use different AlertDialog class:
androidx.appcompat.app.AlertDialog
When I switched to this everything started working as expected. Here's where I found the solution:
https://github.com/material-components/material-components-android/issues/162
what theme to use for the AlertDialog to adjust the color automatically with day/night theme
Define your alert dialog in styles.xml
<style name="MyDialogStyle" parent="Theme.AppCompat.DayNight.Dialog.Alert"/>
In your code
AlertDialog.Builder(this, R.style.MyDialogStyle)
And give it a try!
Related Topics
How to Check Visibility of Software Keyboard in Android
Sha-1 Fingerprint of Keystore Certificate
Detect Whether There Is an Internet Connection Available on Android
How to Make an Imageview With Rounded Corners
All Com.Android.Support Libraries Must Use the Exact Same Version Specification
Activity Has Leaked Window That Was Originally Added
Static Way to Get 'Context' in Android
Android Studio: Add Jar as Library
What Does Android:Layout_Weight Mean
How to Get a List of Installed Android Applications and Pick One to Run
"Cannot Resolve Symbol R" in Android Studio
How to Stop Edittext from Gaining Focus When an Activity Starts in Android
Navigationview Get/Find Header Layout
How to Make a Custom Keyboard in Android