Search code examples
c#androidxmlxamarin.android

Xamarin.android remove background of dialog


When I create a dialog it displays as it should but for some reason it creates a white background behind the card view

Popup code

<androidx.cardview.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    app:cardCornerRadius="20dp">
    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/DialogBarLayout"
        android:layout_gravity="center"
        android:background="@android:color/holo_red_dark"
        android:layout_height="match_parent"
        android:layout_width="match_parent">
        <ProgressBar
            android:id="@+id/progressBar"
            android:layout_width="125dp"
            android:layout_height="125dp"
            android:foregroundGravity="center"
            android:indeterminate="false"
            android:layout_centerInParent="true"
            style="?android:attr/progressBarStyleHorizontal"
            android:secondaryProgress="100"
            android:progressDrawable="@drawable/progress_circle"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintTop_toBottomOf="@+id/progressBar"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            android:gravity="center"
            android:text="@string/loading"
            android:id="@+id/loading_msg"
            android:layout_toEndOf="@id/progressBar"
        />
    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>

The way I create it

    public Dialog DialogProgressBar(Context content)
    {
      Dialog popupDialog = new Dialog(content);
      popupDialog.SetContentView(Resource.Layout.progress_dialog_bar);
      popupDialog.Window.SetSoftInputMode(SoftInput.AdjustResize);
      popupDialog.Window.SetTitle("Alert");
      popupDialog.SetCancelable(false);
      prgBar = (ProgressBar)popupDialog.FindViewById(Resource.Id.progressBar);
      return popupDialog;
    }

My question is: How can I remove the white board behind the card view and make it transparent?


Solution

  • The default color of a dialog window is white so you will have to change the background resource for the Window. Something like so:

    popupDialog.Window.SetBackgroundDrawableResource(Resource.Color.transparent);
    

    In colors.xml

    <resources>
        ...
        <color name="transparent">#00000000</color>
    </resources>