ActionBarSherlock doesn't support light-theme alert dialogs? - android

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.

Related

MaterialComponents theme alert dialog buttons

Recently I switched from support library to com.google.android.material:material:1.0.0
But now I have a problem, in this pages there's a note https://github.com/material-components/material-components-android/blob/master/docs/getting-started.md
Note: Using a Material Components theme enables a custom view inflater which replaces default components with their Material counterparts. Currently, this only replaces Button XML components with MaterialButton.
And the theme I am using
Theme.MaterialComponents.Light.NoActionBar
does exactly what it says in that note, it replaces AlertDialog Buttons to MaterialButtons but the problem is that by default MaterialButtons are colored background and now the buttons looks like this:
How can I make them borderless and backgroundless again?
PS I am using alert builder to create alert dialogs:
android.app.AlertDialog.Builder
I figured out what was causing this problem. I need to use different AlertDialog class:
androidx.appcompat.app.AlertDialog
When I switched to this everything started working as expected. Here's where I found the solution:
https://github.com/material-components/material-components-android/issues/162
When using com.google.android.material:material:1.0.0 and androidx.appcompat.app.AlertDialog you can customize each button in the buttonBar by using Widget.MaterialComponents.Button.TextButton as parent.
val builder: AlertDialog.Builder = AlertDialog.Builder(ContextThemeWrapper(context, R.style.AlertDialogTheme))
Use the default layout or add a custom by builder.setView(R.layout.my_dialog)
In your styles:
<style name="AlertDialogTheme" parent="Theme.MaterialComponents.Light.Dialog.Alert">
<item name="buttonBarPositiveButtonStyle">#style/Alert.Button.Positive</item>
<item name="buttonBarNegativeButtonStyle">#style/Alert.Button.Neutral</item>
<item name="buttonBarNeutralButtonStyle">#style/Alert.Button.Neutral</item>
</style>
<style name="Alert.Button.Positive" parent="Widget.MaterialComponents.Button.TextButton">
<item name="backgroundTint">#color/transparent</item>
<item name="rippleColor">#color/colorAccent</item>
<item name="android:textColor">#color/colorPrimary</item>
<item name="android:textSize">14sp</item>
<item name="android:textAllCaps">false</item>
</style>
<style name="Alert.Button.Neutral" parent="Widget.MaterialComponents.Button.TextButton">
<item name="backgroundTint">#color/transparent</item>
<item name="rippleColor">#color/colorAccent</item>
<item name="android:textColor">#color/gray_dark</item>
<item name="android:textSize">14sp</item>
</style>
If you are using the Material Components library the best way to have an AlertDialog is to use the MaterialAlertDialogBuilder.
new MaterialAlertDialogBuilder(context)
.setTitle("Dialog")
.setMessage("Lorem ipsum dolor ....")
.setPositiveButton("Ok", /* listener = */ null)
.setNegativeButton("Cancel", /* listener = */ null)
.show();
It is the default result:
If you want also to apply a different style or color to the buttons you can check this answer.
I tested the above answers. Although I got a good idea, none worked for my case. So, this is my answer.
Make sure to have android:theme="#style/AppMaterialTheme" in your manifest file under Application or Activity.
Open your Styles.xml file and change it based on the following.
<style name="AppMaterialTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<item name="colorPrimary">#color/primaryBlue</item>
<item name="colorPrimaryDark">#color/primaryBlue</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="colorControlActivated">#color/primaryBlue</item>
<item name="colorControlHighlight">#color/colorAccent_main</item>
<item name="colorButtonNormal">#color/white</item>
<item name="materialAlertDialogTheme">#style/AlertDialogMaterialTheme</item>
</style>
<style name="AlertDialogMaterialTheme" parent="ThemeOverlay.MaterialComponents.MaterialAlertDialog">
<item name="buttonBarPositiveButtonStyle">#style/Alert.Button.Positive</item>
<item name="buttonBarNegativeButtonStyle">#style/Alert.Button.Negative</item>
</style>
<style name="Alert.Button.Positive" parent="Widget.MaterialComponents.Button.UnelevatedButton">
<item name="android:fillColor">#color/color_0054BB</item>
<item name="android:textColor">#color/white</item>
<item name="android:textAllCaps">false</item>
<item name="android:textSize">14sp</item>
<item name="rippleColor">#color/colorAccent_main</item>
</style>
<style name="Alert.Button.Negative" parent="Widget.MaterialComponents.Button.OutlinedButton">
<item name="strokeColor">#color/color_0054BB</item>
<item name="android:textColor">#color/color_0054BB</item>
<item name="android:textAllCaps">false</item>
<item name="android:textSize">14sp</item>
<item name="android:layout_marginEnd">8dp</item>
<item name="rippleColor">#color/colorAccent_main</item>
</style>
You won't need to apply the theme to your AlertDialog as your Activity applies the theme to it. So, create the dialog normally.
The result will be.
First, it's better to use use MaterialAlertDialog if you are using Material Theme.
You can read more here – Material.io → Theming dialogs
MaterialAlertDialogBuilder(context)
.setTitle(R.string.confirm)
.setMessage(R.string.logout)
.setPositiveButton(R.string.logout_alert_positive) { _, _ -> activity?.logout() }
.setNegativeButton(R.string.never_mind, null)
.show()
This is the layout.xml of the MaterialAlertDialog actions. As you can see there are 3 buttons and each has their own styles. So, here is how you can change them.
Step 1: Tell Android that you want to alter the default MaterialAlertDialog theme.
<style name="Base.AppTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar">
...
<item name="materialAlertDialogTheme">#style/AlertDialog</item>
...
</style>
Step 2: Tell Android that you want to alter a specific button style. buttonBarNeutralButtonStyle, buttonBarNegativeButtonStyle or buttonBarPositiveButtonStyle
<style name="AlertDialog" parent="ThemeOverlay.MaterialComponents.MaterialAlertDialog">
<item name="buttonBarNegativeButtonStyle">#style/NegativeButtonStyle</item>
</style>
Step 3: Define your custom style
<style name="NegativeButtonStyle" parent="Widget.MaterialComponents.Button.TextButton">
<item name="android:textColor">#FF0000</item>
</style>
Found another solution for this with using MaterialComponents here: https://issuetracker.google.com/issues/116861837#comment9
<style name="Theme.Custom.Material.Alert.Dialog.Light" parent="Theme.MaterialComponents.Light.Dialog.Alert">
<item name="materialButtonStyle">#style/Widget.AppCompat.Button.Borderless</item>
</style>
<style name="Theme.Custom.Material.Base.Light" parent="Theme.MaterialComponents.Light.NoActionBar">
<item name="android:dialogTheme">#style/Theme.Custom.Material.Alert.Dialog.Light</item>
<item name="android:alertDialogTheme">#style/Theme.Custom.Material.Alert.Dialog.Light</item>
....
</style>
Though it is still not "intended behavior" to me.
If you don't want to use androidx.appcompat.app.AlertDialog, you can just redefine the style of the dialog buttons:
In your style.xml :
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
...
<item name="android:buttonBarButtonStyle">#style/DialogButton</item>
...
</style>
<style name="DialogButton" parent="Widget.MaterialComponents.Button.TextButton"/>
If you are using the com.android.support:design:28.0.0 library, using android.support.v7.app.AlertDialog works as expected.

