0

I try to change the text color of the entry from a Xamarin Forms Prompt. I tried giving a default color for the Entry control in the App resource dictionary:

<Style TargetType="Entry">
    <Setter Property="TextColor" Value="White"/>
</Style>

I also tried providing a style in the styles.xml file of the Android project:

<style name="MainTheme" parent="MainTheme.Base">
      <item name="android:colorActivatedHighlight">@android:color/transparent</item>
      <item name="colorControlActivated">#FFFFFF</item>
      <item name="colorControlNormal">#FFFFFF</item>"
      <item name="colorAccent">#FFFFFF</item >
      <item name="alertDialogTheme">@style/AppCompatAlertDialogStyle</item>  
  </style>

<style name="AppCompatAlertDialogStyle" parent="Theme.AppCompat.Dialog.Alert">
    <!--buttons-->
    <item name="colorAccent">#ffffff</item>
    <!--title-->
    <item name="android:textColor">#ffffff</item>
    <!--text-->
    <item name="android:textColorPrimary">#ffffff</item>
    <!--selection list-->
    <item name="android:textColorTertiary">#ffffff</item>
    <!--background-->
    <item name="android:background">#000000</item>
  </style>

And yet no success. It just remains black, and I would like to change the default black to other color that would match my theme (green for instance). I don't care if the solution is cross-platform or not, as my project only targets Android for the moment, that's why I tried the styles.xml approach. I guess there is a key for the dictionary of the style, but I just can't find it. Any idea how could I achieve this?

Gabriel Stancu
  • 940
  • 2
  • 13
  • 26

2 Answers2

1

I would go with custom Prompt, which is really simple to create. Here is an example:

<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="TestPrompt.MainPage">
    <AbsoluteLayout Padding="0" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
        <StackLayout HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
            <Button Text="Display Prompt!"
                VerticalOptions="CenterAndExpand"
                HorizontalOptions="Center"
                Clicked="OnButtonClicked" />
        </StackLayout>


        <ContentView x:Name="popupView" BackgroundColor="#C0808080" Padding="10, 0" IsVisible="false" AbsoluteLayout.LayoutBounds="0, 0, 1, 1" AbsoluteLayout.LayoutFlags="All">
            <StackLayout VerticalOptions="Center" HorizontalOptions="Center">
                <StackLayout Orientation="Vertical" HeightRequest="150" WidthRequest="200" BackgroundColor="White">
                    <ContentView Padding="12, 0, 0, 0" HorizontalOptions="StartAndExpand">
                        <Label FontSize="16" FontAttributes="Bold" TextColor="Green" Text="What is your name?" />
                    </ContentView>
                    <Entry TextColor="Green" FontSize="16" VerticalOptions="CenterAndExpand"/>
                    <StackLayout Orientation="Horizontal" VerticalOptions="End">
                        <Button Text="OK" TextColor="Green" FontSize="16"  BackgroundColor="White" TextTransform="None" Clicked="OK_Clicked" />
                        <Button Text="Cancel" TextColor="Green" FontSize="16"  BackgroundColor="White" TextTransform="None" Clicked="Cancel_Clicked" />
                    </StackLayout>
                </StackLayout>
            </StackLayout>
        </ContentView>
    </AbsoluteLayout>
</ContentPage>

In C# you just need to do something like this:

        void OnButtonClicked(object sender, EventArgs args)
        {
            //This will show the pop up
            popupView.IsVisible = true;
        }

        private void OK_Clicked(object sender, EventArgs e)
        {
            popupView.IsVisible = false;
            //Your stuff here
        }

        private void Cancel_Clicked(object sender, EventArgs e)
        {
            popupView.IsVisible = false;
            //Your stuff here
        }

As you can see, you are able to control here everything. This is how this example looks like:

enter image description here

adamm
  • 849
  • 1
  • 6
  • 17
  • @Gabriel Is this solution OK for your problem? Please let me know by [accepting/upvoting](https://stackoverflow.com/help/someone-answers) if it does. Otherwise, please leave the feedback. – adamm May 19 '21 at 16:04
  • Not really, I ended up sliding in a textbox on the user's screen. Will upvote the solution as this was the starting point of my "solution", but I will not mark it as answer as it does not actually answer the original question. – Gabriel Stancu Jun 07 '21 at 13:15
0

I think you have to give the TargetType a Name.

for example x:Key="AEntry"

In App.xaml

 <Style x:Key="AEntry" TargetType="Entry">
        <Setter Property="TextColor" Value="Green"/>
    </Style>

In Mainpage.xaml call Style

  <Entry Style="{StaticResource AEntry}" />
Bas H
  • 2,114
  • 10
  • 14
  • 23
  • I know, that's for Entries that I put in my XAMLs. But this is an Entry that is included in the Xamarin Prompt (extension from Display Alert that has an Entry control in it), which can't be accessed by me through XAML, so I tried defining a custom value for ALL Entries and eventually overriding it in my XAML files where I actually have access to the controls. – Gabriel Stancu May 17 '21 at 20:21
  • Something like this , found it here .You can ad the Textcolor i think. https://stackoverflow.com/questions/60669404/how-to-change-background-color-of-default-display-alert-in-xamarin-android – Bas H May 17 '21 at 20:39