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

    With credits to everyone above, would simplify for the future.
    If you are into AlertDialog colors i guess you already customized menu, tabbar etc so you know what the styles.xml files are about. So in your Android project, in the Resources folder, in the values-v21 subfolder you have your sub-theme for newer devices only. In that the styles.xml file add dialog related code:

      <!--Your sub-theme-->
      <style name="MyTheme" parent="MyTheme.Base">
         ...
      <item name="alertDialogTheme">@style/AppCompatAlertDialogStyle</item>  
      </style>
    
      <!--AlertDialog Colors-->
      <style name="AppCompatAlertDialogStyle" parent="Theme.AppCompat.Dialog.Alert">
        <!--buttons-->
        <item name="colorAccent">#c7ac56</item>
        <!--title-->
        <item name="android:textColor">#c7ac56</item>
        <!--text-->
        <item name="android:textColorPrimary">#fafafa</item>
        <!--selection list-->
        <item name="android:textColorTertiary">#fafafa</item>
        <!--background-->
        <item name="android:background">#5c8487</item>
      </style>
    

    It is obvious instead of putting numeric color values you can reference your colors from the values/colors.xml file like for example @color/primary.

  • AlcantaraRWAlcantaraRW USMember

    Perfect, @NickKovalsky ... thanks!

Sign In or Register to comment.