Reputation: 10590
I need to change the style for every single dialog in my application. My understanding, after reading through the Styles and Themes documentation (which is fairly poor, really), I came up with this snippet:
styles.xml
<resources>
<style name="RMTheme" parent="@android:style/Theme">
<item name="android:dialogLayout">@style/CustomDialog</item>
</style>
<style name="CustomDialog" parent="@android:style/Theme.Dialog">
<item name="android:windowBackground">@color/transparent_color</item>
<item name="android:textColorPrimary">@android:color/black</item>
</style>
</resources>
This should, if properly applied, create every dialog with a transparent background and black text.
Here's where I apply the theme.
AndroidManifest.xml
<application
android:theme="@style/RMTheme"
android:debuggable="true"
android:label="@string/app_name"
android:icon="@drawable/icon">
I'm guessing the problem lies in the way I'm trying to apply my own namespace's Theme.Dialog
to the android namespace's Theme.Dialog
. Basically, and I think this is clear from the code, I just want to add/override some properties in android's default dialog style.
EDIT: Note also that I do not want to (and in fact cannot, since I'm compiling for API level 9) use the Dialog(Context context, int style)
constructor. I want to apply the theme globally without explicity passing the theme id to every dialog constructor, and furthermore, it's not available to me at this API level anyway.
EDIT: Woops... apparently Dialog(Context context, int style)
IS available at this API level. It was AlertDialog(Context context, int style)
I was thinking of. Nonetheless, I still want to apply this theme globally for all dialogs rather than having to use that constructor.
Upvotes: 3
Views: 7618
Reputation: 64
Just simply put this in your theme:
<item name="android:dialogTheme">@style/MyDialogTheme</item>
and in style.xml define MyDialogTheme as follows:
<style name="MyDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorSecondary">@color/colorSecondary</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:textColorPrimary">@color/text_color</item>
<item name="android:textColor">@color/text_color</item>
<item name="android:windowBackground">@color/background</item>
</style>
Also make sure your base theme has parent set to Theme.AppCompat.
Upvotes: 0
Reputation: 1544
Another trick is this:
Instead of
new Dialog(getApplicationContext(), ...)
Do
new Dialog(MyActivity.this, ...)
Upvotes: -1
Reputation: 2170
android:dialogLayout
does not exist in API 8 if you look at themes.xml
included with the Android SDK. You can find all relevant xml files under "Android SDK Folder"/platforms/android-8/data/res/values
This is the only property in style/Theme
for that API level that is related to Dialog
.
<!-- Dialog attributes -->
<item name="alertDialogStyle">@android:style/AlertDialog</item>
That then references to this:
<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>
</style>
These are different 9 patch drawables, the OS selects it based on your theme, Froyo offered a few such as Theme.Black
and Theme.Light
.
For API level 8, that´s it! The only thing you seem to be able to change via XML are those images relating to the dialog's background. Newer OS versions allow you to override the alertDialogTheme
attribute, which gives you much more control via XML.
I make an ultra simple AlertDialog
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("TESTING 123");
builder.setPositiveButton("YES", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
builder.setNegativeButton("NO", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
AlertDialog dialog = builder.create();
dialog.show();
This is how it looks on a Gingerbread device (too lazy to create a Froyo emulator...)
Now we override the alert dialog style via the XML.
<style name="AppBaseTheme" parent="android:style/Theme">
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<item name="android:alertDialogStyle">@style/NewAlertDialog</item>
</style>
<style name="NewAlertDialog">
<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>
We add the theme to the application
tag in the manifest file.
Now the Dialog looks like this: android:theme="@style/AppTheme"
Upvotes: 1