Transparent dialog doesn't work in some devices - android

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):

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 :)

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>

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.

This worked for me:
alertDialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
if not, look at this post

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

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);

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.

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.

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(--);

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);
}
}

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))

Related

Android Loading Dialog Set Background with Custom Layout [duplicate]

How do I remove the black background from a dialog box in Android. The pic shows the problem.
final Dialog dialog = new Dialog(Screen1.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.themechanger);
Add this code
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
Or this one instead:
dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
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 !
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"
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!!
if you want destroy dark background of dialog , use this
dialog.getWindow().setDimAmount(0);
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();
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
*/
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!
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.
Try this in your code:
getWindow().setBackgroundDrawableResource(android.R.color.transparent);
it will definately working...in my case...! my frend
You can use(optional)
dialog.window?.setBackgroundDrawableResource(android.R.color.transparent)
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
dialog.setTransparentBackground()
Have some fun programming...
Same solution as zGnep but using xml:
android:background="#null"
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();
Use this code, it's works for me:
Dialog dialog = new Dialog(getActivity(),android.R.style.Theme_Translucent_NoTitleBar);
dialog.show();
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.
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.
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.
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"
Window window = d.getWindow();
window.setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND,WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
this is my way, you can try!
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);
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>
ColorDrawable drawable = new ColorDrawable(ContextCompat.getColor(ctx, android.R.color.transparent));
dialog.getWindow().setBackgroundDrawable(drawable);
In Kotlin you can use this code:
val dialog = Dialog(context)
dialog.window?.decorView?.background = null
If you use Kotlin, this code can help you:
dialog.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
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>

Transparent Dialog background in Android

Edit:
Answer found Here
Why the background of ProgressDialog doesn't set to the transparent?
I know this question has many duplicates, but i can't seem to find a solution.
I want to make my Dialog Background transparent, But it seems not to be working.
This is my code in xml.
<style name="TransparentProgressDialog" >
<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>
And this is the output.
Issues 1: It shows a grey background and Shadow around it.
I want only the animation dialog to show.
I've tried using a transparent background image, but still shows the same grey background.
I've also tried using
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
in my Java code but didn't work still.

Change Android Dialog background to different color

I have a Dialog that I display in my application, but I want to be able to change the semi-transparent black background to be 100% transparent. Like below, the part that grays out the activity behind the dialog, I'd like that to be transparent so that I can see the underlying activity as it, instead of being darkened. Thanks for your help!
I was able to achieve my desired result by creating the Dialog and passing a style to its constructor like so...
Dialog ad = new Dialog(context, R.style.SpinnerDialogDropdown);
<style name="SpinnerDialogDropdown">
<item name="android:windowFrame">#null</item>
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
**<item name="android:backgroundDimEnabled">false</item>**
</style>

ActionBarSherlock doesn't support light-theme alert dialogs?

