Dependency Service: set accessibility focus to other control after a button click

Raj 1 Reputation point
2021-11-16T19:05:10.227+00:00

My issue is similar to below question :

https://social.msdn.microsoft.com/Forums/en-US/ef62f3b6-4599-4853-a274-a21238c02225/dependency-service-set-accessibility-focus-after-a-button-click?forum=xamarinforms

Unable to move the focus to other control when a button is clicked. Below is the code

class OnViewVisibilityService : IOnViewVisibilityService
    {
        public void getVisibileView(Xamarin.Forms.View view)
        {
            var vRenderer = Platform.CreateRendererWithContext(view, Android.App.Application.Context);
            var view1 = vRenderer.View;
            view1.SendAccessibilityEvent(Android.Views.Accessibility.EventTypes.ViewFocused);

        }
    }

----------- calling Dependency service in a button tap event -------------------

DependencyService.Get<IOnViewVisibilityService>().getVisibileView(PageHeaderLabel);
//PageHeaderLabel is Xamarin form Label

Could anyone look into it and let me what am missing?

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,297 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 68,741 Reputation points Microsoft Vendor
    2021-11-17T03:15:51.557+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    I find use Dependence Service cannot get the TextView, So I achive it in the Custom Renderer. we should create a custom Label. Create a method called GetAccessibilityForLabel,

       public class MyLabel:Label  
           {  
               public event EventHandler GetAccessibility;  
               public void GetAccessibilityForLabel() {  
         
                   if (GetAccessibility != null)  
                   {  
                       GetAccessibility(this, EventArgs.Empty);  
                   }  
         
               }  
         
           }  
    

    then we can call it in the button click event,

       private void Button_Clicked(object sender, EventArgs e)  
               {  
          
                   myLabel.GetAccessibilityForLabel();  
                 
               }  
    

    Then we can achieve this get Accessibility in the custom renderer with textView.SendAccessibilityEvent(EventTypes.ViewHoverEnter);.

       namespace App150.Droid  
       {  
           class MyLabelRenderer : LabelRenderer  
           {  
               public MyLabelRenderer(Context context) : base(context)  
               {  
               }  
               TextView textView;  
               MyLabel myLabel;  
               protected override void OnElementChanged(ElementChangedEventArgs<Label> e)  
               {  
                   base.OnElementChanged(e);  
                   if (e.NewElement != null)  
                   {  
                       textView = Control as TextView;  
                       myLabel = e.NewElement as MyLabel;  
                       myLabel.GetAccessibility += OnAccessibilityRequested;  
                   }  
                   if (e.OldElement != null)  
                   {  
                       myLabel.GetAccessibility -= OnAccessibilityRequested;  
                   }      
                }  
         
               private void OnAccessibilityRequested(object sender, EventArgs e)  
               {  
                       Device.BeginInvokeOnMainThread(() => {                  
                           textView.SendAccessibilityEvent(EventTypes.ViewHoverEnter);  
                       });  
                    
               }  
           }  
       }  
    

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.