3

So I am working on Android application. Right now I am creating MainPage where I insert Entry which has the bottom line as always. The bottom line on my previewer is White while on my phone it appears to be Black.

So to fix the issue I decided to play with renderers and see if I can fix it. I created Class in App called CustomEntryRenderer which inherits from Entry. Then I created Class in App.Android called CustomEntryRednererAndroid which is supposed to change the color of bottom entry line. But it doesn`t affect it. I tried doing the same with some custom renderers I found on the internet.

For example deleting bottom line didn`t affect the program as well: removing line

Entry from MainPage.xaml:

<Entry 

        Grid.Row="4"
        Grid.ColumnSpan="2"
        TextColor="Silver"         
        Placeholder="Write Your nickname"
        PlaceholderColor="Silver"

        />

CustomEntryRenderer:

 public class CustomEntryRenderer : Entry
{
}

CustomEntryRendererAndroid:

[assembly: ExportRenderer(typeof(CustomEntryRenderer), typeof(MyEntryRenderer))]
namespace App3.Droid
{
public class MyEntryRenderer : EntryRenderer
{

    public MyEntryRenderer(Context context) : base(context) { }
    protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
    {
        base.OnElementChanged(e);

        if (Control == null || e.NewElement == null) return;

        if (Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop)
            Control.BackgroundTintList = ColorStateList.ValueOf(Android.Graphics.Color.White);
        else
            Control.Background.SetColorFilter(Android.Graphics.Color.White, PorterDuff.Mode.SrcAtop);
    }
}

}

Top answer for Android And for some reason also in CustomEntryRendererAndroid.cs I had to use Android.Graphic instead of Xamarin.Forms.Color. But I dont think that is the issue.

I have been trying for a couple of hours now and can`t find the way out of this situation. Would appreciate any ideas.

D.Weltrowski
  • 321
  • 1
  • 4
  • 15

2 Answers2

6

In xaml you are using the default Entry control and not your CustomEntryRenderer which is what your renderer is affecting. Also, you might want to rename it because it is not actually your renderer, but your custom control.

To resolve your issue you can either change your renderer typeof(CustomEntryRenderer) to typeof(Entry) to affect all Android entries in your app by default. For example, this worked for my test app for all Entries:

[assembly: ExportRenderer(typeof(Entry), typeof(MyEntryRenderer))]
namespace YourNameSpace
{
    public class MyEntryRenderer : EntryRenderer
    {
        public MyEntryRenderer(Context context) : base(context) { }

        protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
        {
            base.OnElementChanged(e);

            if (Control == null || e.NewElement == null) return;

            if (Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop)
                Control.BackgroundTintList = ColorStateList.ValueOf(Android.Graphics.Color.White);
            else
                Control.Background.SetColorFilter(Android.Graphics.Color.White, PorterDuff.Mode.SrcAtop);
        }
    }
}

The other option is to switch your xaml code in MainPage to actually use your custom control. For example, <local:CustomEntryRenderer/>

Nick Peppers
  • 3,161
  • 23
  • 27
  • Thanks for answer! I tried changing `typeof(CustomEntryRenderer)` to `typeof(Entry)` but after doing that and starting my app with Live Player. The xamarin Live just crashes. Every time I try and I tried it with different renderers. Also do You know why previewer shows different line color then app started on phone? – D.Weltrowski Feb 18 '18 at 19:19
  • I updated my answer to be more specific. If you just change it to be Entry you can delete your `CustomEntryRender` class. Afterwards, I'd make sure to delete the bin and obj folder and rebuild your project. As for the Xamarin Live Previewer last time I tried using it it was still super buggy and is still in development so I'd just deploy directly to the device if possible. – Nick Peppers Feb 18 '18 at 19:39
  • Thanks a lot! This fixed the issue and I had to deploy directly on my phone as well. – D.Weltrowski Feb 18 '18 at 19:56
4

Adding this in the Styles.xml repairs it globally

<item name="colorControlNormal">#1BB8A3</item>"
El0din
  • 3,208
  • 3
  • 20
  • 31