40

I've breaking my head over this quite a bit. What I need to do is, change the style of all AlertDialogs in my android application - dialog background needs to be white-ish, and text needs to be black-ish. I tried creating a lot of styles, themes, and applying from the code, manifest, etc, but no success, with regard to the text colors inside the AlertDialog. Right now, I have the simplest of codes, set like this:

Manifest:

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

styles.xml:

<style name="AppTheme" parent="AppBaseTheme">
    <item name="android:alertDialogStyle">@style/DialogStyle</item>
</style>

<style name="DialogStyle" parent="@android:style/Theme.Dialog">
    <!-- changing these background stuff works fine -->
    <item name="android:bottomBright">@android:color/white</item>
    <item name="android:bottomDark">@android:color/white</item>
    <item name="android:bottomMedium">@drawable/dialog_footer_bg</item>
    <item name="android:centerBright">@android:color/white</item>
    <item name="android:centerDark">@drawable/dialog_body_bg</item>
    <item name="android:centerMedium">@android:color/white</item>
    <item name="android:fullBright">@color/orange</item>
    <item name="android:fullDark">@color/orange</item>
    <item name="android:topBright">@color/green</item>
    <item name="android:topDark">@drawable/dialog_header_bg</item>

The items listed below don't work (please read the comments I've put above each element):

    <!-- panelBackground is not getting set to null, there is something squarish around it -->
    <item name="android:panelBackground">@null</item>

    <!-- Setting this textColor doesn't seem to have any effect at all. Messages, title, button text color, whatever; nothing changes. -->
    <item name="android:textColor">#000000</item>

    <!-- Also tried with textAppearance, as follows. Didn't work -->
    <item name="android:textAppearance">?android:attr/textColorPrimaryInverse</item>

    <!-- Also tried changing textAppearancePrimary, to no avail -->
    <item name="android:textColorPrimary">#000000</item>

    <!-- Also need to change the dialog title text, tried it as follows, dint work: -->
    <item name="android:windowTitleStyle">@style/DialogWindowTitle</item>
</style>

The DialogWindowTitle is defined as follows:

<style name="DialogWindowTitle">
    <item name="android:textAppearance">?android:attr/textAppearanceMediumInverse</item>
</style>

So none of these is working. Can anyone tell me what I could be doing wrong, and how can I:

  1. Change text color for messages (content text)
  2. Change title text color
  3. Remove the panel background

Note: I need to support API 8 (2.2) upwards. Also, I've went through most of the related question here, and google groups, but can't figure out, though I have a feeling its right under my nose!

Edit: adding screenshot:

AlertDialog not themed as expected

Community
  • 1
  • 1
