Search code examples
c#androidxamarinandroid-linearlayoutandroid-windowmanager

Android View (Dialog / Layout) across multiple Activities


I've created a view that I want to use as an error message.

If the phone has no internet connection (mobile data or wifi), I want to show an error message within my app so user knows that everything from that point on, will be locally saved (not on the server).

The problem here is that I want this message to stay up even when user goes from one activity to another.

I tried using a Dialog and LinearLayout on WindowManager. Didn't work.

This is what I did.

    public void noConnectionLayout()
    {
        LinearLayout mainLayout = new LinearLayout (this.ApplicationContext);
        mainLayout.Clickable = false;
        mainLayout.Focusable = false;
        mainLayout.FocusableInTouchMode = false;
        mainLayout.LongClickable = false;
        mainLayout.SetBackgroundColor (Android.Graphics.Color.Red);
        mainLayout.Orientation = Orientation.Vertical;

        var spaceFromTopPara = (int) (65 * this.Resources.DisplayMetrics.Density);
        var layoutHeight = (int)(25 * this.Resources.DisplayMetrics.Density);

        WindowManagerLayoutParams windowLayoutParams = new WindowManagerLayoutParams (
            ViewGroup.LayoutParams.MatchParent,
            layoutHeight,
            WindowManagerTypes.SystemError, 
            WindowManagerFlags.NotTouchModal | WindowManagerFlags.NotFocusable, 
            Format.Translucent);
        windowLayoutParams.ScreenOrientation = Android.Content.PM.ScreenOrientation.Portrait;
        windowLayoutParams.Gravity = GravityFlags.Top;
        windowLayoutParams.Y = spaceFromTopPara;

        TextView textNoConnectionMesg = new TextView (this);
        textNoConnectionMesg.Text = "No Connection available.";
        textNoConnectionMesg.SetTypeface (this.latoFont, TypefaceStyle.Normal);
        textNoConnectionMesg.LayoutParameters = new ViewGroup.LayoutParams (ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.MatchParent);
        textNoConnectionMesg.Gravity = GravityFlags.Center;
        textNoConnectionMesg.SetTextSize (Android.Util.ComplexUnitType.Dip, 14f);
        textNoConnectionMesg.SetBackgroundColor (Android.Graphics.Color.Transparent);
        textNoConnectionMesg.SetTextColor (Android.Graphics.Color.White);

        mainLayout.AddView (textNoConnectionMesg);

        WindowManager.AddView (mainLayout, windowLayoutParams);

    }

When the activity changes, the view just goes away.

What can I do here?

Thank you for your time.


Solution

  • Dialogs are inherently associated with Activities. It seems odd that your users would even be able to navigate between Activities with a dialog showing. If you want to allow navigation and have the connection status dialog be displayed on each screen, you'll need to show the Dialog in e.g. the onStart of a BaseActivity that all your other Activities extend.