AndroidAccessibleButton.cs 1.94 KB
Newer Older
Poli97's avatar
Poli97 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
using System;
using Android.Content;
using Android.Service.Autofill;
using Android.Views.Accessibility;
using CustomViewAccessibility;
using CustomViewAccessibility.Droid;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;

[assembly: ExportRenderer(typeof(IAccessibleButton), typeof(AndroidAccessibleButton))]
namespace CustomViewAccessibility.Droid
{
    public class AndroidAccessibleButton : ButtonRenderer
    {
        Context context;

        public AndroidAccessibleButton(Context context) : base(context)
        {
            this.context = context;
        }

        protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.Button> e)
        {
            base.OnElementChanged(e);

            if (Control == null)
            {
                return;
            }

            Console.WriteLine("PIPPO created from Android");
            Control.SetBackgroundColor(Android.Graphics.Color.Red);
        }

		protected override Android.Widget.Button CreateNativeControl()
		{
			return new AccessibleButton(context);
		}

		
    }

	public class AccessibleButton : Android.Widget.Button
	{
		public AccessibleButton(Context context) : base(context)
		{
            Console.WriteLine("***** AccessibleButton created *****");
        }

		public override void OnInitializeAccessibilityEvent(AccessibilityEvent e)
		{
			base.OnInitializeAccessibilityEvent(e);
            if (e.EventType == EventTypes.ViewAccessibilityFocused)
            {
                this.SetBackgroundColor(Android.Graphics.Color.Green);
                this.Text = "I AM IN FOCUS";
                Console.WriteLine("ACCESSIBILITY I am in focus");
            }
			else if (e.EventType == EventTypes.ViewAccessibilityFocusCleared)
			{
                this.SetBackgroundColor(Android.Graphics.Color.Red);
                this.Text = "I AM NO MORE IN FOCUS";
                Console.WriteLine("ACCESSIBILITY I am in NOT in focus");
            }
        }
	}
}