Chris
Chris

Reputation: 1642

Changing default TextColor in Xamarin.Forms Listview on Android

I am trying to change the default TextColor in a Xamarin.Forms ListView on Andoid.

The ListView is quite simple:

List<string> cities = new List<string> { "Berlin", "Bonn", "Braunschweig", "Bremen" };
ListView listView = new ListView();
listView.ItemsSource = cities;
Content = listView;

On the device it looks like this:

enter image description here

What I wnat to have, is that the TextColor will be black. As far as I understand Xamarin Forms CustomRenderer there will be a Android.Resource.Layout.SimpleListItem1 generated for each item.

SimpleListItem1 uses the following textAppearance:

android:textAppearance="?android:attr/textAppearanceListItemSmall"

textAppearanceListItemSmall uses the Attribute textAppearanceMedium for rendering, as you can see here.

So I have added the colors and the theme to the resouces:

<?xml version="1.0" encoding="UTF-8" ?>
<resources>
  <color name="Black">#000000</color>
</resources>


<?xml version="1.0" encoding="UTF-8"?>
<resources>
  <style name="MyTheme" parent="android:Theme.Holo.Light">
    <item name="android:textAppearanceMedium">@style/MyDefaultTextAppearanceM</item>
  </style>

  <style name="MyDefaultTextAppearanceM" parent="@android:style/TextAppearance.Medium">
    <item name="android:textColor">@color/Black</item>
  </style>
</resources>

I have also added the Theme to the Attribute for my Activity (Theme = "@style/MyTheme) The Theme is basically working. With <item name="android:colorActivatedHighlight">@color/Blue4</item> I can change the HighlightColor.

So how can I get the text color in ListView black? What I am doing wrong?

Upvotes: 2

Views: 10339

Answers (3)

Gerard C
Gerard C

Reputation: 107

[assembly: ExportRenderer(typeof(MyTextCell), typeof(MyTextCellRenderer))]
namespace MyNamespace.Droid.Renderers
{
    public class MyTextCellRenderer : TextCellRenderer
    {
        protected override Android.Views.View GetCellCore(Cell item, Android.Views.View convertView, ViewGroup parent, Context context)
        {
            Android.Views.View result =  base.GetCellCore(item, convertView, parent, context);

            (result as BaseCellView).SetDefaultMainTextColor(Color.Black);

            return result;
        }
    }
}

Upvotes: 0

Late to the conversation, but maybe this will others down the road.

Xaml based solution, very simple - Just set the TextColor on the ImageCell. Like so:

    <ContentPage.Content>
        <StackLayout VerticalOptions="FillAndExpand">
            <ListView x:Name="listView" VerticalOptions="FillAndExpand"
                SeparatorVisibility="None">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ImageCell TextColor="Black" Text="{Binding Title}"
                            ImageSource="{Binding IconSource}"/>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

Upvotes: 0

Marlon Ribeiro
Marlon Ribeiro

Reputation: 126

Try to add this before you set the Content:

var template = new DataTemplate(typeof(TextCell));
template.SetValue(TextCell.TextColorProperty, Color.Black);
listView.ItemTemplate = template;

Upvotes: 8

Related Questions