I want to keep immersive mode all over the app on Android using Xamarin.forms. I have read few blogs and posts. This one works but only until I press an input text box or I scroll over the screen. I want to avoid this. This question debate my concern but I don't understand the solution. Maybe it is obsolete. My android device is 7.1 and this is my code on my .Android
MainActivity
, void OnCreate
, void OnCreate
:
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity{
protected override void OnCreate(Bundle savedInstanceState){
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(savedInstanceState);
LoadApplication(new App());
ImmersiveMode();
}
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults){
Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}
public void ImmersiveMode() {
int uiOptions = (int)(Forms.Context as Activity).Window.DecorView.SystemUiVisibility;
uiOptions |= (int)SystemUiFlags.Fullscreen;
uiOptions |= (int)SystemUiFlags.HideNavigation;
uiOptions |= (int)SystemUiFlags.ImmersiveSticky;
(Forms.Context as Activity).Window.DecorView.SystemUiVisibility = (StatusBarVisibility)uiOptions;
}
}
How could I resolve this? Could you be specific on where I should located the new code. I'm noob on Xamarin.Forms and it looks tricky for me. Thank you
Edit: this code:
this.Window.AddFlags(WindowManagerFlags.Fullscreen);
on the .Android
MainActivity
hides the Android bar with the wifi signal, time, bluetooth, etc.
Is there a line like that one to hide this android buttons?
Window.DecorView.SystemUiVisibility
is deprecated as ofAndroid R (11)
. There is a new way to do this in this API version.Below is the code I used in one of my apps. I had to override a bunch of methods in MainActivity class.
Previously, I also implemented the
View.IOnSystemUiVisibilityChangeListener
interface. But it is also deprecated and will be removed in future versions:EDIT:
If you want to keep immersive mode after user focus some input control on your screen you can implement
ViewTreeObserver.IOnGlobalFocusChangeListener
interface. Here's how I did: