Share via

Re-Panning App Content When Calling RequestFocus

Nathan Sokalski 4,111 Reputation points
2021-04-01T19:34:16.807+00:00

I have a need to programmatically unfocus & focus EditText(s) in my app, for which I use the ClearFocus() & RequestFocus() methods of EditText. However, RequestFocus() does not open the keyboard or pan the app content like it would if the user had manually selected the EditText. I can display the keyboard using the InputMethodManager, but that still does not pan the app content, therefore sometimes hiding the EditText that is focused & being edited (it does pan it once the user starts typing, but I need it to pan when the EditText is focused). Is there any way to force the keyboard to re-pan the app content? Thanks.

Developer technologies | .NET | Xamarin
0 comments No comments

1 answer

Sort by: Most helpful
  1. Anonymous
    2021-04-02T02:31:08.613+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    You can try to use the following code to hide or show keyboard for your EditText(s) in your app programmatically, it could re-pan the app content.

       var btn_show = FindViewById<Button>(Resource.Id.btn_show);  
                  var btn_hide = FindViewById<Button>(Resource.Id.btn_hide);  
                  EditText editText1 = FindViewById<EditText>(Resource.Id.editText1);  
         
                   InputMethodManager imm = (InputMethodManager)GetSystemService(Context.InputMethodService);  
                    
                   btn_show.Click += delegate  
                   {  
         
                       if (imm != null)  
                       {  
                           editText1.RequestFocus();  
                           imm.ShowSoftInput(editText1, 0);  
                       }  
         
                   };  
         
                   btn_hide.Click += delegate  
                   {  
                       if (imm != null)  
                       {  
                           imm.HideSoftInputFromWindow(editText1.WindowToken, 0);  
                           editText1.ClearFocus();  
                       }  
         
                   };  
               }  
    

    Here is running screenshot. If it is not your need, could you share a video or GIF about your issue?

    Best Regards,

    Leon Lu


    If the response is helpful, please click "Accept Answer" and upvote it.

    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.

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.