Dialog with transparent background in Android
Asked Answered
S

25

297

How do I remove the black background from a dialog box in Android. The pic shows the problem.

enter image description here

final Dialog dialog = new Dialog(Screen1.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.themechanger); 
Settlings answered 29/5, 2012 at 7:50 Comment(3)
please show the code for the dialog creationRectory
this situation also works in this two lines of codes here: #16187318Thierry
Find the best answer here enter link description hereMarivaux
D
807

Add this code

 dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));

Or this one instead:

dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
Delphiadelphic answered 29/5, 2012 at 8:1 Comment(11)
Thank you! However, I prefer to use dialog.getWindow().setBackgroundDrawable(new ColorDrawableResource(R.color.transparent));Harquebus
this solution helps . issue is, the width will fit the screen. there will be no padding as compared to normal dialog. But Android 4.1 handles it by defaultChurchman
What about if i am using ALert Dialog ??Miscount
If you're inside a DialogFragment, just call getDialog().getWindow() ... (after the view was created, e.g. in your onViewCreated callback).Hyman
I prefer to use dialog.getWindow().setBackgroundDrawableResource(R.color.transparent);Billposter
how to do it for popup? I tried this popup.getContentView().setBackgroundResource(android.R.color.transparent); but didn't workGussy
This will change the padding of dialog. I believe the used background is 0 patch image. Is there any way not affecting the padding of dialog?Asis
It will work but if u click that transparent area dialogue will not close how to handle this?Sordid
setBackgroundDrawable is deprecated. It seems setBackground(Drawable drawable) is the new way of doing thisGreg
The hero we need, but not the one we deserveFeld
In Kotlin, this can be written as dialog.window!!.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))Phenocryst
A
107

TL;DR; You just need two things, firstly in your style do something like:

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

Secondly, make 100% sure said style gets applied to your dialog (maybe by passing to constructor).

Full example

<style name="NewDialog">
    <item name="android:windowFrame">@null</item>
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowTitleStyle">@null</item>
    <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
    <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
    <item name="android:backgroundDimEnabled">false</item>
    <item name="android:background">@android:color/transparent</item>
</style>

Use in Java:

Dialog dialog = new Dialog(this, R.style.NewDialog);

I hope helps you !

Alidia answered 19/9, 2013 at 4:38 Comment(4)
It will work but if u click that transparent area dialogue will not close how to handle this?Sordid
@Sordid You can use: dialog.setCanceledOnTouchOutside(true);Alidia
You can use parent="Theme.AppCompat.Dialog" to make exit on touch outside as default.Huei
even possible to handle the alpha for dim background: <item name="android:backgroundDimAmount">0.2</item> or programmatically window.getAttributes().dimAmount = 0.2f;Oestriol
E
36

I've faced the simpler problem and the solution i came up with was applying a transparent bachground THEME. Write these lines in your styles

    <item name="android:windowBackground">@drawable/blue_searchbuttonpopupbackground</item>
</style>
<style name="Theme.Transparent" parent="android:Theme">
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:backgroundDimEnabled">false</item>
</style>

And then add

android:theme="@style/Theme.Transparent"

in your main manifest file , inside the block of the dialog activity.

Plus in your dialog activity XML set

 android:background= "#00000000"
Examen answered 8/12, 2012 at 9:41 Comment(1)
Or use this if you just want to set the Dialog's style: <style name="Theme.Transparent" parent="@android:style/Theme.Dialog">Argal
C
20

if you want destroy dark background of dialog , use this

dialog.getWindow().setDimAmount(0);
Condonation answered 13/1, 2019 at 6:32 Comment(4)
Thank you. It is what I have been looking for it. It's great.Indihar
This does the job in the most simple way, thank you! However, if you want your app to run on 100% of the devices out there (I have set minimum SDK version to 15), Android suggests the following: if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { Objects.requireNonNull(alertDialog.getWindow()).setDimAmount(0); }Footstalk
I prefer using this. Very simple!Khiva
With just this, there will be no dimming but there will be shadow around your dialogLapse
N
17

Somehow Zacharias solution didn't work for me so I have used the below theme to resolve this issue...

<style name="DialogCustomTheme" parent="android:Theme.Holo.Dialog.NoActionBar">
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:colorBackgroundCacheHint">@null</item>
</style>

One can set this theme to dialog as below

final Dialog dialog = new Dialog(this, R.style.DialogCustomTheme); 

Enjoy!!

Nidanidaros answered 13/5, 2013 at 7:8 Comment(1)
<item name="android:windowBackground">@color/transparent</item> This line should replace by below line or else you have to add a value of transparent in colors.xml file. <item name="android:windowBackground">@android:color/transparent</item>Fuss
H
13

Dialog pop up fill default black background color or theme color so you need to set TRANSPARENT background into Dialog. Try below code:-

final Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
dialog.setContentView(R.layout.splash);
dialog.show();
Hovey answered 4/10, 2013 at 7:18 Comment(0)
P
12

You can use the:

setBackgroundDrawable(null);

method.And following is the doc:

  /**
    * Set the background to a given Drawable, or remove the background. If the
    * background has padding, this View's padding is set to the background's
    * padding. However, when a background is removed, this View's padding isn't
    * touched. If setting the padding is desired, please use
    * {@link #setPadding(int, int, int, int)}.
    *
    * @param d The Drawable to use as the background, or null to remove the
    *        background
    */
Plaza answered 29/5, 2012 at 8:1 Comment(1)
this will work, but only when you extend the dialog box, not a fast solution but good one....Settlings
L
10

One issue I found with all the existing answers is that the margins aren't preserved. This is because they all override the android:windowBackground attribute, which is responsible for margins, with a solid color. However, I did some digging in the Android SDK and found the default window background drawable, and modified it a bit to allow transparent dialogs.

First, copy /platforms/android-22/data/res/drawable/dialog_background_material.xml to your project. Or, just copy these lines into a new file:

<inset xmlns:android="http://schemas.android.com/apk/res/android"
    android:inset="16dp">
    <shape android:shape="rectangle">
        <corners android:radius="2dp" />
        <solid android:color="?attr/colorBackground" />
    </shape>
</inset>

Notice that android:color is set to ?attr/colorBackground. This is the default solid grey/white you see. To allow the color defined in android:background in your custom style to be transparent and show the transparency, all we have to do is change ?attr/colorBackground to @android:color/transparent. Now it will look like this:

<inset xmlns:android="http://schemas.android.com/apk/res/android"
    android:inset="16dp">
    <shape android:shape="rectangle">
        <corners android:radius="2dp" />
        <solid android:color="@android:color/transparent" />
    </shape>
</inset>

After that, go to your theme and add this:

<style name="MyTransparentDialog" parent="@android:style/Theme.Material.Dialog">
    <item name="android:windowBackground">@drawable/newly_created_background_name</item>
    <item name="android:background">@color/some_transparent_color</item>
</style>

Make sure to replace newly_created_background_name with the actual name of the drawable file you just created, and replace some_transparent_color with the desired transparent background.

After that all we need to do is set the theme. Use this when creating the AlertDialog.Builder:

    AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.MyTransparentDialog);

Then just build, create, and show the dialog as usual!

Libna answered 25/3, 2015 at 4:28 Comment(1)
I followed this with AppCompat AlertDialog but instead of using android:background, I chose to set a transparent color directly with the background drawable's android:color itself. Needed to do this because, setting android:background somehow left some areas still non transparent. But Thanks for sharing the technique!Orthognathous
K
7

Attention : Don't use builder for changing background.

Dialog dialog = new Dialog.Builder(MainActivity.this)
                                .setView(view)
                                .create();
dialog.show();dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);

change to

Dialog dialog = new Dialog(getActivity());
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(view);
dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
dialog.show();

When using Dialog.builder, it's not giving getWindow() option in it.

Kacikacie answered 28/7, 2018 at 5:19 Comment(0)
B
4

Try this in your code:

getWindow().setBackgroundDrawableResource(android.R.color.transparent);

it will definately working...in my case...! my frend

Bellyful answered 2/7, 2013 at 13:53 Comment(0)
O
4

This is what I did to achieve translucency with AlertDialog.

Created a custom style:

<style name="TranslucentDialog" parent="@android:style/Theme.DeviceDefault.Dialog.Alert">
    <item name="android:colorBackground">#32FFFFFF</item>
</style>

And then create the dialog with:

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.TranslucentDialog);
AlertDialog dialog = builder.create();
Orthognathous answered 30/3, 2016 at 14:18 Comment(0)
B
3

Same solution as zGnep but using xml:

android:background="@null"
Boyhood answered 29/5, 2012 at 8:8 Comment(0)
B
3

Use this code, it's works for me:

Dialog dialog = new Dialog(getActivity(),android.R.style.Theme_Translucent_NoTitleBar);
dialog.show();
Boardinghouse answered 22/9, 2014 at 11:56 Comment(1)
I think it would be more helful for the OP and further visitors, when you add some explaination to your intension.Nonobservance
L
3

You can use

dialog.window?.setBackgroundDrawableResource(android.R.color.transparent)

If you need to reuse this,

I would recommend creating an extension function. Something like extensions.kt

import android.app.Dialog

fun Dialog.setTransparentBackground() {
    window?.setBackgroundDrawableResource(android.R.color.transparent)
}

and use it on any dialog by calling

dialog.setTransparentBackground()

Have some fun programming...

Lussi answered 28/9, 2021 at 16:54 Comment(0)
T
2

If you use Kotlin, this code can help you:

dialog.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
Tawnyatawsha answered 19/10, 2021 at 8:49 Comment(0)
S
1

