So when you create a Custom Renderer for your Xamarin Forms ContentPage, on Android, sometimes you have to tap into the Android Layout changes on your current page.
For instances like, Orientation changes, Keyboard visibility changes on Android and so on.
This is done by Adding a Layout Listener to our Layout’s ViewTree. 🙂 Confusing? Let’s get to work!
Create a Layout Listener…
First let’s create our Layout Listener, the guy who get’s the call back when the Layout Changes on the ViewTree of our Layout.
We derive to from the IOnGlobalLayoutListener interface, where as you need to implement its OnGlobalLayout() method which get’s the call back when a Layout change occurs.
Add the Listener to the ViewTree…
Next add it to the ViewTreeObserver of your Layout.
Since I implemented this on a Xamarin Forms Custom Renderer, first I have gained access to the current Activity’s RootView (from a 3rd party library CurrentActivityPlugin) and then I have added my listener to the ViewTreeObserver of my root view. 😀
but you could also…
Oh well good things comes to those who wait.. lol Xamarin Android provides you an event called GlobalLayout where you could also directly subscribe to Layout changes without having to implement your own listener.
Oh well it depends on your implementation requirement though.
Anyways there you have it! 😀