简体   繁体   中英

How do I add back the transparent background around my dialog activity with a custom theme

I have an activity and I've set it's theme in the manifest to a custom dialog theme.
But now the transparency around the dialog box is gone. Everything is black.

Here's what it looks like with my custom theme:

I want it to look like this:

But with a custom background and text color, like in the first picture.

Here's the part of the manifest with the activity's theme set to my custom theme, as in the first picture:

    <activity
        android:name=".activity.LockScreenActivity"
        android:exported="true"
        android:label="@string/aplicacao_bloqueada_title"
        android:theme="@style/dialog_theme"/>

Here's the part of the manifest with the activity's theme set to android's dialog theme, as in the second picture:

    <activity
        android:name=".activity.LockScreenActivity"
        android:exported="true"
        android:label="@string/aplicacao_bloqueada_title"
        android:theme="@style/Theme.AppCompat.DayNight.Dialog"/>

Here's my xml with my custom theme:

<style name="dialog_theme" parent="Theme.AppCompat.DayNight.Dialog">

        <item name="android:textColor">@color/white</item>
        <item name="android:windowBackground">@color/dark_gray</item>
        <item name="android:backgroundDimEnabled">true</item>
        <item name="android:backgroundDimAmount">50</item>

</style>

Here's my activity class:

public class LockScreenActivity extends AppCompatActivity {

    // Notification activity that is called after too many failed login attempts

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lock_screen);
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                finishAndRemoveTask();
                System.exit(0);
            }
        }, 5000);
    }
}

Layout for my activity:

<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="wrap_content"
    tools:context=".activity.LockScreenActivity">

    <TextView
        android:id="@+id/textView2"
        android:layout_width="214dp"
        android:layout_height="136dp"
        android:gravity="center_vertical"
        android:padding="20dp"
        android:text="@string/aplicacao_bloqueada_text"
        android:textAlignment="viewStart"
        android:textSize="16sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.497"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

What's the best way to do this?

Edit: As nima sugested, I tried adding the following line to the onCreat method of my dialog themed class:

getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

But it turned everything transparent, including the pop up window. See picture:

在此处输入图片说明

How do I solve this?

Edit Your Code Like Bellow:
1-In style

    <style name="dialog_theme" parent="Theme.AppCompat.DayNight.Dialog">

    <item name="android:windowBackground">@android:color/transparent</item>

</style>

2-In Manifest

    <activity
        android:name=".activity.LockScreenActivity"
        android:exported="true"
        android:theme="@style/dialog_theme"
        android:label=""
        />

3- activity_lock_screen

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@color/cardview_dark_background"
android:gravity="center"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="170dp">
<TextView
    android:id="@+id/textView2"
    android:layout_width="214dp"
    android:layout_height="0dp"
    android:layout_weight=".2"
    android:gravity="center"
    android:padding="20dp"
    android:text="Application Blocked !"
    android:textColor="@color/white"
    android:textSize="16sp"
    />
<TextView
    android:id="@+id/textView3"
    android:layout_width="214dp"
    android:layout_height="0dp"
    android:textColor="@color/white"
    android:layout_weight=".4"
    android:gravity="center_horizontal"
    android:padding="20dp"
    android:text="@string/aplicacao_bloqueada_text"
    android:textSize="16sp" />
</LinearLayout>

Your backgroundDimAmount is too high try some smaller number like 0.5

So your dialog_theme would look something like this.

<style name="dialog_theme" parent="Theme.AppCompat.DayNight.Dialog">

        <item name="android:textColor">@color/white</item>
        <item name="android:windowBackground">@color/dark_gray</item>
        <item name="android:backgroundDimEnabled">true</item>
        <item name="android:backgroundDimAmount">0.5</item>

    </style>

As @nima pointed out you can use getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); to make the background transparent.

This will introduce a couple of issues:

  • Transparency of everything

    Fix this by setting a white background to the ConstraintLayout root layout.

  • Transparent activity title

    Fix this by removing the title from the manifest and set it to an empty one in activity with setTitle("") ; and add another TextView to represent the title in the layout:

Remove the title (android:label) from the activity in the manifest: Also you should android:exported="true" as it seems that this activity should be exported to other apps:

<activity
    android:name=".activity.LockScreenActivity"
    android:theme="@style/Theme.AppCompat.DayNight.Dialog"/>

Set the title & background:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_lock_screen);
   
    // ...........

    getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    setTitle("");

}

Layout:

<?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="wrap_content"
    android:background="@color/white"
    android:paddingHorizontal="16dp"
    android:paddingTop="16dp"
    tools:context=".LockScreenActivity">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Application Blocked"
        android:textSize="22sp"
        android:textStyle="bold"
        app:layout_constraintStart_toStartOf="@id/textView2"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="214dp"
        android:layout_height="136dp"
        android:gravity="center_vertical"
        android:padding="20dp"
        android:text="@string/aplicacao_bloqueada_text"
        android:textAlignment="viewStart"
        android:textSize="16sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.497"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView1" />
</androidx.constraintlayout.widget.ConstraintLayout>

UPDATE:

When I use my custom theme, as opposed to Theme.AppCompat.DayNight.Dialog, I still get the black screen

This is because the android:backgroundDimAmount is set to 50 in your custom theme; the valid range for this attribute is 0 through 1; any value greater than that will be considered 1...

0 means there is no dimming (completely transparent, and 1 meaning 100% opaque/black.

So to fix this issue, I guess you mean by 50 as the half way, so you'd set it to 0.5 instead:

<style name="dialog_theme" parent="Theme.AppCompat.DayNight.Dialog">
        <item name="android:textColor">@color/white</item>
        <item name="android:windowBackground">@color/dark_gray</item>
        <item name="android:backgroundDimEnabled">true</item>
        <item name="android:backgroundDimAmount">0.5</item>
</style>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM