Xamarin Forms Android transparent status bar
I created a Xamarin Forms project and I cannot change the color of the android status bar to transparent. I change my colors programmatically in the OnCreate () method of my MainActivity as shown below:
if (Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop)
{
Window.ClearFlags(WindowManagerFlags.TranslucentStatus);
Window.AddFlags(WindowManagerFlags.DrawsSystemBarBackgrounds);
Window.SetStatusBarColor(Color.Transparent);
}
I've tried different colors and it works great, but "Transparent" doesn't work at all. I am testing API 22.
My .xml style looks like this:
<style name="MainTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="colorPrimary">@color/blue</item>
<item name="colorPrimaryDark">@color/blue</item>
<item name="colorAccent">#FF4081</item>
<item name="windowActionModeOverlay">true</item>
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
If I programmatically set my color to purple, for example, it works without issue, but if I set it to transparent, I get my style.xml color to be blue. If I remove my ColorPrimaryDark color style.xml, I get a gray status bar. What could be the solution?
+3
source to share
3 answers
Use the below code. This works great for me.
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
protected override void OnCreate(Bundle bundle)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(bundle);
global::Xamarin.Forms.Forms.Init(this, bundle);
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(18, 52, 86, 255));
}
LoadApplication(new App());
}
}
0
source to share
You can try adding this to styles.xml (Android proj)
<style name="MainTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:statusBarColor">@android:color/transparent</item>
<item name="android:windowTranslucentStatus">true</item>
</style>
Looks like this: -
0
source to share