2

I am using this code to apply a color with trasparent background.

dialog.getWindow().setBackgroundDrawable(new ColorDrawable(MyColor));

Its works very well on my phone, but in my tablet its not working

show that have a white and gray background on my dialig (i use a custom view and background are set @null):

enter image description here

Kristy Welsh
  • 7,828
  • 12
  • 64
  • 106
rcorbellini
  • 1,307
  • 1
  • 21
  • 42

11 Answers11

7

Based on your post, I have some questions that maybe could be causing those issues with transparency.

1.- Since your tablet is showing a different rendering, could be the case that the OS API is below your version on your phone? If that's the case, try using the proper support library that match your phone OS API.

2.- Assuming that both devices are similar on OS API, you could try two different methods.

a) Passing ZERO as parameter for ColorDrawable :)

    getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(0));

b) Create a Different activity just to display your dialog and isolate the theme related to your parent activity (that for sure that is causing the issue), do something like below.

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

The above is the theme you have to associate to your dialog activity, then on your XML layout use the following.

android:background="#aa000000" 

You can apply this approach to dialog, activities that you will require to display a DEMO Screen or instructional :)

Carlos
  • 963
  • 9
  • 19
5

You can try this, but it seems this is already suggested...?

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

Instead, have you tried something like this?

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

Then, when you make your dialog, try this....?

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

This is a bit overkill, but may be needed as well if you really need it:

<resources xmlns:android="http://schemas.android.com/apk/res/android">

<style name="TransparentDialog" parent="@android:style/Theme.Dialog">
    <item name="android:windowFrame">@null</item>
    <item name="android:windowBackground">@color/orange_transparent</item>
    <item name="android:windowIsFloating">false</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowTitleStyle">@null</item>
    <item name="android:colorBackgroundCacheHint">@null</item>
    <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
    <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
    <item name="android:gravity">center</item>
</style>

</resources>
ngoctranfire
  • 793
  • 9
  • 15
3

The problem is that AlertDialog builder is actually not good for designing transparent dialog and will and always have black background which is actually a Theme for it, instead use the Dialog to create a transparent theme instead.

Sample:

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

Using Dialog does not require any theme manipulation for transparent background so it is basically easy.

Arjun Singh
  • 724
  • 1
  • 8
  • 24
1

This worked for me:

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

if not, look at this post

Community
  • 1
  • 1
Dgotlieb
  • 1,393
  • 2
  • 9
  • 15
1

USE THIS:

alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
  alertDialog.show();
1

You need disable dim too:

Window w = dialog.getWindow();
w.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
LayoutParams lp = w.getAttributes();
lp.flags &= ~LayoutParams.FLAG_DIM_BEHIND; 
lp.dimAmount = 0;
w.setAttributes(lp);
Enyby
  • 4,162
  • 2
  • 33
  • 42
0

I would suggest just create an activity with transparent background and then you can display whatever you want in your activity based on your requirement and customize it easily. Here is something to get you started

How do I create a transparent Activity on Android?

Helped me a lot while i was trying to achieve something similar for displaying the busy screen within my app. I hope you find this helpful and do let me know which path did you take. Best of luck.

Community
  • 1
  • 1
Gaurav Sarma
  • 2,248
  • 2
  • 24
  • 45
0
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));

This will be fine in api below lolipop and for above or lolipop use

android:background="@android:color/transparent"

in parent layout of dialog fragment, so in a nutsell use both to overcome this issue in all api.

Shahab Rauf
  • 3,651
  • 29
  • 38
  • it is exactly what i use, My "new ColorDrawable(MyColor)" is a transparent color, and my custom xml have a transparent bg – rcorbellini Mar 28 '16 at 11:25
0

First add a style like this

<style name="customStyleDialog">
    <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">true</item>
    <item name="android:background">@android:color/transparent</item>
</style>

after that when you creating your dialog you set this style too as follows.

Dialog dialog = new Dialog(getActivity(), R.style.customStyleDialog);
// other settings to the dialog
// for making work transparency on low level devices add next line too after show
// dialog 

dialog.show();
dialog.getWindow().setBackgroundDrawable(newColorDrawable(android.graphics.Color.TRANSPARENT));

make sure you add a background color on your view that set to this dialog through

.setContentView(--);
0

Use theme in dialog.

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


public class RProgressDg extends Dialog {

    public RProgressDg(Context context) {
        this(context, R.style.Dialog_Transparent);
    }

    public RProgressDg(final Context context, int theme) {
        super(context, theme);
        this.setContentView(R.layout.dg_pro_round);
    }

}
tiny sunlight
  • 6,231
  • 3
  • 21
  • 42
0

For Kotlin you can change this line to the new line:

old:

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

new:

 dialog?.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
Mori
  • 2,653
  • 18
  • 24