Aswin Kumar
  • 5,158
  • 5
  • 34
  • 39
  • Can you provide a screenshot of how it currently looks? – Ahmad Apr 24 '13 at 21:10
  • @Ahmad: Screenshot added – Aswin Kumar Apr 25 '13 at 02:31
  • i wanna know how you got those background colors to change. using this exact same code, i cannot replicate a colored AlertDialog against API level 9. did you apply the theme or style some special way? what is your target and min sdk level set to? – moonlightcheese Jan 08 '14 at 12:29
  • weird... somehow i got it working and i have no idea how. thanks a lot for this post, i was scratching my head for hours on end over this. for anyone searching, this question/answer is the definitive way to customize AlertDialogs in Android API levels < 11. – moonlightcheese Jan 08 '14 at 14:16
  • 1
    ...but for some reason, if i run this code on any device running Android >= 3.0, the dialog colors do not change. so it seems this has to be handled two different ways for >2.3.3 and <2.3.3... maybe in separate APKs? what a mess... they really screwed this up between Android versions. – moonlightcheese Jan 08 '14 at 15:19
  • @moonlightcheese I think the properties under DialogStyle are only for non-fragment-based dialogs, that's why they wouldn't work on post honeycomb devices. You don't need several apks, but just to define additional properties for the fragment-based dialogs. They won't bother each other, and if they would, you can always use resource qualifiers. Have a look to [Dadou's answer](http://stackoverflow.com/a/24560017/467900) to see new Dialog's themes and related styles. – mdelolmo Oct 29 '14 at 10:39

8 Answers8

50

You need to define a Theme for your AlertDialog and reference it in your Activity's theme. The attribute is alertDialogTheme and not alertDialogStyle. Like this:

<style name="Theme.YourTheme" parent="@android:style/Theme.Holo">
    ...
    <item name="android:alertDialogTheme">@style/YourAlertDialogTheme</item>
</style>

<style name="YourAlertDialogTheme">
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
    <item name="android:windowMinWidthMajor">@android:dimen/dialog_min_width_major</item>
    <item name="android:windowMinWidthMinor">@android:dimen/dialog_min_width_minor</item>
    <item name="android:windowTitleStyle">...</item>
    <item name="android:textAppearanceMedium">...</item>
    <item name="android:borderlessButtonStyle">...</item>
    <item name="android:buttonBarStyle">...</item>
</style>

You'll be able to change color and text appearance for the title, the message and you'll have some control on the background of each area. I wrote a blog post detailing the steps to style an AlertDialog.

David Ferrand
  • 5,357
  • 1
  • 33
  • 43
  • 4
    blog post got me exactly what I needed. I shouldve been using android:textColorPrimary and android:textColorAlertDialogListItem to style the contents of my dialog. – Nlinscott Dec 12 '14 at 06:51
  • 1
    alertDialogTheme is api11 – sherpya Mar 15 '15 at 04:07
  • `alertDialogTheme `means everything else. – zionpi Jun 24 '15 at 13:34
  • 1
    setting `android:textAppearanceMedium` doesn't work for me when using app compat alert dialog: http://stackoverflow.com/questions/35179537/android-message-font-size-in-alertdialog-through-styles – Tomask Feb 03 '16 at 16:50
  • that's correct using Material theme you cannot use `android:textAppearanceMedium` to change the content style, see this thread for a workaround http://stackoverflow.com/questions/35179537/android-message-font-size-in-alertdialog-through-styles/36749547#36749547 – Sergio Serra Apr 20 '16 at 16:14
  • But how to change font typeface of buttons? – Dr.jacky May 09 '18 at 04:55
14

Remove the panel background

 <item name="android:windowBackground">@color/transparent_color</item> 
 <color name="transparent_color">#00000000</color>

This is Mystyle:

 <style name="ThemeDialogCustom">
    <item name="android:windowFrame">@null</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
    <item name="android:windowBackground">@color/transparent_color</item>
    <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
    <item name="android:colorBackgroundCacheHint">@null</item>
</style>

Which i have added to the constructor.

Add textColor :

<item name="android:textColor">#ff0000</item>
Terril Thomas
  • 1,486
  • 13
  • 32
  • 1
    `name="android:textColor"` line worked for me. `@color/profileWhite` was editing only text message color. – LadyWoodi Oct 28 '16 at 09:04
10

Here's my Code to theme the alert dialog box:

<style name="alertDialog" parent="Theme.AppCompat.Dialog.Alert">
    <item name="android:background">@color/light_button_text_color</item>
    <item name="android:textColor">@android:color/black</item>
    <item name="android:textColorPrimary">@android:color/black</item>
    <item name="android:textColorSecondary">@android:color/black</item>
    <item name="android:titleTextColor" tools:targetApi="m">@android:color/black</item>
</style>

Place this code in styles.xml. In your java apply this theme as:

AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.alertDialog);

Output of the code

Anuj Gupta
  • 576
  • 5
  • 9
9

You have to add the style to the constructor of the dialog

builder = new AlertDialog.Builder(this, R.style.DialogStyle);
general03
  • 855
  • 1
  • 10
  • 33
  • note that use of this constructor is only available in API level 11+ – moonlightcheese Jan 08 '14 at 00:57
  • You're right, i looked for a solution several days without solution. So I use a basic Dialog with my custom view in xml. Like this `final ContextThemeWrapper l_contextThemeWrapper = new ContextThemeWrapper(this, R.style.myDialogStyle ); dialog = new Dialog(l_contextThemeWrapper); dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); dialog.setContentView(R.layout.dialog);` – general03 Jan 08 '14 at 08:54
