Search code examples
androidandroid-layoutandroid-alertdialogandroid-progressbar

How can I remove my layout background so I can see the activity behind it?


I am implementing a custom loading screen with a ProgressBar and TextView to show the user that I am loading the data on the activity. Currently, the layout comprising of ProgressBar and TextView shows a greyish background, and I want to remove it. I just want to show the loading text and progress bar without any background. How can I achieve that?

Layout file

<androidx.constraintlayout.widget.ConstraintLayout
    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="match_parent"
    android:layout_height="match_parent"
    
    >

    <ProgressBar
        android:id="@+id/progressBarloading"
        style="?android:attr/progressBarStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        />

    <TextView
        android:id="@+id/textView7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Loading..."
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/progressBarloading"
        app:layout_constraintVertical_bias="0.074"
        android:textSize="18sp"
        />
</androidx.constraintlayout.widget.ConstraintLayout>

Currently it looks like this, how can I remove the greyish background?

enter image description here


Solution

  • Hi if you want to make transparent Dialog you must change the background to transparent in your layout and also in your dialog you need to use dialog.getWindow().getDecorView().setBackgroundColor(Color.TRANSPARENT);

    and this is my code

    Layout.xml :

    <androidx.constraintlayout.widget.ConstraintLayout 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="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/transparent"
        >
    
        <ProgressBar
            android:id="@+id/progressBarloading"
            style="?android:attr/progressBarStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
        <TextView
            android:id="@+id/textView7"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Loading..."
            android:textSize="18sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.498"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/progressBarloading"
            app:layout_constraintVertical_bias="0.074" />
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    and this the code in the dialog :

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
            View view = LayoutInflater.from(this).inflate(R.layout.dialog, null, false);
            builder.setView(view);
            AlertDialog dialog = builder.create();
            dialog.show();
            dialog.getWindow().getDecorView().setBackgroundColor(Color.TRANSPARENT);