Well as the title says, i'm using the actionBarSherlock library and a light theme, and sometimes I need to show a dialog using the alertDialog.Builder class.
Thing is, no matter what I try, the theme doesn't apply to the dialog itself. The theme should work on both new APIs and old ones (prior to honeycomb).
example:
code:
final AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(activity,
R.style.AppTheme_LightDialog));
or:
final AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(activity,
R.style.Theme_Sherlock_Light_Dialog));
xml:
<style name="AppTheme_LightDialog" parent="#android:style/Theme.Light">
<item name="android:windowFrame">#null</item>
<item name="android:windowTitleStyle">#style/DialogWindowTitle.Sherlock.Light</item>
<item name="android:windowBackground">#drawable/abs__dialog_full_holo_light</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowAnimationStyle">#android:style/Animation.Dialog</item>
<item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
<item name="android:windowActionBar">false</item>
<item name="android:windowActionModeOverlay">true</item>
<item name="android:windowCloseOnTouchOutside">true</item>
<item name="android:windowNoTitle">true</item>
<item name="android:backgroundDimAmount">0.6</item>
<item name="android:colorBackgroundCacheHint">#null</item>
<item name="android:textColorPrimary">#color/abs__primary_text_holo_light</item>
<item name="android:textColorPrimaryInverse">#color/abs__primary_text_holo_dark</item>
<item name="windowMinWidthMajor">#dimen/abs__dialog_min_width_major</item>
<item name="windowMinWidthMinor">#dimen/abs__dialog_min_width_minor</item>
<item name="windowActionBar">false</item>
<item name="windowContentOverlay">#null</item>
<item name="android:textAppearance">?android:attr/textAppearanceInverse</item>
</style>
I know i can use the dialogFragment, but is there another way? the dialogs are quite simple, and as there are many of them, it would be annoying to change them all.
EDIT: I might be wrong, but it seems that using the android:alertDialogStyle attribute (shown here) changes it for old APIs , but it doesn't have a lot of customization. In fact, it supports only colors, and I'm not sure how to set even the text color (of the title and/or the message).
After researching a bit, I think it's not an ActionBarScherlock issue, but a Light Theme issue in alert dialogs. Let's try some things:
Use:
final AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(activity, R.style.AppTheme_LightDialog));
Change:
<style name="AppTheme_LightDialog" parent="#android:style/Theme.Light">
To:
<style name="AppTheme_LightDialog" parent="#android:style/Theme.Dialog">
Then override the default "Theme.Dialog" styles (copy-pasted from the Android git tree):
<style name="AppTheme_LightDialog" parent="#android:style/Theme.Dialog">
<item name="android:windowFrame">#null</item>
<item name="android:windowTitleStyle">#android:style/DialogWindowTitle</item>
<item name="android:windowBackground">#android:drawable/panel_background</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowAnimationStyle">#android:style/Animation.Dialog</item>
<item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
</style>
You may need to copy the original resources (#android:style/DialogWindowTitle, #android:style/Animation.Dialog and #android:drawable/panel_background) to your project.
And finally, the tricky part (from Shawn Castrianni ), as it seems Android needs some extra help to apply correctly a style to AlertDialog's text. Add to your "AppTheme_LightDialog" style:
<item name="android:textColor">?android:attr/textColorPrimaryInverseDisableOnly</item>
UPDATE:
It seems that prior to Honeycomb text styling is not actually applied to AlertDialogs. The above code gives you a solution to >=Honeycomb devices. There's an interesting work-around to make it work also in those devices (check this and this), but you may want to start asking you if you prefer a different approach which requires less work.
BTW, I'm not sure if it's your case, but it's important that you also use the same ContextThemeWrapper if you inflate a custom layout for the AlertDialog. For example,
Change:
View view = View.inflate(activity, R.layout.myDialog, null);
To:
View view = View.inflate(new ContextThemeWrapper(activity, R.style.AppTheme_LightDialog), R.layout.myDialog, null);
This is what I did and it made the body of the dialog white. The title is still on a black background:
new AlertDialog.Builder(
new ContextThemeWrapper(
activity,
R.style.Theme_Sherlock_Light));
I also tried Theme_Sherlock_Light_NoActionBar, but it doesn't seem to make any difference.

Dialog with transparent background in Android

How do I remove the black background from a dialog box in Android. The pic shows the problem.
final Dialog dialog = new Dialog(Screen1.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.themechanger);
Add this code
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
Or this one instead:
dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
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 !
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"
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!!
if you want destroy dark background of dialog , use this
dialog.getWindow().setDimAmount(0);
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();
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
*/
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!
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.
Try this in your code:
getWindow().setBackgroundDrawableResource(android.R.color.transparent);
it will definately working...in my case...! my frend
You can use(optional)
dialog.window?.setBackgroundDrawableResource(android.R.color.transparent)
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
dialog.setTransparentBackground()
Have some fun programming...
Same solution as zGnep but using xml:
android:background="#null"
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();
Use this code, it's works for me:
Dialog dialog = new Dialog(getActivity(),android.R.style.Theme_Translucent_NoTitleBar);
dialog.show();
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.
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.
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.
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"
Window window = d.getWindow();
window.setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND,WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
this is my way, you can try!
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);
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>
ColorDrawable drawable = new ColorDrawable(ContextCompat.getColor(ctx, android.R.color.transparent));
dialog.getWindow().setBackgroundDrawable(drawable);
In Kotlin you can use this code:
val dialog = Dialog(context)
dialog.window?.decorView?.background = null
If you use Kotlin, this code can help you:
dialog.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
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>

Categories

Resources