Search code examples
androidxamarin.formsxamarin.androidstatusbarandroid-7.0-nougat

Status bar overlay image shifted down in android nought


I am trying to add an image to the status bar in xamarin forms android.. But the image is completely shifted down and coming under the normal status bar color i set in android nougat.

This happened when i updated the xamarin forms version to 2.4.0.74863 in 2.3.4.270 its working fine. Is this the xamrin forms issue or my code issue.

The code i used to draw overlay on status bar is

activity.Window.AddFlags(WindowManagerFlags.DrawsSystemBarBackgrounds);
                activity.Window.ClearFlags(WindowManagerFlags.TranslucentStatus);
                activity.Window.SetStatusBarColor(Android.Graphics.Color.Transparent);

ViewGroup contentView = (ViewGroup)activity.FindViewById(Android.Resource.Id.Content);
            //if (contentView.ChildCount > 1)
            //{
            //  contentView.RemoveViewAt(1);
            //}

            // get status bar height
            int res = activity.Resources.GetIdentifier("status_bar_height", "dimen", "android");
            int height = 0;
            if (res != 0)
                height = activity.Resources.GetDimensionPixelSize(res);


            // create new imageview and set resource id
            ImageView image = new ImageView(activity);
            LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MatchParent, height);
            params1.Width = LinearLayout.LayoutParams.MatchParent;
            image.LayoutParameters = params1;
            image.SetImageResource(imageRes);

            image.SetScaleType(ImageView.ScaleType.FitXy);

            // add image view to content view
            contentView.AddView(image);
            contentView.SetFitsSystemWindows(true);

I call this as a dependedncy from the xaml page.

EDIT

After i added the flags that @york mentioned the bottom navigation bar has become translucent and the app view went down.

Bottom navigation bar


Solution

  • Just need add the following code in your Activity style :

    <style name="MyTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
        ...
        <item name="android:windowTranslucentStatus">false</item>
        <item name="android:windowTranslucentNavigation">true</item>
        <item name="android:statusBarColor">@android:color/transparent</item>
    </style>
    

    Effect :

    enter image description here