Page is not resizing after pressing Back when keyboard is shown.

AlexLapointeAlexLapointe USMember ✭✭

My problem is with Android using Xamarin.Forms.

I'm using FormsAppCompatActivity.
I also tied the AndroidBug5497WorkaroundForXamarinAndroid.assistActivity(this);

Page 1 [TabbedPage -> ContentPage] -> Navigation.PushAsync() and navigate to Page 2-> Open the Keyboard -> Press the Back arrow in the navigation bar to return to Page 1 -> Page 1 is not resizing, the keyboard is not there, but it's still taking the space.

On the Page 2, on Disappearing, i'm doing NoteEditor.Unfocus();.
On Page 1, I tried InvalidateMeasure(), but no success.

Anyone have a solution for this?

Thanks!

Answers

  • ShantimohanElchuriShantimohanElchuri USMember ✭✭✭✭

    If you are using Navigation.PushAsync() to go from Page 1 to 2 then you should use Navigation.PopAsync() for proper navigation. If you leave the navigation to OS, in Android or Windows, the first press of Back button dismisses the keyboard on the Page 2 and only the 2nd press, when you are using the PopAsync() method, will navigate back to Page 1.

  • AlexLapointeAlexLapointe USMember ✭✭

    I have the problem when I press the Back arrow in the navigation bar/title bar to return to Page 1", I'm not pressing the "hardware/software" back button.

  • ShantimohanElchuriShantimohanElchuri USMember ✭✭✭✭
    edited June 2016

    Doesn't matter, whatever way it is the methods are almost the same except that you don't have to PopAsync(). Actually when you press that 'Back Arrow...' I find that it does both dismissing the keyboard and navigating back. This action also fires the OnDisappearing(). Do you have any code there that is interfering with the normal operation?

    Can you show the screenshots of Page 1 before and after Page 2?

  • AlexLapointeAlexLapointe USMember ✭✭

    The problem is related with FormsAppCompatActivity, the pages are not resized when they keyboard appears.

    I found this
    http://stackoverflow.com/questions/36419653/adjust-resize-does-not-work-in-api-22-with-appcompat-on-xamarin-forms
    http://stackoverflow.com/questions/34013548/xamarin-forms-2-0-appcompat-android-keyboard-mode

    But the solution using Window.SetSoftInputMode(SoftInput.AdjustResize); and AndroidBug5497WorkaroundForXamarinAndroid make the bug happen. The page are resizing, but when you press back with the keyboard open, the workaround is not working.

  • AndyMartin.6838AndyMartin.6838 USMember ✭✭
    edited July 2016

    I am having the same issue @AlexLapointe

    Did you post anything to Bugzilla about this? For now I'm going to have to force the users to rely on the hardware back button since it does not have the problem. The device I'm testing is a Droid Max on KitKat.

            public async Task Push(Page page)
            {
    #if __ANDROID__
                //Fixes bug where the keyboard messes up the page
                //Related to AdjustResize.
                // 1. Open Keyboard 
                // 2. Press the Navigation page back button
                // 3. Observe that the page is not properly sized afterwards
                NavigationPage.SetHasBackButton(page, false);
    #endif
    
                await _navigation.PushAsync(page);
            }
    
  • AlexLapointeAlexLapointe USMember ✭✭

    I did not post on bugzilla, I think I'll just force the resize using the resolution of the app the first time it open, or something near this. But it's gonna be a while before I'll do this, tell me if you find something.

  • j.kj.k BYMember

    I'm experiencing the same problem now. Do you have any success resolving it?

  • AlexLapointeAlexLapointe USMember ✭✭

    in XamarinFormsActivity

    `protected override void OnCreate(Bundle savedInstanceState)
    {
    Window.SetSoftInputMode(SoftInput.AdjustResize);
    if (Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop)
    {
    // Bug in Android 5+, this is an adequate workaround
    AndroidBug5497WorkaroundForXamarinAndroid.assistActivity(this);
    }
    }

    public class AndroidBug5497WorkaroundForXamarinAndroid
    {
      // For more information, see https://code.google.com/p/android/issues/detail?id=5497
      // To use this class, simply invoke assistActivity() on an Activity that already has its content view set.
    
      // CREDIT TO Joseph Johnson (http://stackoverflow.com/users/341631/joseph-johnson) for publishing the original Android solution on stackoverflow.com
    
      public static void assistActivity(Activity activity)
      {
        new AndroidBug5497WorkaroundForXamarinAndroid(activity);
      }
    
      private View mChildOfContent;
      private int usableHeightPrevious;
      private FrameLayout.LayoutParams frameLayoutParams;
    
      private AndroidBug5497WorkaroundForXamarinAndroid(Activity activity)
      {
        FrameLayout content = (FrameLayout)activity.FindViewById(Android.Resource.Id.Content);
        mChildOfContent = content.GetChildAt(0);
        ViewTreeObserver vto = mChildOfContent.ViewTreeObserver;
        vto.GlobalLayout += (object sender, EventArgs e) =>
        {
          possiblyResizeChildOfContent();
        };
        frameLayoutParams = (FrameLayout.LayoutParams)mChildOfContent.LayoutParameters;
      }
    
      private void possiblyResizeChildOfContent()
      {
        int usableHeightNow = computeUsableHeight();
        if (usableHeightNow != usableHeightPrevious)
        {
          int usableHeightSansKeyboard = mChildOfContent.RootView.Height;
          int heightDifference = usableHeightSansKeyboard - usableHeightNow;
    
          frameLayoutParams.Height = usableHeightSansKeyboard - heightDifference;
    
          mChildOfContent.RequestLayout();
          usableHeightPrevious = usableHeightNow;
        }
      }
    
      private int computeUsableHeight()
      {
        Rect r = new Rect();
        mChildOfContent.GetWindowVisibleDisplayFrame(r);
        if (Build.VERSION.SdkInt < BuildVersionCodes.Lollipop)
        {
          return (r.Bottom - r.Top);
        }
        return r.Bottom;
      }
    }
    
    protected async override void OnAppearing() // In the page after the back
    {
      base.OnAppearing();
    
      //It's working... but I don't like it.
      await Task.Delay(250);
    
      //Not sure if this one is needed
      InvalidateMeasure();
    }`
    

    If you find something better tell me plz.

  • j.kj.k BYMember

    this seems to fix problem when navigating back. but there is another problem when you just open and hide keyboard on the same page. the page doesn't seem to resize itself correctly after hiding. it's especially noticable for pages with several fields only. they are totally misplaced

  • AlexLapointeAlexLapointe USMember ✭✭

    I had only one page with a textbox.

    Maybe something like that, try a StackLayout :

    <ContentPage> <StackLayout Spacing="0" Padding="0"> <Grid Padding="10" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand"> <Grid Padding="1" BackgroundColor="Black" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand"> <Editor BackgroundColor="White" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" /> </Grid> </Grid> <Grid HeightRequest="1" BackgroundColor="Black" VerticalOptions="End" HorizontalOptions="FillAndExpand" /> <Button Text="Test" HeightRequest="60" VerticalOptions="End" HorizontalOptions="FillAndExpand" /> </StackLayout> </ContentPage>

  • a_ba_b USMember
    edited September 2016

    The following worked for me:

      private void possiblyResizeChildOfContent()
      {
        int usableHeightNow = computeUsableHeight();
        if (usableHeightNow != usableHeightPrevious)
        {
          int usableHeightSansKeyboard = mChildOfContent.RootView.Height;
          int heightDifference = usableHeightSansKeyboard - usableHeightNow;
    
          frameLayoutParams.Height = usableHeightSansKeyboard - heightDifference;
    
          mChildOfContent.Invalidate(); // using Invalidate instead of RequestLayout
          usableHeightPrevious = usableHeightNow;
        }
      }
    

    No idea what the ramifications of this change would or could be.

    edit: actually this just cause the same problem on normal keyboard dismissals

  • AlexLapointeAlexLapointe USMember ✭✭

    So you had no luck finding a solution?

    I think I'll take the time to do a simple project where the problem happen and send it has a bug or post it on stackoverflow.

  • AbhimanyuSinghalAbhimanyuSinghal INMember ✭✭

    Was anyone able to find a proper workaround for this issue? The above code is not working and is introducing issues elsewhere.

  • AlexLapointeAlexLapointe USMember ✭✭

    No, it's very annoying, if someone could provide an exemple for Android 4.0.3 and above. It would be nice.

    The demo app from Xamarin Evolve 2016 was nice to see how to manage the style for different versions, but there was no textbox, in the app...

  • EricHedstromEricHedstrom USMember

    It works for me including the previous screen after pressing the back button if I simplify possiblyResizeChildOfContent:

        private void possiblyResizeChildOfContent()
        {
            int usableHeightNow = computeUsableHeight();
            if (usableHeightNow != usableHeightPrevious)
            {
                int usableHeightSansKeyboard = mChildOfContent.RootView.Height;
                frameLayoutParams.Height = usableHeightSansKeyboard;
    
                mChildOfContent.RequestLayout();
                usableHeightPrevious = usableHeightNow;
            }
        }
    

    With the extra if logic in that method, it worked in Android 5 but not 6.

  • kp8080kp8080 INMember
    edited June 7

    I have the same issue if I press back button that time some space are be displayed at page bottom.

    I was used adjust resize in oncreate() in mainactivity.

    1.jpg 65.7K
    2.jpg 626.4K
    3.jpg 43.9K
  • JimmyGarridoJimmyGarrido USXamarin Team Xamurai

    Hey all, this is a known issue which we are working on. Thanks!

  • pragiyapragiya LKMember

    Hi Jimmy are there any update me also suffering with the same issue.

  • LeonardoFernandezLeonardoFernandez USMember ✭✭
    Hi community this bug is already solved in any Xamarin Forms update ? I need some advice for overcome this issue. Thanks.
  • CostasAletrariCostasAletrari USMember ✭✭

    Hi all , has anyone found the fix for this? It just seems like there needs to be form of delay to allow the keyboard to dismiss.
    As a working around im either going try and dismiss the keyboard first before returning back. Or make a webapi call to a method that would sleep for 1 second. not ideal. but it might work.

  • CostasAletrariCostasAletrari USMember ✭✭

    Making a extremely simple api call that thread.sleep(1000) (sleeps for 1 second) works finally. Its just a delay to allow the keyboard to drop. probably could decrease to less than a second though. Until the bug is fixed. Hope this help someone move on.

Sign In or Register to comment.