Transparent dialog doesn't work in some devices

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

Dialog skinning with AppCompat-v7 22 results in ugly shadows on api < 21

I'm using AppCompat to write a material design styled app. Since AppCompat does not affect dialogs, I'm skinning the dialogs as such:
styles.xml:
<style name="AppTheme.Base" parent="Theme.AppCompat">
<!-- Set AppCompat’s color theming attrs -->
<item name="colorPrimary">#color/green</item>
<item name="colorPrimaryDark">#color/green_darker</item>
<item name="colorAccent">#color/accent</item>
<item name="android:alertDialogTheme">#style/alertDialog</item>
<item name="android:dialogTheme">#style/alertDialog</item>
</style>
<style name="alertDialog" parent="Theme.AppCompat.Dialog">
<item name="colorPrimary">#color/green</item>
<item name="colorPrimaryDark">#color/green_darker</item>
<item name="colorAccent">#color/accent</item>
</style>
I am gettings exactly what I wanted on android api >= 21, but on other devices I end up with a "box" around the dialogs.
Is there a way to get rid of the "box" around the dialog and even get the colors and material theme applied on api < 21, preferably without any additional depencendies?
App on Api < 21:
App on API >= 21:
With the new AppCompat v22.1 you can use the new android.support.v7.app.AlertDialog or the new AppCompatDialog
Just use a code like this (of course in your case you have to use a custom layout to have the progress bar)
import android.support.v7.app.AlertDialog
AlertDialog.Builder builder =
new AlertDialog.Builder(this, R.style.AppCompatAlertDialogStyle);
builder.setTitle("Dialog");
builder.setMessage("Lorem ipsum dolor ....");
builder.setPositiveButton("OK", null);
builder.setNegativeButton("Cancel", null);
builder.show();
And use a style like this:
<style name="AppCompatAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="colorAccent">#FFCC00</item>
<item name="android:textColorPrimary">#FFFFFF</item>
<item name="android:background">#5fa3d0</item>
</style>
In case anyone is still looking for a simple and effective solution, just add this line to your "alertDialog" style:
<item name="android:windowBackground">#android:color/transparent</item>
P.S. changing this property also affects PreferenceFragment's dialogs on API >= 21, so make sure you are using different styles: with transparent bg for API < 21, and without any changes for API >= 21
I had the exact same problem with the new AppCompat 22 library when using android.support.v7.app.AlertDialog for all my AlertDialogs, but the extra background layers only affected ProgressDialogs.
All my AlertDialogs when styled using my custom theme looked great, but the ProgressDialog's had the weird overlay on the background as described in the OP.
An option I had was to set a specific style each time I constructed a ProgressBar, but I was looking for an application wide solution.
With the help of these two links:
How to Style AlertDialogs like a Pro and Joerg Richter Blog I was able to get rid of the extra layer drawn on < 21 ProgressDialogs.
The issue I found was that on all versions the ProgressBar draws its background based on what is defined by default in "android:alertDialogStyle".
So to get rid of the extra layer, I had to define my own styles and set them for "android:alertDialogStyle". In doing so, I'm also overriding the default layouts applied to ProgressDialogs.
Here's my themes.xml:
<item name="android:alertDialogTheme">#style/MyAlertDialogTheme</item>
<item name="alertDialogTheme">#style/MyAlertDialogTheme</item>
<item name="android:alertDialogStyle">#style/MyAlertDialogStyles</item>
And my styles.xml:
<style name="MyAlertDialogTheme">
<item name="android:windowIsFloating">true</item>
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowAnimationStyle">#android:style/Animation.Dialog</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:background">#color/theme_alert_dialog_background</item>
<item name="colorAccent">#color/theme_accent_1</item>
</style>
<style name="AlertDialog">
<item name="android:fullDark">#android:color/transparent</item>
<item name="android:topDark">#android:color/transparent</item>
<item name="android:centerDark">#android:color/transparent</item>
<item name="android:bottomDark">#android:color/transparent</item>
<item name="android:fullBright">#android:color/transparent</item>
<item name="android:topBright">#android:color/transparent</item>
<item name="android:centerBright">#android:color/transparent</item>
<item name="android:bottomBright">#android:color/transparent</item>
<item name="android:bottomMedium">#android:color/transparent</item>
<item name="android:centerMedium">#android:color/transparent</item>
</style>
ProgressDialog - Try for below android 5
dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
or
dialog.getWindow().clearFlags(LayoutParams.FLAG_DIM_BEHIND);
You may download revision 22.1.0 (updated a few days ago) and use android.support.v7.app.AlertDialog

