I have several elements which I need to modify when the keyboard is shown or hidden. If I understand correctly, this can be done in Android 11 using
SetOnApplyWindowInsetsListener
. However, I am having trouble figuring out how to do this (and none of the examples are in Xamarin.Android / C#). I have looked at the following sites:
https://proandroiddev.com/exploring-windowinsets-on-android-11-a80cf8fe19be
https://proandroiddev.com/android-11-creating-an-ime-keyboard-visibility-listener-c390a40d1ad0
https://developer.android.com/reference/android/view/View.html#setOnApplyWindowInsetsListener(android.view.View.OnApplyWindowInsetsListener)
How exactly do I create the
IOnApplyWindowInsetsListener
? Are there any good tutorials or examples of this in C# for Xamarin.Android? Thanks.
You could try the following way to create a class for implementing
IOnApplyWindowInsetsListener
:
public class RootViewDeferringInsetsCallback : WindowInsetsAnimationCompat.Callback, IOnApplyWindowInsetsListener{
......
Then, there are some methods you must implement which would be mentioned by VS:
public override WindowInsetsCompat OnProgress(WindowInsetsCompat insets, IList<WindowInsetsAnimationCompat> runningAnimations){}
private View view;
private WindowInsetsCompat lastWindowInsets;
private bool deferredInsets = false;
public RootViewDeferringInsetsCallback(int dispatchMode) : base(dispatchMode)
public WindowInsetsCompat OnApplyWindowInsets(View v, WindowInsetsCompat insets)
view = v;
lastWindowInsets = insets;
return WindowInsetsCompat.Consumed;
Referring to the blog you mentioned, we could know that we could use the following code to get the state of keyboard:
bool visible = false;
visible = insets.IsVisible(WindowInsetsCompat.Type.Ime());
if (visible == true){ // do something}
In Activity file, we could call it via:
ViewCompat.SetOnApplyWindowInsetsListener(view, rootViewInsetsCallback);
ViewCompat.SetWindowInsetsAnimationCallback(view, rootViewInsetsCallback);
Do you think the above will be useful to you?