Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Tuesday, January 24, 2017 11:24 AM
I work with XamarinForms and has some issues with Entry
in ScrollView
on Android platform. Autoscroll doesn't work correctly when I focuse on an entry placed in ScrollView
. Entry
appears above the keyboard but the whole page becomes unscrollable (I can't scroll completely to bottom or top of page). I try to set WindowSoftInputMode
to adjustPan
or adjustResize
or both, and I try different ways to set it (in code, by attribute, directly in AndroidManifest.xml
). But any case doesn't work (in each cases behavior remain the same). Please, maybe someone have encountered the same problem and I hope can help me. Thanks a lot.
All replies (10)
Tuesday, January 24, 2017 11:40 AM
@YaroslavTsapiv,
ScrollView
has no affect on Android for scrolling when the keyboard is present. Instead, you will want to set the following in your MainActivity.OnCreate
.
Window.SetSoftInputMode(Android.Views.SoftInput.AdjustResize);
There might be additional problems because of an Android bug. I found a solution to that on Adam Pedleys site.
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;
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;
}
}
Use it after the SetSoftInputMode line:
AndroidBug5497WorkaroundForXamarinAndroid.assistActivity(this);
The site from the link above also has an example renderer to disable the full screen behavior in landscape.
Tuesday, January 24, 2017 4:13 PM
Hi @JohnMiller , thank you for answer. I have tried this solution before. And it doesn't work correctly with ScrollView
in my case. I have to use ScrollView
, because I have a lot of dynamic data in the page. Although this solution works well in Samsung device, but not LG. However when I override method RequestChildFocus
for ScrollViewRenderer
and do not call base.RequestChildFocus
the bug goes away, but appears issue with numeric Entry
(it stops working). So this solution doesn't solve my issue. I know that this problem can be solved in native Android by WindowSoftInputMode
. Maybe you know some another solution which will work with scrollView.
Wednesday, January 25, 2017 3:38 PM
@YaroslavTsapiv,
Sorry that didn't help! If you have a sample or project you can share that could demostrate the issue you are having on a specific device, please add a bug report on Bugzilla and attach it. Feel free to ping me with the bug number and I will take a look at the project. Please follow the bug writing guidelines when reporting an issue.
Monday, March 20, 2017 8:56 PM
@JohnMiller
UWP has the same problem !
Do you have any solutions for UWP ?
Monday, March 20, 2017 10:04 PM
@AlexanderSheety,
Yep! There is a solution in the link I posted in my comment.
Tuesday, March 21, 2017 3:15 PM
@JohnMiller
My investigations at Android: This solution doesn't work if entry is inside scrollview! View doesn't resize correctly! I tested StackLayout,AbsoluteLayout as topmost page layout. I hadn't results! But i finded some interesting in Grid. Entry inside scrollview works if Grid is topmost page layout! I don't know why only grid, but it is worked!
Work Sample:
! >! using Xamarin.Forms.PlatformConfiguration; ! >! using Xamarin.Forms.PlatformConfiguration.AndroidSpecific; ! >! ! >! public class App : Xamarin.Forms.Application ! >! { ! >! public App() ! >! { ! >! this.On().UseWindowSoftInputModeAdjust(WindowSoftInputModeAdjust.Resize); ! >! var stack = new Grid ! >! { ! >! HorizontalOptions = LayoutOptions.FillAndExpand, ! >! VerticalOptions = LayoutOptions.FillAndExpand, ! >! RowDefinitions = ! >! { ! >! new RowDefinition { Height = 62 }, ! >! new RowDefinition { Height = new GridLength(1, GridUnitType.Star) }, ! >! },
! >! }; ! >! var boxmenu=new BoxView{ ! >! BackgroundColor = Color.Black; ! >! HorizontalOptions = LayoutOptions.FillAndExpand; ! >! VerticalOptions = LayoutOptions.FillAndExpand; ! >! }; ! >! var topmenubar=new AbsoluteLayout{ ! >! HorizontalOptions = LayoutOptions.FillAndExpand; ! >! VerticalOptions = LayoutOptions.FillAndExpand; ! >! Children={ ! >! {boxmenu, new Rectangle(0, 0, 1, 1), AbsoluteLayoutFlags.All}, ! >! } ! >! }; ! >! stack.Children.Add(topmenubar,0,0); ! >!
! >! var scrollstack=new StackLayout{ ! >! HorizontalOptions = LayoutOptions.FillAndExpand; ! >! VerticalOptions = LayoutOptions.Start; ! >! BackgroundColor = Color.White; ! >! Spacing = 0; ! >! Padding = new Thickness(10,0,10,10); ! >! }; ! >! for (int i=0;i<10;i++) ! >! { ! >! var label = new Label { ! >! FontSize=15, ! >! Text ="Label"+i, ! >! TextColor = Color.Black, ! >! HorizontalOptions = LayoutOptions.Start, ! >! VerticalOptions = LayoutOptions.Start, ! >! Margin= new Thickness(0,10,0,0); ! >! }; ! >! var entry = new Entry { ! >! HorizontalOptions = LayoutOptions.Start, ! >! VerticalOptions = LayoutOptions.Start, ! >! BackgroundrColor=Color.FromRgb(240,240,240), ! >! TextColor=Color.Black, ! >! FontSize= 15, ! >! };! >! scrollstack.Children.Add(label); ! >! scrollstack.Children.Add(entry); ! >! }; ! >! var scroll=new ScrollView{ ! >! Content=scrollstack, ! >! }; ! >! stack.Children.Add(scroll,0,1); ! >! MainPage = stack; ! >! } ! >! protected override void OnStart() ! >! { ! >! // Handle when your app starts ! >! } ! >!
! >! protected override void OnSleep() ! >! { ! >! // Handle when your app sleeps ! >! } ! >!
! >! protected override void OnResume() ! >! { ! >! // Handle when your app resumes ! >! } ! >! }
My investigations at UWP: This solution doesn't work if entry is inside scrollview! View doesn't resize correctly!
Questions (Android and UWP ):
- How can i stop resize or pan view after showing keyboard ?
- How can i get keyboard height for calculations scroll height? ( I want to send scroll event to my scrollview )
Wednesday, June 21, 2017 2:46 PM
Sorry I just replied to wrong thread. Here is the solution I was looking for: https://developer.xamarin.com/guides/xamarin-forms/platform-features/platform-specifics/consuming/android/#Setting*the*Soft*Keyboard*Input_Mode
Thursday, July 13, 2017 7:32 AM
@JohnMiller: Workaround works fine, thanks! I chose the alternative method on Adams site, it is shorter and does not require creating another class.
Monday, February 11, 2019 4:49 PM
I've applied the workaround mentioned by @JohnMiller, which caused a new Problem:
If I focus an input field at the bottom of the scroll view there is an empty space between the bottom of the ScrollView and the keyboard. Besides that, the Appbar and the very first element inside the ScrollView disappears completely. It looks like the whole View of the App is moved outside the display bounds.
Has anybody an idea what went wrong?
Thanks
Cedric
Thursday, August 22, 2019 12:57 PM
@JohnMiller
The following workaround as per the link provided by you doesn't work for Android 8.0 (Oreo) on LG G7+ ThinQ phone.
Xamarin.Forms.Application.Current.On<Xamarin.Forms.PlatformConfiguration.Android>().UseWindowSoftInputModeAdjust(WindowSoftInputModeAdjust.Resize);
Any idea on what could be the reason and how can I fix this problem?
Thanks.