locked
Forms 2.0 AppCompat android keyboard mode RRS feed

  • Question

  • User136779 posted

    Hello. Xamarin I updated to version 4, Forms and 2.0 versions. On Android, I use AppCompat.

    I had a problem. Previously, the Android keyboard caused resize view. Now this does not happen. The keyboard appears on the top view. And the desired Elements to be hiding.

    I've tried:

    ``` [Activity(WindowSoftInputMode = SoftInput.AdjustResize, Label = "Title", Icon = "@drawable/icon", Theme = "@style/MyTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)] public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity { protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle);

            global::Xamarin.Forms.Forms.Init(this, bundle);
    
    
            ToolbarResource = Resource.Layout.toolbar;
            TabLayoutResource = Resource.Layout.tabs;
    
            LoadApplication(new App());
            Window.DecorView.SetFitsSystemWindows(true);
        }
    }
    

    ```

    Daylight AppCompat has been made on this lesson: https://blog.xamarin.com/material-design-for-your-xamarin-forms-android-apps/

    Thank you.

    Tuesday, December 1, 2015 5:32 AM

All replies

  • User136779 posted

    Tuesday, December 1, 2015 12:44 PM
  • User136779 posted

    It used to be (Holo)

    Tuesday, December 1, 2015 1:41 PM
  • User136779 posted

    ...

    Tuesday, December 1, 2015 1:42 PM
  • User136779 posted

    I have solved the problem. Decompile class FormsAppCompatActivity and watched how the method works OnCreate.

    As a result, I turned out the following code: ``` [Activity(Label = "Title", Icon = "@drawable/icon", Theme = "@style/MyTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)] public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity { protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle);

            global::Xamarin.Forms.Forms.Init(this, bundle);
    
            Window.SetSoftInputMode(SoftInput.AdjustResize);
            AndroidBug5497WorkaroundForXamarinAndroid.assistActivity(this);
    
            ToolbarResource = Resource.Layout.toolbar;
            TabLayoutResource = Resource.Layout.tabs;
    
            LoadApplication(new App());
    
        }
    
        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 Android.Views.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;
                    if (heightDifference > (usableHeightSansKeyboard / 4))
                    {
                        // keyboard probably just became visible
                        frameLayoutParams.Height = usableHeightSansKeyboard - heightDifference;
                    }
                    else
                    {
                        // keyboard probably just became hidden
                        frameLayoutParams.Height = usableHeightSansKeyboard;
                    }
                    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;
            }
    
        }
    }
    

    ```

    It is important to add Window.SetSoftInputMode(SoftInput.AdjustResize); After calling base.OnCreate(bundle);

    Wednesday, December 2, 2015 5:36 AM