- How can I change the background of Android alert dialogs?
- 3 Answers 3
- How to set Transparent Background as a Custom Dialog Box in android
- 8 Answers 8
- How to set dialog window background to transparent, without affecting its margin
- 6 Answers 6
- API >= 23
- API TRANSPARENT . For this you can use setAlpha(. ) method on InsetDrawable .
- Dialog with transparent background in Android
- 23 Answers 23
- Android Dialog — Rounded Corners and Transparency
- 11 Answers 11
How can I change the background of Android alert dialogs?
I use the AlertDialog class in my application. By default, these alert dialogs have a transparent background. I’m trying to use an opaque background instead, very unsuccessfully. These are my styles:
I applied the «MyOpaqueActivity» style successfully for whole activities (the window background is changed to «my_background»), but it doesn’t work for alert dialogs within those activities. The «alertDialogStyle» attribute and my «MyOpaqueAlertDialog» style don’t seem to have any effect.
So how can I change the background of these alert dialogs?
3 Answers 3
Your approach won’t work. It seems AlertDialog (and Builder) hardcode the theme and don’t honor alertDialogStyle anywhere:
They came to the same conclusion here.
A custom dialog class derived from AlertDialog that calls the protected constructor AlertDialog(context, R.style.MyOpaqueAlertDialog) would be a workaround.
In the latest android source, there is a new public constructor for AlertDialog.Builder that takes a theme argument. Unfortunately, it hasn’t been released yet (maybe in Gingerbread?).
It’s easy to do this in an XML style once you figure that AlertDialogs have somewhat special attributes.
However, it would be interesting to know which Android version you’re referring to (or are targeting at). Theme.Dialog is relatively new as far as I know. Newer Android versions also don’t use AlertDialogs under all circumstances where older versions used them.
This works for me in Android 2.2:
Newer Android versions also have android:progressLayout and android:horizontalProgressLayout attributes for the AlertDialog style.
Also, in newer Android versions, it is possible to refer to an AlertDialog theme by using alertDialogTheme instead of alertDialogStyle in a custom theme. AlertDDialogThemes support more familiar and more powerful attributes like windowBackground, windowTitleStyle etc. Have a look at styles.xml and themes.xml.
Unfortunately, the documentation of which feature was added when is super poor. Please find out for yourself whcih approach is the best for you.
Источник
How to set Transparent Background as a Custom Dialog Box in android
I need to make my custom dialog box as a transparent.
How do set Transparent Dialog Box.
How do avoid this block color.
8 Answers 8
Use dialog instead of AlertDialog
If you are on API >= 11, you can try to set a theme to your Dialog like that :
Or you can try to set the background transparent :
Just create a style and apply that to your dialog
Then, create your Dialog by using this theme;
Hope this will help you to achieve your goal.
add the code above after this line.
Use dialog.getWindow().setBackgroundDrawable(null) to remove the default background.
Here is the doc for reference:
This question of yours describes exactly what my problem was. I tried every single solution I stumbled upon in this thread as well as in several more threads. Nothing worked. Finally I stumbled upon this thread and the corresponding answer.
I had to create a custom dialog class. So I did and it worked. Actually the following styles were sufficient for me:
The code of the dialog class:
And how I use this dialog in the activity code:
I don’t know why I have to create a custom class in order for the theme to be taken into account. Now I have achieved what I wanted to do: transparent dialog. The next fight will be to position positive and negative buttons as long as I can no longer user the convenience methods of AlertDialog.Builder . Hopefully my code will prove of help to you.
Also notice the low value of alpha — maybe even try it with 00 so that you make sure whether your configuration works.
Источник
How to set dialog window background to transparent, without affecting its margin
Currently, I have the following dialog, which I will perform expand/ collapse animation on its items.
This dialog is created via the following code
However, when animation being performed, here’s the side effect.
Note, the unwanted extra white region at the dialog after animation, is not caused by our custom view. It is the system window white background of the dialog itself.
I tend to make the system window background of the dialog, to become transparent.
Although the unwanted white background is no longer seen, the original margin of the dialog is gone too. (The dialog width is now full screen width)
How can I make it transparent, without affecting its margin?
6 Answers 6
There’s a pretty easy way to do that:
You need to «modify» the Drawable that is being used as a background of the Dialog . Those sort of Dialogs use an InsetDrawable as a background.
API >= 23
Only SDK API 23+ allows you to get the source Drawable wrapped by the InsetDrawable ( getDrawable() method). With this, you can do whatever you want — e.g. change color to something completely different (like RED or something). If you use this approach remember that the wrapped Drawable is a GradientDrawable and not a ColorDrawable !
API TRANSPARENT . For this you can use setAlpha(. ) method on InsetDrawable .
EDIT (as a result of Cheok Yan Cheng’s comments):
or you can actually skip casting to InsetDrawable and get the same result. Just remember that doing so will cause the alpha to be changed on the InsetDrawable itself and not on the Drawable that is wrapped by the InsetDrawable .
Try to below Theme:
Try below code to apply Theme to AlertDialog.Builder :
I hope help you !
The background image abc_popup_background_mtrl_mult which is part of the compat library contains already a margin in the picture information.
This is why the margin goes away when you remove the background image. I strongly recommend not to use the ViewTreeObserver , it will been called multiple times and can cause performance issues. Better work with the screen size:
Your problem is properly in the layout try to check the views with the Hierarchy viewer.
just add this line after show dialog. I would prefer using Dialog instedof using AlertDialog
Let’s start with Google recommendation which says to use DialogFragment instead of a simple Dialog.
@rekire is right that margins set by drawable, going forward it is set by either 9 patch or programmatically depending on theme.
So you either can set your padding to your content view or create dialog using DialogFragment here is an example which changes height of dialog based on it’s content, and note you don’t need to use tree observer which is as mentioned before may cause performance issue.
So the example
Note: I wrapped everything into scroll view and set padding you can skip it if you want.
and finally you can show your dialog with this piece of code
I will not go into details why Fragment dialog is better but one thing is clear that you can encapsulate logic for it and have separate class. Hope this solves your issue.
Источник
Dialog with transparent background in Android
How do I remove the black background from a dialog box in Android. The pic shows the problem.
23 Answers 23
Or this one instead:
I hope help 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
in your main manifest file , inside the block of the dialog activity.
Plus in your dialog activity XML set
One can set this theme to dialog as below
You can use the:
method.And following is the doc:
Dialog pop up fill default black background color or theme color so you need to set TRANSPARENT background into Dialog. Try below code:-
if you want destroy dark background of dialog , use this
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:
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:
After that, go to your theme and add this:
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 :
Then just build, create, and show the dialog as usual!
Источник
Android Dialog — Rounded Corners and Transparency
I’m trying to make a custom android dialog with rounded corners. My current attempts have given me this result.
As you can see, the corners are rounded, but it leaves the white corner still intact.
Below is the xml that I put in the drawable folder to create the blue dialog with the red border with the rounded corners.
Below is the layout of the dialog.
And below is the Activity in which I create the dialog.
11 Answers 11
The only solution I have found is here. Use Dialog instead of AlertDialog and set transparent background:
dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
Therefore you can’t use the builder. But you can use new Dialog() also in onCreateDialog callback of DialogFragment if you follow to best guidelines.
This works also for Gingerbread.
Besides the layered drawable can be simplified to one shape with xml element for the border.
I had similar issue when made dialog extending DialogFragment and to fix this used:
Hope this helps.
The below code solved the issue
In you java file keep below code and change your layout name
just try using this, this worked for me
Use 9-patch PNG with transparency in those corners.
I understood that activity’s background makes sense. So use @robert’s answer with these changes.
in DialogFragment layout set width and height or add minimum sizes:
Remove @color/. from styles of needed activities and set these backgrounds in activity’s layouts.
In DialogFragment write:
According to robert answer, you should apply setStyle(STYLE_NO_FRAME, 0) , but there appear new problems. If you have a narrow DialogFragment like in Custom dialog too small, then you should follow this guide.
Add to styles.xml these 3 lines for dialog size:
In layout of your DialogFragment add style:
In code of your DialogFragment write:
Look at AndroidManifest.xml and find all activities that can show these dialogs, check android:theme=». » themes and go to styles.xml . Now take a look at @color/. items of these themes. There should be a transparent color or these items might not exist. If you have these background items, whole activities will have those backgrounds and dialogs too! So, if you have a camera activity with DialogFragment above it, you will see this.
Remove background items of needed styles. Also maybe background is set in code, check it.
In Dialog with transparent background in Android and many pages it is written to add one of these:
probably in onViewCreated() or onCreateDialog() , but it didn’t help me, because the background of the Activity was set in styles.
Tested on Samsung Galaxy S4 running Android 5.0.1.
Источник