Android WindowSoftInputMode.AdjustResize still pan

I have successfully implemented AdjustResize on several occasions, but one in particular still refuses to do anything other than pan when the keyboard is displayed.

Any ideas what I am doing wrong?

I am using xamarin attribute like this:

[Activity (
    LaunchMode=Android.Content.PM.LaunchMode.SingleTask, 
    Label = "Active Jobs", 
    MainLauncher = false, 
    Icon = "@drawable/icon", 
    WindowSoftInputMode = SoftInput.AdjustResize,
    ScreenOrientation = Android.Content.PM.ScreenOrientation.Portrait)]     

      

EDIT: basic axml layout for this operation

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:minWidth="25px"
        android:minHeight="25px"
        android:background="#FFFFFF"
        android:gravity="center|top"
        android:paddingTop="20dp"
        android:paddingLeft="20dp"
        android:paddingRight="20dp"
        android:paddingBottom="40dp">
        <TextView
            android:text="No active works on this device."
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:padding="0dp"
            android:gravity="center"
            android:layout_centerHorizontal="true"
            android:layout_alignParentTop="true"
            android:visibility="gone"
            android:textColor="#006985"
            android:textSize="30dp" />
        <ListView
            android:minWidth="25px"
            android:minHeight="25px"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:gravity="center"
            android:textColor="#006985"
            android:textSize="30dp"
            android:divider="@null"
            android:dividerHeight="-0.6dp"
            android:layout_gravity="top" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center|center"
        android:paddingLeft="40dp"
        android:paddingRight="40dp"
        android:paddingTop="40dp"
        android:paddingBottom="60dp"
        android:layout_alignParentBottom="true"
        android:visibility="gone"
        android:background="#006985"
        android:orientation="vertical">
        <EditText
            android:inputType="textNoSuggestions"
            android:layout_width="fill_parent"
            android:layout_height="80dp"
            android:background="#FFFFFF"
            android:textColor="#006985"
            android:textSize="30dp"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            android:hint="Find..." />
        <Button
            android:text="Add New Task"
            android:layout_width="fill_parent"
            android:layout_height="100dp"
            android:background="@drawable/buttonHollowWhite"
            android:layout_centerHorizontal="true"
            android:textColor="#FFFFFF"
            android:textSize="30dp"
            android:layout_marginTop="40dp"
            android:layout_gravity="center" />
    </LinearLayout>
</RelativeLayout>

      

+3


source to share


2 answers


Refer to this error in Android

I redesigned the workflow to work with Xamarin.Android based fooobar.com/questions/16389 / ... .

To implement the fix, simply add the following code to your method OnCreate

.



AndroidBug5497WorkaroundForXamarinAndroid.assistActivity (this);

      

And the complete class:

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 (https://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;
            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);
        return (r.Bottom - r.Top);
    }

}

      

+5


source


If someone like me is still annoying, buy Backbutton navigation issue, my job to fix the issue for now. but i'm still testing ... if nothing works try it.

The main change was the event, which in case WindowFucusChange runs smoothly with an underlay. The downside is that the event is triggered once when the editor is focused / unfocused. This is the reason for waiting for a task with a while loop for the screen size. Maybe extra!

vto.GlobalFocusChange + = Vto_WindowFocusChange;



private AndroidBug5497Workaround (activity activity) {

        var content = (FrameLayout)activity.FindViewById(Android.Resource.Id.Content);
        mChildOfContent = content.GetChildAt(0);
        var vto = mChildOfContent.ViewTreeObserver;
        vto.GlobalFocusChange += Vto_WindowFocusChange;
        frameLayoutParams = (FrameLayout.LayoutParams)mChildOfContent.LayoutParameters;
    }

    private async void Vto_WindowFocusChange(object sender, ViewTreeObserver.GlobalFocusChangeEventArgs e)
    {
        if (e.NewFocus != null && e.OldFocus != null)
        {
                var usableHeightNow = await computeUsableHeight(mChildOfContent.RootView.Height);
                int usableHeightSansKeyboard = mChildOfContent.RootView.Height;
                int heightDifference = usableHeightSansKeyboard - usableHeightNow;
                frameLayoutParams.Height = usableHeightSansKeyboard - heightDifference;
                mChildOfContent.RequestLayout();
                usableHeightPrevious = usableHeightNow;
        }
        else
        {
                frameLayoutParams.Height = mChildOfContent.RootView.Height;
                mChildOfContent.RequestLayout();
                usableHeightPrevious = mChildOfContent.RootView.Height;
        }
    }
    private async Task<int> computeUsableHeight(int previous)
    {
        Rect r = new Rect();
        var w = previous;
        await Task.Run(async() => {

            mChildOfContent.GetWindowVisibleDisplayFrame(r);

            while (r.Bottom == previous)
            {
                await Task.Delay(150);

                if (Build.VERSION.SdkInt < BuildVersionCodes.Lollipop)
                {
                    return (r.Bottom - r.Top);
                }

                mChildOfContent.GetWindowVisibleDisplayFrame(r);

                System.Diagnostics.Debug.WriteLine("Current screen size... " + r.Bottom);
            }
            return r.Bottom;
        });
        return r.Bottom;
    }

      

0


source







All Articles