In my case solution works like this:

final Drawable drawable = new ColorDrawable(android.graphics.Color.TRANSPARENT);
dialogAssignTag.getWindow().setBackgroundDrawable(drawable);

Additionally, in xml file of your custom dialog:

android:alpha="0.8"
Salinasalinas answered 12/9, 2014 at 8:36 Comment(0)
F
1

In case you extended the DialogFrament class, you can set the theme with:

setStyle(DialogFragment.STYLE_NORMAL, R.style.customDialogTheme);

And then make the custom theme in your styles.xml file (see @LongLv's answer for parameters)

Don't forget to add <item name="android:windowCloseOnTouchOutside">true</item> if you want the dialog to close if the user touches outside the dialog.

Filly answered 10/4, 2018 at 20:40 Comment(0)
N
1

Set these style code in style

<style name="Theme.Transparent" parent="android:Theme">
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:backgroundDimEnabled">false</item>
</style>

And simply change false to true below line

<item name="android:backgroundDimEnabled">true</item>

It will dim your background.

Natka answered 2/1, 2019 at 9:0 Comment(0)
T
1

In my case, nothing was working to apply a transparent background.

Only I used in my dialog onStart():

dialog?.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))

But it was not taking any effect. I checked styles.xml, nothing linked to my problem.

Finally, when I was checking how my dialog is gets created I found that the navigation component was creating the dialog whenever I request the dialog fragment.

There In XML of navgraph.xml, I defined the dialog fragment as a fragment so, It was created as a fragment instead of a dialog. Changing that fragment to dialog made everything fall in place.

BTW: You cannot modify from fragment to dialog in the GUI of the navigation editor. You should change by hand in code.

This might be one of a cause when some effect on a dialog is not reflected in runtime.

Trovillion answered 11/2, 2022 at 9:27 Comment(0)
T
0
Window window = d.getWindow();
window.setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND,WindowManager.LayoutParams.FLAG_BLUR_BEHIND);

this is my way, you can try!

Throaty answered 29/5, 2012 at 8:15 Comment(1)
This is deprecated now: @deprecated Blurring is no longer supported.Cyclic
C
0

For anyone using a custom dialog with a custom class you need to change the transparency in the class add this line in the onCreate():

getWindow().setBackgroundDrawableResource(android.R.color.transparent);
Caliche answered 5/11, 2018 at 8:28 Comment(0)
P
0
ColorDrawable drawable = new ColorDrawable(ContextCompat.getColor(ctx, android.R.color.transparent));
dialog.getWindow().setBackgroundDrawable(drawable);
Paramecium answered 27/1, 2020 at 21:12 Comment(0)
M
0

Make sure R.layout.themechanger has no background color because by default the dialog has a default background color.

You also need to add dialog.getWindow().setBackgroundDrawable(newColorDrawable(Color.TRANSPARENT));

And finally

<style name="TransparentDialog">
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowTitleStyle">@null</item>
</style>
Mccue answered 13/3, 2020 at 1:0 Comment(0)
C
0

In Kotlin you can use this code:

val dialog = Dialog(context)
dialog.window?.decorView?.background = null
Cerated answered 28/12, 2022 at 16:2 Comment(0)
M
-1

Kotlin way to create dialog with transparent background:

 Dialog(activity!!, R.style.LoadingIndicatorDialogStyle)
            .apply {
                // requestWindowFeature(Window.FEATURE_NO_TITLE)
                setCancelable(true)
                setContentView(R.layout.define_your_custom_view_id_here)

                //access your custom view buttons/editText like below.z
                val createBt = findViewById<TextView>(R.id.clipboard_create_project)
                val cancelBt = findViewById<TextView>(R.id.clipboard_cancel_project)
                val clipboard_et = findViewById<TextView>(R.id.clipboard_et)
                val manualOption =
                    findViewById<TextView>(R.id.clipboard_manual_add_project_option)

                //if you want to perform any operation on the button do like this

                createBt.setOnClickListener {
                    //handle your button click here
                    val enteredData = clipboard_et.text.toString()
                    if (enteredData.isEmpty()) {
                        Utils.toast("Enter project details")
                    } else {
                        navigateToAddProject(enteredData, true)
                        dismiss()
                    }
                }

                cancelBt.setOnClickListener {
                    dismiss()
                }
                manualOption.setOnClickListener {
                    navigateToAddProject("", false)
                    dismiss()
                }
                show()
            }

Create LoadingIndicatorDialogStyle in style.xml like this:

<style name="LoadingIndicatorDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:statusBarColor">@color/black_transperant</item>
    <item name="android:layout_gravity">center</item>
    <item name="android:background">@android:color/transparent</item>
    <!--<item name="android:windowAnimationStyle">@style/MaterialDialogSheetAnimation</item>-->
</style>
Mentholated answered 23/11, 2020 at 14:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.