Search code examples
androidkotlinandroid-activitybackground

Transparent Background on Activity


I've been pulling my hair out with this specific issue. Whenever I load the current activity (on top) of the previous activity, it causes the background of the loading activity (with transparent background) to turn black.

I can see the transparent setting when the transition is loading, but when its fully loaded it turns black.

Transparent background present in transition

Black background when fully loaded.

Here is the code I use to make the background transparent:

window.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))

The above code works elsewhere on another view just like this one. All settings are the same on both views, but this one doesn't want to work correctly.

This is the code for the activity:

<?xml version="1.0" encoding="utf-8"?>
<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="@color/transparent"
    android:fitsSystemWindows="true"
    android:orientation="vertical"
    android:scaleType="fitCenter"
    tools:context=".ui.activities.TourConnectActivity">

    <ImageView
        android:id="@+id/backgroundImage"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginStart="24dp"
        android:layout_marginTop="64dp"
        android:layout_marginEnd="24dp"
        android:background="@drawable/rounded_top"
        android:contentDescription="background"
        android:scaleType="centerCrop"
        android:src="@drawable/launch_bg"
        app:layout_constraintBottom_toTopOf="@+id/view7"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:ignore="HardcodedText" />

    <View
        android:id="@+id/view7"
        android:layout_width="0dp"
        android:layout_height="40dp"
        android:layout_marginStart="24dp"
        android:layout_marginEnd="24dp"
        android:background="@drawable/preview_green_border_top"
        app:layout_constraintBottom_toTopOf="@+id/linearLayout2"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/backgroundImage" />

    <ImageView
        android:id="@+id/logo"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_marginStart="128dp"
        android:layout_marginEnd="128dp"
        android:background="@drawable/org_logo_border"
        android:contentDescription="background"
        android:cropToPadding="true"
        android:scaleType="fitCenter"
        app:layout_constraintBottom_toBottomOf="@+id/view7"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:srcCompat="@drawable/dot"
        tools:ignore="HardcodedText" />

    <LinearLayout
        android:id="@+id/linearLayout2"
        android:layout_width="match_parent"
        android:layout_height="315dp"
        android:layout_marginStart="24dp"
        android:layout_marginEnd="24dp"
        android:layout_marginBottom="64dp"
        android:background="@drawable/rounded_bottom"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/view7">

        <TextView
            android:id="@+id/connectTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="This tour is presented by"
            android:textColor="#333"
            android:textSize="12sp"
            tools:ignore="HardcodedText" />

        <TextView
            android:id="@+id/connectSubtitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:fontFamily="@font/roboto"
            android:text="Jackson Hole Land Trust"
            android:textAlignment="center"
            android:textColor="@color/colorTSGGreen"
            android:textSize="20sp"
            android:textStyle="bold"
            tools:ignore="HardcodedText" />

        <ScrollView
            android:id="@+id/scrollView"
            android:layout_width="match_parent"
            android:layout_height="175dp">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="16dp"
                android:background="@color/transparent">

                <TextView
                    android:id="@+id/connectMessage"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:fontFamily="@font/roboto"
                    android:text="@string/lorem_ipsum_3"
                    android:textColor="#333"
                    android:textSize="16sp" />
            </LinearLayout>
        </ScrollView>

        <Button
            android:id="@+id/learnMore"
            style="@style/Widget.MaterialComponents.Button.Icon"
            android:layout_width="280dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:drawableStart="@drawable/ic_none"
            android:drawableEnd="@drawable/ic_exit_to_app"
            android:onClick="buttonClick"
            android:paddingLeft="16dp"
            android:paddingRight="16dp"
            android:text="@string/learn_more"
            android:textColor="@color/loginTextColor"
            android:textSize="18sp"
            app:backgroundTint="@color/colorTSGGreen" />

    </LinearLayout>

    <ImageView
        android:id="@+id/buttonBack"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="32dp"
        android:contentDescription="close button"
        android:onClick="iconClick"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/ic_close" />
</androidx.constraintlayout.widget.ConstraintLayout>

I'm inclined to think that it has something to do with the map view on the previous activity, but I'm not sure that could be it.

Any input would be appreciated. Thanks.


Solution

  • A better approach than setting a transparent background programmatically is to change its style. Especially when the background remains the same in other cases. Check out this question and its top rated answer.