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);
}
Related
I created a new custom dialog with an oval background. However, there is a margin on the dialog which I do not know how to remove. I've tried to apply custom themes to the dialog, but so far I have not found a way to remove this margin.
What made the difference for styling was this custom style xml:
<style name="BokiDialogTheme" parent="Theme.AppCompat.Dialog">
<item name="android:windowIsFloating">false</item>
<item name="android:windowBackground">#android:color/transparent</item>
</style>
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>
I have a theme called "greenhighlight" — this theme was generated using the Android Action Bar Style Generator, and inherits from the default ActionBarSherlock theme. The theme does nothing except change the highlight at the bottom of the ActionBar from blue to green.
To theme all my activities, I just do:
<application android:theme="#style/Theme.greenhighlight"...
This works pretty well for activities (note the green highlight on the bottom of the ActionBar):
However, I'm having difficulty theming my dialogs to match my activities:
My "greenhighlight_Dialog" theme is defined as:
<style name="greenhighlight_Dialog" parent="#style/Theme.Sherlock.Dialog">
<item name="android:progressBarStyleHorizontal">
#style/greenhighlight_ProgressBar
</item>
</style>
I'm inheriting from the default Sherlock dialog theme, and overriding the progress bar using the progress bar style as defined by my generated "greenhighlight" theme — you can see that the progress bar is the correct shade of green in the screenshot above.
To use the theme, I'm running the following code:
ContextThemeWrapper ctw =
new ContextThemeWrapper(this, R.style.greenhighlight_Dialog);
AlertDialog.Builder builder = new AlertDialog.Builder(ctw);
...
ProgressDialog pd = new ProgressDialog(this, R.style.greenhighlight_Dialog);
...
My problem is that I have no idea what attributes I need to override. I've been looking through styles.xml and themes.xml as recommended by the Styles and Themes doco (which notes that "the R.style reference, however, is not well documented and does not thoroughly describe the styles") — but there are a lot of styles defined on Theme.Dialog and I'm unsure as to which ones I need to override to get the change I want.
What attributes to I need to override for my dialogs to have green title text, a green highlight bar underneath the title and green check marks for checked list items?
I went digging around the source and came across the alert_dialog_holo.xml layout file. This is the divider view:
<View android:id="#+id/titleDivider"
android:layout_width="match_parent"
android:layout_height="2dip"
android:visibility="gone"
android:background="#android:color/holo_blue_light" />
In the AlertController class, it's visibility is set to VISIBLE if there is a title present. Since the color is hardcoded, there's not going to be a way to overwrite this with a style attribute.
This is the title view:
<com.android.internal.widget.DialogTitle android:id="#+id/alertTitle"
style="?android:attr/windowTitleStyle"
android:singleLine="true"
android:ellipsize="end"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
So it uses the windowTitleStyle. It took a lot of chasing, but I eventually found the style that uses:
<style name="TextAppearance.Holo.DialogWindowTitle">
<item name="android:textSize">22sp</item>
<item name="android:textColor">#android:color/holo_blue_light</item>
</style>
Following the parent styles back, I was able to change the text color only via styles:
<style name="AppTheme" parent="#android:style/Theme.Holo">
<item name="android:alertDialogTheme">#style/AlertDialogStyle</item>
</style>
<style name="AlertDialogStyle" parent="#android:style/Theme.Holo.Dialog">
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowMinWidthMajor">#android:dimen/dialog_min_width_major</item>
<item name="android:windowMinWidthMinor">#android:dimen/dialog_min_width_minor</item>
<item name="android:windowTitleStyle">#style/DialogWindowTitle</item>
</style>
<style name="DialogWindowTitle">
<item name="android:maxLines">1</item>
<item name="android:scrollHorizontally">true</item>
<item name="android:textAppearance">#style/DialogWindowTitleAppearance</item>
</style>
<style name="DialogWindowTitleAppearance" parent="#android:style/TextAppearance.Holo.DialogWindowTitle">
<item name="android:textColor">#00ff00</item>
</style>
Now for the divider, you can't change it via styles, but since you know the id, you can extend the AlertDialog class and intercept it when it creates its layout (onCreate) and change it's color. Though this is handled in the private AlertController class, so I'm unsure how much luck you'll have. I'll look into this more and come back if I come up with anything.
I found a solution to remove the divider: just add the following line in your custom alert dialog style (please refer to AlertDialogStyle in Jason Robinson's answer):
<item name="android:divider">#null</item>"
Then the divider will gone.
If you still want to add a custom divider, you can add it in your custom content layout.
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
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?