question

njsokalski avatar image
0 Votes"
njsokalski asked RobCaplan edited

Re-Panning App Content When Calling RequestFocus

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.

dotnet-xamarin
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

LeonLu-MSFT avatar image
0 Votes"
LeonLu-MSFT answered LeonLu-MSFT commented

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.


· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I think you misunderstood my problem. My problem is not focusing/unfocusing the EditText, or showing/hiding the keyboard, it is panning the content of the app to insure that the focused EditText is visible. In the Activity attribute of my MainActivity, I have the following:

 WindowSoftInputMode = SoftInput.AdjustPan

I have multiple EditText(s) on my page. When the one is focused by the user selecting it, the keyboard pans the screen like I want so that the EditText remains visible while they type. However, if I programmatically focus the EditText & open the keyboard, the screen does not get panned, therefore possibly hiding the selected EditText.

0 Votes 0 ·

Please share your code that could reproduce your issue, just describe your problem, it is hard to image your issue, then reproduce this issue.

0 Votes 0 ·