Created
May 8, 2018 13:09
-
-
Save rdelrosario/cc887458ae767293e5cb4b471c102cb1 to your computer and use it in GitHub Desktop.
CustomEditorRenderer Android
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.ComponentModel; | |
using Android.Content; | |
using Android.Content.Res; | |
using Android.Graphics.Drawables; | |
using ExtendedEditorSample.Controls; | |
using ExtendedEditorSample.Droid; | |
using Xamarin.Forms; | |
using Xamarin.Forms.Platform.Android; | |
[assembly: ExportRenderer(typeof(ExtendedEditorControl), typeof(CustomEditorRenderer))] | |
namespace ExtendedEditorSample.Droid | |
{ | |
public class CustomEditorRenderer : EditorRenderer | |
{ | |
bool initial = true; | |
Drawable originalBackground; | |
public CustomEditorRenderer(Context context) : base(context) | |
{ | |
} | |
protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.Editor> e) | |
{ | |
base.OnElementChanged(e); | |
if (Control != null) | |
{ | |
if(initial) | |
{ | |
originalBackground = Control.Background; | |
initial = false; | |
} | |
} | |
if (e.NewElement != null) | |
{ | |
var customControl = (ExtendedEditorControl)Element; | |
if (customControl.HasRoundedCorner) | |
{ | |
ApplyBorder(); | |
} | |
if (!string.IsNullOrEmpty(customControl.Placeholder)) | |
{ | |
Control.Hint = customControl.Placeholder; | |
Control.SetHintTextColor(customControl.PlaceholderColor.ToAndroid()); | |
} | |
} | |
} | |
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e) | |
{ | |
base.OnElementPropertyChanged(sender, e); | |
var customControl = (ExtendedEditorControl)Element; | |
if(ExtendedEditorControl.PlaceholderProperty.PropertyName == e.PropertyName) | |
{ | |
Control.Hint = customControl.Placeholder; | |
}else if (ExtendedEditorControl.PlaceholderColorProperty.PropertyName == e.PropertyName) | |
{ | |
Control.SetHintTextColor(customControl.PlaceholderColor.ToAndroid()); | |
}else if (ExtendedEditorControl.HasRoundedCornerProperty.PropertyName == e.PropertyName) | |
{ | |
if (customControl.HasRoundedCorner) | |
{ | |
ApplyBorder(); | |
}else | |
{ | |
this.Control.Background = originalBackground; | |
} | |
} | |
} | |
void ApplyBorder() | |
{ | |
GradientDrawable gd = new GradientDrawable(); | |
gd.SetCornerRadius(10); | |
gd.SetStroke(2, Color.Black.ToAndroid()); | |
this.Control.Background = gd; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment