Nirav Shah
Nirav Shah

Reputation: 2084

XamarinForm : Android : Statusbar overlap contentpage

In Xamarin Form based application for Android, I have an issue that Statusbar is overlapping ContentPage.

Setting hard code Thickness value for ContentPage in XAML may not work for each Android device.

So, I have found few solutions on blogs but not working anyone . Everywhere written that Calculate Statusbar height from Xamarin.Droid project and set padding to the content page.

So anyone please suggest about how to set Padding to top on content page on Runtime by calculating Statusbar height?

Thanks in advance.

Upvotes: 0

Views: 489

Answers (1)

Nirav Shah
Nirav Shah

Reputation: 2084

I have found the solution :

For this we need to create Custom renderer.

Please find below mentioned code for PageRenderer from Xamarin.Droid project :

[assembly: ExportRenderer(typeof(ContentPage), typeof(MyContentPageRenderer))]

namespace ProjectName.Droid.Renderer

{

public class MyContentPageRenderer : PageRenderer
{
    public MyContentPageRenderer()
    {
    }


    protected override void OnElementChanged(ElementChangedEventArgs<Page> e)
    {
        base.OnElementChanged(e);


    }

    protected override void OnElementPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
    {
        base.OnElementPropertyChanged(sender, e);



        float pixWidth = (float)Resources.DisplayMetrics.WidthPixels;
        float fdpWidth = (float)App.Current.MainPage.Width;
        float pixPerDp = pixWidth / fdpWidth;

        this.Element.Padding = new Thickness(0, MainActivity.StatusBarHeight/pixPerDp, 0, 0);

    } } }

And Please find code of "MainActivity" as mentioned below for StatusBarHeight calculation :

public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
    public static int StatusBarHeight;

    protected override void OnCreate(Bundle bundle)
    {

        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());

        StatusBarHeight = getStatusBarHeight();

    }

    public int getStatusBarHeight()
    {

        int statusBarHeight = 0, totalHeight = 0, contentHeight = 0;
        int resourceId = Resources.GetIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0)
        {
            statusBarHeight = Resources.GetDimensionPixelSize(resourceId);


        }
        return statusBarHeight;
    }
}

This has solved my problem.

Upvotes: 1

Related Questions