Can't find source of TYPE_SYSTEM_ALERT

For a project I want to create my own window style. I mean something like this:
<style name="MyFloatingWindow">
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:background">#android:color/transparent</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowNoTitle">true</item>
</style>
To get some ideas how I can implement my ideas I want to look into the style source of andriod but I can't find it. What is the original style of WindowManager.LayoutParams.TYPE_SYSTEM_ALERT?
Or is it possible to create a new layout style with TYPE_SYSTEM_ALERT as parent? What do I have to write as parent?
<style name="MyOverlay" parent="android:***">
</style>
The TYPE_SYSTEM_ALERT simply means that the windows are always on top of application windows. In multiuser systems shows only on the owning user's window. It has nothing to do with the any style of your window or how the window will look like.
There are a lot of pre-defined styles in styles.xml under frameworks\base\core\res\res\values. For example the AlertDialog.
<style name="AlertDialog">
<item name="fullDark">#android:drawable/popup_full_dark</item>
<item name="topDark">#android:drawable/popup_top_dark</item>
<item name="centerDark">#android:drawable/popup_center_dark</item>
<item name="bottomDark">#android:drawable/popup_bottom_dark</item>
<item name="fullBright">#android:drawable/popup_full_bright</item>
<item name="topBright">#android:drawable/popup_top_bright</item>
<item name="centerBright">#android:drawable/popup_center_bright</item>
<item name="bottomBright">#android:drawable/popup_bottom_bright</item>
<item name="bottomMedium">#android:drawable/popup_bottom_medium</item>
<item name="centerMedium">#android:drawable/popup_center_medium</item>
<item name="progressLayout">#android:layout/progress_dialog</item>
<item name="horizontalProgressLayout">#android:layout/alert_dialog_progress</item>
</style>

