Browse Source

Default typefaces are no longer used, typeface(String, String) added to the Builder.

Aidan Follestad 10 years ago
parent
commit
bc3b3b3955

+ 24 - 10
library/src/main/java/com/afollestad/materialdialogs/MaterialDialog.java

@@ -94,11 +94,6 @@ public class MaterialDialog extends DialogBase implements View.OnClickListener {
         super(getTheme(builder));
         mBuilder = builder;
 
-        if (mBuilder.regularFont == null)
-            mBuilder.regularFont = TypefaceHelper.get(getContext(), "Roboto-Regular");
-        if (mBuilder.mediumFont == null)
-            mBuilder.mediumFont = TypefaceHelper.get(getContext(), "Roboto-Medium");
-
         this.view = LayoutInflater.from(getContext()).inflate(R.layout.md_dialog, null);
         this.setCancelable(builder.cancelable);
 
@@ -177,7 +172,7 @@ public class MaterialDialog extends DialogBase implements View.OnClickListener {
                 sv.setPadding(0, paddingTop, 0, paddingBottom);
                 sv.setClipToPadding(false);
 
-                ScrollView.LayoutParams innerViewLayoutParams=
+                ScrollView.LayoutParams innerViewLayoutParams =
                         new ScrollView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                                 ViewGroup.LayoutParams.WRAP_CONTENT);
                 innerViewLayoutParams.setMargins(frameMargin, 0, frameMargin, 0);
@@ -992,18 +987,37 @@ public class MaterialDialog extends DialogBase implements View.OnClickListener {
         }
 
         /**
-         * Sets the fonts used in the dialog.
+         * Sets the fonts used in the dialog. It's recommended that you use {@link #typeface(String, String)} instead,
+         * to avoid duplicate Typeface allocations and high memory usage.
          *
-         * @param medium  The font used on titles and action buttons. Null uses the default.
-         * @param regular The font used everywhere else, like on the content and list items. Null uses the default.
+         * @param medium  The font used on titles and action buttons. Null uses device default.
+         * @param regular The font used everywhere else, like on the content and list items. Null uses device default.
          * @return The Builder instance so you can chain calls to it.
+         * @deprecated It's recommended that you use {@link #typeface(String, String)} instead, to avoid duplicate Typeface allocations and high memory usage.
          */
-        public Builder typeface(@NonNull Typeface medium, @NonNull Typeface regular) {
+        @Deprecated
+        public Builder typeface(Typeface medium, Typeface regular) {
             this.mediumFont = medium;
             this.regularFont = regular;
             return this;
         }
 
+        /**
+         * Sets the fonts used in the dialog, by file names. This also uses TypefaceHelper in order
+         * to avoid any un-needed allocations (it recycles typefaces for you).
+         *
+         * @param medium  The name of font in assets/fonts, minus the extension (null uses device default). E.g. [your-project]/app/main/assets/fonts/[medium].ttf
+         * @param regular The name of font in assets/fonts, minus the extension (null uses device default). E.g. [your-project]/app/main/assets/fonts/[regular].ttf
+         * @return The Builder instance so you can chain calls to it.
+         */
+        public Builder typeface(String medium, String regular) {
+            if (medium != null)
+                this.mediumFont = TypefaceHelper.get(this.context, medium);
+            if (regular != null)
+                this.regularFont = TypefaceHelper.get(this.context, regular);
+            return this;
+        }
+
         public Builder icon(@NonNull Drawable icon) {
             this.icon = icon;
             return this;

+ 1 - 0
library/src/main/java/com/afollestad/materialdialogs/base/DialogBase.java

@@ -124,6 +124,7 @@ public class DialogBase extends AlertDialog implements DialogInterface.OnShowLis
     }
 
     protected void setTypeface(TextView text, Typeface t) {
+        if (t == null) return;
         int flags = text.getPaintFlags() | Paint.SUBPIXEL_TEXT_FLAG;
         text.setPaintFlags(flags);
         text.setTypeface(t);