diff options
author | Samantha Houts <samantha@teamredwall.com> | 2017-01-31 11:49:15 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-01-31 11:49:15 -0800 |
commit | ae59382c9046501edb37882ad1c065aacce60319 (patch) | |
tree | 3578ce8e0396a38aeb8323d4051f50a19a5340fb /Xamarin.Forms.Platform.Android/ViewRenderer.cs | |
parent | 23d228039acc504049f6a5153f5839d4c714930a (diff) | |
download | xamarin-forms-ae59382c9046501edb37882ad1c065aacce60319.tar.gz xamarin-forms-ae59382c9046501edb37882ad1c065aacce60319.tar.bz2 xamarin-forms-ae59382c9046501edb37882ad1c065aacce60319.zip |
[All] Basic Accessibility Support (#713)
* [Core] Add accessibility properties
* [Controls] Add accessibility gallery
* [iOS] Implement accessibility properties
* [Android] Implement accessibilty properties
* [Win] Implement accessibility properties
* [Win] Select ListView item on selected for a11y
* Update docs
* TODO: macOS accessibility
* [iOS] Fix failing UI Tests
Diffstat (limited to 'Xamarin.Forms.Platform.Android/ViewRenderer.cs')
-rw-r--r-- | Xamarin.Forms.Platform.Android/ViewRenderer.cs | 99 |
1 files changed, 98 insertions, 1 deletions
diff --git a/Xamarin.Forms.Platform.Android/ViewRenderer.cs b/Xamarin.Forms.Platform.Android/ViewRenderer.cs index c4bd3fa0..31a9c282 100644 --- a/Xamarin.Forms.Platform.Android/ViewRenderer.cs +++ b/Xamarin.Forms.Platform.Android/ViewRenderer.cs @@ -19,6 +19,9 @@ namespace Xamarin.Forms.Platform.Android } ViewGroup _container; + string _defaultContentDescription; + bool? _defaultFocusable; + string _defaultHint; bool _disposed; EventHandler<VisualElement.FocusRequestArgs> _focusChangeHandler; @@ -66,7 +69,7 @@ namespace Xamarin.Forms.Platform.Android { if (Control == null) return (base.GetDesiredSize(widthConstraint, heightConstraint)); - + AView view = _container == this ? (AView)Control : _container; view.Measure(widthConstraint, heightConstraint); @@ -124,6 +127,8 @@ namespace Xamarin.Forms.Platform.Android if (e.PropertyName == VisualElement.IsEnabledProperty.PropertyName) UpdateIsEnabled(); + else if (e.PropertyName == Accessibility.LabeledByProperty.PropertyName) + SetLabeledBy(); } protected override void OnLayout(bool changed, int l, int t, int r, int b) @@ -155,6 +160,79 @@ namespace Xamarin.Forms.Platform.Android } } + protected override void SetContentDescription() + { + if (Control == null) + { + base.SetContentDescription(); + return; + } + + if (Element == null) + return; + + if (SetHint()) + return; + + if (_defaultContentDescription == null) + _defaultContentDescription = Control.ContentDescription; + + var elemValue = string.Join(" ", (string)Element.GetValue(Accessibility.NameProperty), (string)Element.GetValue(Accessibility.HintProperty)); + + if (!string.IsNullOrWhiteSpace(elemValue)) + Control.ContentDescription = elemValue; + else + Control.ContentDescription = _defaultContentDescription; + } + + protected override void SetFocusable() + { + if (Control == null) + { + base.SetFocusable(); + return; + } + + if (Element == null) + return; + + if (!_defaultFocusable.HasValue) + _defaultFocusable = Control.Focusable; + + Control.Focusable = (bool)((bool?)Element.GetValue(Accessibility.IsInAccessibleTreeProperty) ?? _defaultFocusable); + } + + protected override bool SetHint() + { + if (Control == null) + { + return base.SetHint(); + } + + if (Element == null) + return false; + + var textView = Control as global::Android.Widget.TextView; + if (textView == null) + return false; + + // Let the specified Title/Placeholder take precedence, but don't set the ContentDescription (won't work anyway) + if (((Element as Picker)?.Title ?? (Element as Entry)?.Placeholder ?? (Element as EntryCell)?.Placeholder) != null) + return true; + + if (_defaultHint == null) + _defaultHint = textView.Hint; + + var elemValue = string.Join(". ", (string)Element.GetValue(Accessibility.NameProperty), (string)Element.GetValue(Accessibility.HintProperty)); + + if (!string.IsNullOrWhiteSpace(elemValue)) + textView.Hint = elemValue; + else + textView.Hint = _defaultHint; + + return true; + } + protected void SetNativeControl(TNativeView control) { SetNativeControl(control, this); @@ -219,6 +297,25 @@ namespace Xamarin.Forms.Platform.Android Control.OnFocusChangeListener = this; UpdateIsEnabled(); + SetLabeledBy(); + } + + void SetLabeledBy() + { + if (Element == null || Control == null) + return; + + var elemValue = (VisualElement)Element.GetValue(Accessibility.LabeledByProperty); + + if (elemValue != null) + { + var id = Control.Id; + if (id == -1) + id = Control.Id = FormsAppCompatActivity.GetUniqueId(); + + var renderer = elemValue?.GetRenderer(); + renderer?.SetLabelFor(id); + } } void UpdateIsEnabled() |