5

I changed color programmatically in this way :

var builder = new AlertDialog.Builder (this);
...
...
...
var dialog = builder.Show ();
int textColorId = Resources.GetIdentifier ("alertTitle", "id", "android");
TextView textColor = dialog.FindViewById<TextView> (textColorId);
textColor?.SetTextColor (Color.DarkRed);

as alertTitle, you can change other data by this way (next example is for titleDivider):

int titleDividerId = Resources.GetIdentifier ("titleDivider", "id", "android");
View titleDivider = dialog.FindViewById (titleDividerId);
titleDivider?.SetBackgroundColor (Color.Red);

this is in C#, but in java it is the same.

StefanoM5
  • 1,327
  • 1
  • 24
  • 34
  • 2
    This is the java version of the code - `int textColorId = getResources().getIdentifier("alertMessage", "id", "android"); TextView textColor = (TextView) alertDialog.findViewById(textColorId); if (textColor != null) { textColor.setTextColor(Color.RED); }` – Rishabh Wadhwa Jul 28 '17 at 12:12
3

It depends how much you want to customize the alert dialog. I have different steps in order to customize the alert dialog. Please visit: https://stackoverflow.com/a/33439849/5475941

enter image description here enter image description here enter image description here enter image description here

Community
  • 1
  • 1
Mohammad
  • 6,024
  • 3
  • 22
  • 30
2

Building on @general03's answer, you can use Android's built-in style to customize the dialog quickly. You can find the dialog themes under android.R.style.Theme_DeviceDefault_Dialogxxx.

For example:

builder = new AlertDialog.Builder(this, android.R.style.Theme_DeviceDefault_Dialog_MinWidth);
builder = new AlertDialog.Builder(this, android.R.style.Theme_DeviceDefault_Dialog_NoActionBar);
builder = new AlertDialog.Builder(this, android.R.style.Theme_DeviceDefault_DialogWhenLarge);
Jeshurun
  • 22,940
  • 6
  • 79
  • 92
1

Use this in your Style in your values-v21/style.xml

<style name="AlertDialogCustom" parent="@android:style/Theme.Material.Dialog.NoActionBar">
        <item name="android:windowBackground">@android:color/white</item>
        <item name="android:windowActionBar">false</item>
        <item name="android:colorAccent">@color/cbt_ui_primary_dark</item>
        <item name="android:windowTitleStyle">@style/DialogWindowTitle.Sphinx</item>
        <item name="android:textColorPrimary">@color/cbt_hints_color</item>
        <item name="android:backgroundDimEnabled">true</item>
        <item name="android:windowMinWidthMajor">@android:dimen/dialog_min_width_major</item>
        <item name="android:windowMinWidthMinor">@android:dimen/dialog_min_width_minor</item>
</style>

And for pre lollipop devices put it in values/style.xml

<style name="AlertDialogCustom" parent="@android:style/Theme.Material.Dialog.NoActionBar">
        <item name="android:windowBackground">@android:color/white</item>
        <item name="android:windowActionBar">false</item>
        <item name="android:colorAccent">@color/cbt_ui_primary_dark</item>
        <item name="android:windowTitleStyle">@style/DialogWindowTitle.Sphinx</item>
        <item name="android:textColorPrimary">@color/cbt_hints_color</item>
        <item name="android:backgroundDimEnabled">true</item>
        <item name="android:windowMinWidthMajor">@android:dimen/dialog_min_width_major</item>
        <item name="android:windowMinWidthMinor">@android:dimen/dialog_min_width_minor</item>
</style>

<style name="DialogWindowTitle.Sphinx" parent="@style/DialogWindowTitle_Holo">
       <item name="android:textAppearance">@style/TextAppearance.Sphinx.DialogWindowTitle</item>
</style>

<style name="TextAppearance.Sphinx.DialogWindowTitle" parent="@android:style/TextAppearance.Holo.DialogWindowTitle">
        <item name="android:textColor">@color/dark</item>
        <!--<item name="android:fontFamily">sans-serif-condensed</item>-->
        <item name="android:textStyle">bold</item>
</style>
msmukesh4
  • 589
  • 4
  • 7