Android: DialogPreference color/style?

My main preference activity is set to "#android:style/Theme.Light". One of my preferences is a DialogPreference who's Dialog contains a ListView. The ListView's dialog is dark grey (because DialogPreference uses AlertBuilder which creates dark grey dialogs) and the text in the list is black (because Theme.Light causes listViews to have black text). Is there an easy way to either get the ListView to behave with the same style as the dark dialog? Or to get the dark dialog to behave with the same style as the light activity?
EDIT:
Based on Merlin's comments, it seems like what I should try to do is create a LightDialog Theme. In order to do this I tried: 1. extending android's Theme.Light and adding the dialogy properties from Theme.Dialog
<style name="Theme.LightDialog" parent="#android:style/Theme.Light">
<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>
</style>
... and 2. extending android's Theme.Dialog and adding the lighty properties from Theme.Light.
<style name="Theme.LightDialog" parent="#android:style/Theme.Dialog">
<item name="android:windowBackground">#drawable/screen_background_light</item>
<item name="android:colorBackground">#android:color/background_light</item>
<item name="android:colorForeground">#androidcolor/bright_foreground_light</item>
<item name="android:colorForegroundInverse">#android:color/bright_foreground_light_inverse</item>
<item name="android:textColorPrimary">#android:color/primary_text_light</item>
<item name="android:textColorSecondary">#android:color/secondary_text_light</item>
<item name="android:textColorTertiary">#android:color/tertiary_text_light</item>
<item name="android:textColorPrimaryInverse">#android:color/primary_text_dark</item>
<item name="android:textColorSecondaryInverse">#android:color/secondary_text_dark</item>
<item name="android:textColorTertiaryInverse">#android:color/tertiary_text_dark</item>
<item name="android:textColorPrimaryDisableOnly">#android:color/primary_text_light_disable_only</item>
<item name="android:textColorPrimaryInverseDisableOnly">#android:color/primary_text_dark_disable_only</item>
<item name="android:textColorPrimaryNoDisable">#android:color/primary_text_light_nodisable</item>
<item name="android:textColorSecondaryNoDisable">#android:color/secondary_text_light_nodisable</item>
<item name="android:textColorPrimaryInverseNoDisable">#android:color/primary_text_dark_nodisable</item>
<item name="android:textColorSecondaryInverseNoDisable">#android:color/secondary_text_dark_nodisable</item>
<item name="android:textColorHint">#android:color/hint_foreground_light</item>
<item name="android:textColorHintInverse">#android:color/hint_foreground_dark</item>
</style>
Both of these attempts failed because they use non-public attributes. Any suggestions on how to create a LightDialog theme?
You can inherit defult themes when creating styles in Android.
Please read the documentation on Applying Styles and Themes specifically the section on inheritance
You should be able to take Theme.light and correct any issues that you have with it.
You may find that different vendors alter the themes on their devices so if you are targeting a broad range of hardware then you may be better creating a full theme to ensure that your app is consistent on all platforms.
UPDATE
As stated in this answer Consistent UI color in all Android devices there are public and non-public attributes. The answer provides a link to a list of public attributes however kernel.org is still down so you will need to dig through the source for core/res/res/values/public.xml
You can specify style of DialogPreference's dialog using android:alertDialogTheme (supported starting from API 11) them property of preferences activity:
<style name="PreferencesActivityTheme" parent="#style/Theme.AppCompat.Light.DarkActionBar">
<item name="android:alertDialogTheme">#style/Theme.MyDialog</item>
</style>
<style name="Theme.MyDialog" parent="#style/Theme.AppCompat.Light.Dialog">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
I have a solution how you can do it programmatically (I think its the easier way):
public class CustomDialogPreference extends DialogPreference {
...
#Override
protected void showDialog(Bundle state) {
super.showDialog(state);
//changing color of divider
int divierId = getDialog().getContext().getResources()
.getIdentifier("android:id/titleDivider", null, null);
View divider = getDialog().findViewById(divierId);
divider.setBackgroundColor(getContext().getResources().getColor(R.color.light_orange));
//changing color of title textview
int titleId = getDialog().getContext().getResources()
.getIdentifier("android:id/alertTitle", null, null);
TextView title = (TextView) getDialog().findViewById(titleId);
title.setTextColor(getContext().getResources().getColor(R.color.light_orange));
}
...
}

Categories

Resources