How can I change the color of the semi-transparent background that is shown behind a Dialog.
The default is black and I want to make it white (with the same alpha like the default)
This is the theme I'm using for my custom dialog:
another question - how can I make the dialog to fill the screen width (with 20dp margin on each side)
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="CustomDialogTheme" parent="android:Theme.Dialog">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">fill_parent</item>
<item name="android:padding">30dp</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">true</item>
<item name="android:background">#color/white</item>
</style>
</resources>
Make a xml layout and set the layout to the dialog.
This might help for the syntax: you must set the content of the dialog to a layout
http://www.helloandroid.com/tutorials/how-display-custom-dialog-your-android-application
This might help for the background: you must overwrite the theme/style
Android Custom PopupWindow/Dialog
Related
I've been overriding the default button style in my app like this:
<style name="ButtonStyle" parent="Base.Widget.AppCompat.Button">
<item name="android:textAllCaps">false</item>
<item name="android:textSize">18sp</item>
<item name="android:textColor">#color/primaryTextContrast</item>
<item name="android:background">#drawable/button</item>
</style>
That worked fine until Nougat, but with Nougat there's been a change (a bug fix I think) such that this style also applies to buttons in dialogs, while in previous versions it did not. This has the effect of giving the dialog buttons white text on a white background.
In case it's relevant, button is a 9 patch in drawable and is overridden in drawable-v21:
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="?attr/colorControlHighlight">
<item android:drawable="?attr/colorPrimary"/>
</ripple>
The primary color is dark, and primaryTextContrast is white. Dialogs do not get dark buttons in Nougat for some reason - they seem to pick up the text color but not the background. So I need to either let the dialog buttons do their default thing, or make the buttons fully inherit the style with a dark background and white text.
#nasch, Dialog buttons use button bar style. In this scenario, you could do something like
<style name="buttonBarButtonStyle" parent="Widget.AppCompat.Button.ButtonBar.AlertDialog">
<item name="android:textColor">#color/colorPrimary</item>
</style>
In your main style definition, you then define
<style name="MyCustomTheme" parent="Theme.AppCompat">
<item name="buttonBarButtonStyle">#style/buttonBarButtonStyle</item>
...
Hope this helps.
I want to change title color background(green) to full width.
<style name="AppCompatAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="colorAccent">#000000</item>
<item name="android:textColorPrimary">#5c5c5c</item>
<item name="android:background">#fbdd9d</item>
<item name="android:windowTitleStyle">#style/TitleDialogTextStyle</item>
</style>
<style name="TitleDialogTextStyle" parent="Base.DialogWindowTitle.AppCompat">
<item name="android:textColor">#ffffff</item>
<item name="android:background">#color/green</item>
</style>
Please find attached snap. I want green color to full width.
I made a lot of tries, I never succeded to change the color defining a custom
android:windowTitleStyle or a android:windowTitleBackgroundStyle.
It only works using the following trick, add to your custom dialog style :
<item name="android:topDark">#color/your_color</item>
And the title background will be colored as you want.
I have created my own custom dialog and it works fine but I want to change dimmed background to a custom pattern (for example an image file or a xml shape). How can I achieve that?
Note that I do not want to change intensity of dimming but I just want, this dimming be replaced with a pattern
I found a workaround for this problem, I derived this from #vipul mittal answer,
I should set dialog theme as following:
<item name="android:windowIsFloating">false</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">#android:color/transparent</item>
With this theme my dialog will be:
full screen because windowIsFloating is set to false
surrounding area is fully transparent because windowBackground is set to #android:color/transparent
Now I should wrap my dialog xml layout contents with a wrapper that plays surrounding area role, in this case I have picked FrameLayout for this:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/dialog_back"> <!-- this is surrounding area drawable -->
<!-- dialog contents goes here -->
</FrameLayout>
Here is a screenshot of my final dialog:
Change dialog theme to:
<style name="CustomDialogTheme" parent="#android:style/Theme.Dialog">
<item name="android:windowBackground">#android:color/white</item>
<item name="android:windowIsFloating">false</item>
<item name="android:windowNoTitle">true</item>
</style>
In custom dialog when you call super call this:
public CustomDialog(Context context) {
super(context, R.style.CustomDialogTheme);
}
Say I have a menu sliding out in android and I want to darken whatever was behind it.
I tied making a full screen layout and use a countdown timer to make its background gradually go from transparent to 50% black, but it gets slow as hell.
Are there any other options to make a gradual transition? Other then making it in a snap?
Thanks!
Why don't you use a theme? Under res/values, make a styles.xml file. In that, define
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Theme.Translucent" parent="android:Theme">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">#color/cache_color</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">true</item>
<item name="android:backgroundDimEnabled">true</item>
</style>
</resources>
Then in your manifest:
<activity android:name="com.example.FooActivity" android:theme="#style/Theme.Translucent"></activity>
I am making a custom dialog with a transparent window background set in the style. I have another button in my activity behind the dialog set with the same button_selector as the background, and it works fine, so I know the problem is something with the style and more specifically the windowBackground attribute.
Does anyone know how I can get a transparent window background for my custom dialog but still allow my button selector to work properly?
Included are pictures of how it looks with the button background set to #drawable/lightblue1, and #drawable/button_selector.
this is my style xml
<resources>
<style name="CustomDialogTheme" parent="#android:style/Theme.Dialog">
<item name="android:windowBackground">#color/transparent</item>
<item name="android:windowIsFloating">false</item>
<item name="android:windowNoTitle">true</item>
</style>
</resources>
If I remove the <item name="android:windowBackground">#color/transparent</item> line then my button selector works correctly, but my dialog is put inside the systems default dialog container background.
This is my button declaration xml. If i change #drawable/button_selector to one of the actual png files then it appears correctly, but with the selector my button background gets set to transparent.
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/button_selector"
android:layout_marginBottom="15dp"
android:textSize="35sp"
android:text="#string/btnText1">
</Button>
Here is how I create the dialog from java:
Dialog dialog = new Dialog(TimeClock.this, R.style.CustomDialogTheme);
dialog.setContentView(R.layout.tag);
dialog.show();
Here is the button_selector.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="#drawable/darkblue1" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="#drawable/darkblue1" /> <!-- focused -->
<item android:drawable="#drawable/lightblue1" /> <!-- default -->
</selector>
EDIT: I ended up "faking" my dialog with a translucent activity so that I could get better control over its appearance.
I don't understand exactly what is going wrong, but maybe it is worth defining your CustomDialogTheme based on an existing translucent theme? e.g.
<resources>
<style name="CustomDialogTheme" parent="#android:style/Theme.Translucent.NoTitleBar">
It is also possible that you may need to set some of the extra style items like (taken from this question):
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowIsFloating">true</item>
I was wondering if Android uses one of those settings to allow your button selector to work?