Bug 59931 - FormsAppCompatActivity.GetField("_statusBarHeight") returns null
Summary: FormsAppCompatActivity.GetField("_statusBarHeight") returns null
Status: RESOLVED ANSWERED
Alias: None
Product: Forms
Classification: Xamarin
Component: Android (show other bugs)
Version: 2.4.0
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2017-10-03 17:18 UTC by MK
Modified: 2017-10-06 18:59 UTC (History)
3 users (show)

See Also:
Tags:
Is this bug a regression?: ---
Last known good build:


Attachments
Screenshot of status bar gap (24.04 KB, image/jpeg)
2017-10-06 17:07 UTC, MK
Details

Description MK 2017-10-03 17:18:53 UTC
Using GetField("_statusBarHeight") on FormsAppCompatActivity in my Android MainActivity OnCreate function returns null. In Xamarin.Forms 2.3.4.270 this still worked correctly. Below the whole relevant code block:



//Activate "resize" soft input mode for pushing controls when a keyboard appears, code from here:
//https://xamarinhelp.com/accommodate-on-screen-keyboard-xamarin-forms/
                
Xamarin.Forms.Application.Current.On<Xamarin.Forms.PlatformConfiguration.Android().UseWindowSoftInputModeAdjust(WindowSoftInputModeAdjust.Resize);
if (Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop) {
     Window.DecorView.SystemUiVisibility = 0;
     var statusBarHeightInfo = typeof(FormsAppCompatActivity).GetField("_statusBarHeight",
                   System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
     statusBarHeightInfo.SetValue(this, 0);
     Window.SetStatusBarColor(new Android.Graphics.Color(ContextCompat.GetColor(Forms.Context, 
                   Resource.Color.primaryDark)));
}
Comment 1 MK 2017-10-03 17:20:46 UTC
To clarify: this bug happened for me after updating to version 2.4.0.282
Comment 2 Paul DiPietro [MSFT] 2017-10-05 16:22:18 UTC
See the below PR for why using reflection no longer works or is needed:

https://github.com/xamarin/Xamarin.Forms/pull/892
Comment 3 MK 2017-10-06 17:07:56 UTC
Created attachment 25178 [details]
Screenshot of status bar gap
Comment 4 MK 2017-10-06 18:04:37 UTC
Hi Paul,
thanks for the reply.
From the pull request you linked I gather that the code I posted is not necessary anymore, as the gap at the top of the screen shouldn't appear anymore at all. So I guess I should be able to simply use WindowSoftInputMode = SoftInput.AdjustResize in the flags of the main activity? 
Just did a quick test but both on Samsung Galaxy phone and Android 5.1 emulator, the gap is still there and the status bar is  (see attachment).
What am I missing, is there other code needed for AdjustResize mode?

Sorry we are very close to release so I don't have time to dig further at the moment, so for now we will return to Xamarin.Forms 2.3.4.270.

You may also want to pass on to the documentation team that https://xamarinhelp.com/accommodate-on-screen-keyboard-xamarin-forms/ needs to be updated.
Comment 5 MK 2017-10-06 18:59:47 UTC
Hi,
sorry I take it back, in the end I did try a bit more and the issue was that I still had this line in my style.xml in values-v21:
<item name="android:statusBarColor">@android:color/transparent</item>

This is clearly not needed anymore as you no longer have the status bar underlay. Now status bar shows correctly even with input mode resize.

Note You need to log in before you can comment on or make changes to this bug.