Text color change Alert Dialog

Golf.1650Golf.1650 USMember
edited November 2015 in Xamarin.Forms

I'm trying to change the color of the text of the warning dialog button, but I have no success. I try to change the buttons are on the attached image

My style xml:

http://pastebin.com/aGuSN3by

Someone has already achieved?

12.JPG 23.2K

Posts

  • Golf.1650Golf.1650 USMember

    up

  • CodeLinguistCodeLinguist USMember ✭✭
    edited January 2016

    Hello,

    Create a styles.xml inside Resources/values-21 folder.
    Inside that file, write something like this:

    <style name="MyAppTheme" parent="android:Theme.Material.Light">
            <item name="android:alertDialogTheme">@style/AlertDialogCustom</item>
        </style>
    
        <style name="AlertDialogCustom" parent="@android:style/Theme.Material.Dialog">
            <item name="android:textColor">@color/accent_color</item>
            <item name="android:textColorPrimary">@color/text_color</item>
            <item name="android:textColorTertiary">@color/text_color</item>
            <item name="android:buttonBarButtonStyle">@style/MyApp.DialogButton</item>
            <item name="android:background">@color/background_color</item>
        </style>
    
        <style name="MyApp.DialogButton" parent="@android:style/TextAppearance.Material.Button">
            <item name="android:textColor">@color/accent_color_dark</item>
            <item name="android:background">@color/transparent</item>
        </style>
    

    Here's a good article to help you further:
    http://gmariotti.blogspot.com/2015/04/a-material-styled-alertdialog.html

  • Can you post your working example I am trying to do this in C# and it not working.
    I added the new style.xml but can't see how to register it, and when you run the app it the same old dialogs.

    If this was done with Java then there is not point posting it as Android Dev in Xamarin is C# sorry for getting iritated but C# is diffrent to Java and Xamarin dose not implement R. in C#.

    Or I not been able to get it to work.

  • AdamMeaneyAdamMeaney USMember ✭✭✭✭

    He is saying you cannot do it in C# unless you create your own dependency service or something to create dialogs.

    You have to edit the Xamarin.Android resources and set the style, then use it in your MainActivity. This will change that on Android.

    The base idea is shown here, and you can look at the Google dev docs for more if you need.

  • 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);
    
  • AlcantaraRWAlcantaraRW USMember ✭✭

    Perfect, @NickKovalsky ... thanks!

  • VladBilykVladBilyk RUMember

    Starting from Forms 2.5 referencing <item name="alertDialogTheme">@style/AppCompatAlertDialogStyle</item> stops work for me, but <item name="android:alertDialogTheme">@style/AppCompatAlertDialogStyle</item> works fine...

  • JohnHardmanJohnHardman GBUniversity mod

    And if you want the title text to be a different color to the body text, add an extra style for the title, as described at https://stackoverflow.com/questions/29797134/how-to-use-and-style-new-alertdialog-from-appcompat-22-1-and-above

Sign In or Register to comment.