Browse Source

3 new global theming attributes.

Aidan Follestad 10 years ago
parent
commit
4efd3f8379

+ 29 - 9
README.md

@@ -6,6 +6,12 @@ The code you see below is also found in the sample project. You can download a A
 
 ### What's New
 
+###### Version 0.4.0
+
+> 1. Yet more bug fixes and improvements throughout
+> 2. Action button selectors have rounded corners
+> 3. More global theming capabilities. Override the accent color used for action buttons, titles, and content from your Activity theme. See the [Global Theming](#global-theming) section below.
+
 ###### Version 0.3.5 – 0.3.6
 
 > 1. Bug fixes.
@@ -31,12 +37,6 @@ The code you see below is also found in the sample project. You can download a A
 > 2. Convenience `show()` method in Builder, to skip call to `build()`.
 > 3. Various important fixes from pull requests and the maintainer.
 
-###### Version 0.2.0
-
-> 1. Action buttons must be explicitly shown by setting text to them. The buttons will be hidden in any dialog type if no text is passed for them. This also allows you to display neutral or negative action buttons individually without relying on positive text. 
-> 2. List dialogs now use CharSequence arrays rather than String arrays.
-> 3. Other bug fixes are included.
-
 ---
 
 ### Gradle Dependency (jCenter)
@@ -45,7 +45,7 @@ Easily reference the library in your Android projects using this dependency in y
 
 ```Groovy
 dependencies {
-    compile 'com.afollestad:material-dialogs:0.3.6'
+    compile 'com.afollestad:material-dialogs:0.4.0'
 }
 ```
 
@@ -359,6 +359,7 @@ new MaterialDialog.Builder(this)
         .neutralColor(materialRed500)
         .titleAlignment(Alignment.CENTER)
         .titleColor(materialRed500)
+        .contentColor(Color.WHITE)
         .theme(Theme.DARK)
         .show();
 ```
@@ -369,14 +370,33 @@ To see more colors that fit the Material design palette, see this page: http://w
 
 ### Global Theming
 
-If you don't want to manually make calls to `theme()` everytime you construct a dialog, you can put a single attribute in
-your Activity themes that makes all dialogs dark.
+By default, the dialog inherits and extracts theme colors from other attributes and theme colors of the app
+or operating system. This behavior can be overridden in your Activity themes:
 
 ```xml
 <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
 
+    <!--
+        All dialogs will default to Theme.DARK
+    -->
     <item name="md_dark_theme">true</item>
 
+    <!--
+        By default, the title text is pure black or pure white based on the theme
+    -->
+    <item name="md_title_color">#E91E63</item>
+
+    <!--
+        By default, the content text is derived from the ?android:textColorSecondary OS attribute
+    -->
+    <item name="md_content_color">#9C27B0</item>
+
+    <!--
+        By default, the accent color is derived from the colorAccent attribute of AppCompat or
+        android:colorAccent attribute of the Material theme
+    -->
+    <item name="md_accent_color">#673AB7</item>
+
 </style>
 ```
 

+ 2 - 2
library/build.gradle

@@ -9,7 +9,7 @@ android {
         minSdkVersion 8
         targetSdkVersion 21
         versionCode 1
-        versionName "0.3.7"
+        versionName "0.4.0"
     }
     buildTypes {
         release {
@@ -32,7 +32,7 @@ publish {
     userOrg = 'drummer-aidan'
     groupId = 'com.afollestad'
     artifactId = 'material-dialogs'
-    version = '0.3.7'
+    version = '0.4.0'
     description = 'A library for implementing Material design styled dialogs across all versions of Android.'
     website = 'https://github.com/afollestad/material-dialogs'
     issueTracker = "${website}/issues"

+ 5 - 1
library/src/main/java/com/afollestad/materialdialogs/DialogUtils.java

@@ -19,9 +19,13 @@ public class DialogUtils {
     }
 
     public static int resolveColor(Context context, int attr) {
+        return resolveColor(context, attr, 0);
+    }
+
+    public static int resolveColor(Context context, int attr, int fallback) {
         TypedArray a = context.getTheme().obtainStyledAttributes(new int[]{attr});
         try {
-            return a.getColor(0, 0);
+            return a.getColor(0, fallback);
         } finally {
             a.recycle();
         }

+ 16 - 8
library/src/main/java/com/afollestad/materialdialogs/MaterialDialog.java

@@ -117,9 +117,6 @@ public class MaterialDialog extends DialogBase implements View.OnClickListener,
         this.positiveText = builder.positiveText;
         this.neutralText = builder.neutralText;
         this.negativeText = builder.negativeText;
-        this.positiveColor = builder.positiveColor;
-        this.negativeColor = builder.negativeColor;
-        this.neutralColor = builder.neutralColor;
         this.items = builder.items;
         this.setCancelable(builder.cancelable);
         this.selectedIndex = builder.selectedIndex;
@@ -127,6 +124,17 @@ public class MaterialDialog extends DialogBase implements View.OnClickListener,
         this.autoDismiss = builder.autoDismiss;
         this.adapter = builder.adapter;
 
+        this.positiveColor = builder.positiveColor;
+        this.negativeColor = builder.negativeColor;
+        this.neutralColor = builder.neutralColor;
+
+        final int mdAccentColor = DialogUtils.resolveColor(mContext, R.attr.md_accent_color);
+        if (mdAccentColor != 0) {
+            if (this.positiveColor == 0) this.positiveColor = mdAccentColor;
+            if (this.negativeColor == 0) this.negativeColor = mdAccentColor;
+            if (this.neutralColor == 0) this.neutralColor = mdAccentColor;
+        }
+
         title = (TextView) view.findViewById(R.id.title);
         icon = (ImageView) view.findViewById(R.id.icon);
         titleFrame = view.findViewById(R.id.titleFrame);
@@ -208,7 +216,8 @@ public class MaterialDialog extends DialogBase implements View.OnClickListener,
             if (builder.titleColor != -1) {
                 title.setTextColor(builder.titleColor);
             } else {
-                title.setTextColor(DialogUtils.resolveColor(getContext(), android.R.attr.textColorPrimary));
+                final int fallback = DialogUtils.resolveColor(getContext(), android.R.attr.textColorPrimary);
+                title.setTextColor(DialogUtils.resolveColor(getContext(), R.attr.md_title_color, fallback));
             }
             if (builder.titleAlignment == Alignment.CENTER) {
                 title.setGravity(Gravity.CENTER_HORIZONTAL);
@@ -217,19 +226,18 @@ public class MaterialDialog extends DialogBase implements View.OnClickListener,
             }
         }
 
-        //Content color is set here.
         if (builder.contentColor != -1) {
             content.setTextColor(builder.contentColor);
         } else {
-            content.setTextColor(DialogUtils.resolveColor(getContext(), android.R.attr.textColorSecondary));
+            final int fallback = DialogUtils.resolveColor(getContext(), android.R.attr.textColorSecondary);
+            content.setTextColor(DialogUtils.resolveColor(getContext(), R.attr.md_content_color, fallback));
         }
 
         invalidateActions();
         setOnShowListenerInternal();
         setViewInternal(view);
 
-        if (builder.theme == Theme.LIGHT && Build.VERSION.SDK_INT <=
-                Build.VERSION_CODES.GINGERBREAD_MR1) {
+        if (builder.theme == Theme.LIGHT && Build.VERSION.SDK_INT <= Build.VERSION_CODES.GINGERBREAD_MR1) {
             setInverseBackgroundForced(true);
             title.setTextColor(Color.BLACK);
             content.setTextColor(Color.BLACK);

+ 4 - 0
library/src/main/res/values/attrs.xml

@@ -3,6 +3,10 @@
 
     <attr name="md_selector" format="reference" />
     <attr name="md_divider" format="color" />
+
     <attr name="md_dark_theme" format="boolean" />
+    <attr name="md_title_color" format="boolean" />
+    <attr name="md_content_color" format="boolean" />
+    <attr name="md_accent_color" format="boolean" />
 
 </resources>

+ 1 - 1
sample/build.gradle

@@ -9,7 +9,7 @@ android {
         minSdkVersion 11
         targetSdkVersion 21
         versionCode 44
-        versionName "0.3.7"
+        versionName "0.4.0"
     }
     buildTypes {
 //        release {

+ 1 - 0
sample/src/main/java/com/afollestad/materialdialogssample/MainActivity.java

@@ -394,6 +394,7 @@ public class MainActivity extends ActionBarActivity implements FolderSelectorDia
                 .negativeColorRes(R.color.material_red_400)
                 .titleAlignment(Alignment.CENTER)
                 .titleColorRes(R.color.material_red_400)
+                .contentColorRes(android.R.color.white)
                 .theme(Theme.DARK)
                 .build()
                 .show();