You could use a SpannableString, set the typeface on it and return it back to the AlertDialog.Builder
This is a helper function that adds a typeface to a CharSequence and returns a SpannableString -
private static SpannableString typeface(Typeface typeface, CharSequence chars) {
if (chars == null) {
return null;
}
SpannableString s = new SpannableString(chars);
s.setSpan(new TypefaceSpan(typeface), 0, s.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
return s;
}
Class that sets the TypeFace on the text -
public class TypefaceSpan extends MetricAffectingSpan {
private final Typeface typeface;
public TypefaceSpan(Typeface typeface) {
this.typeface = typeface;
}
@Override
public void updateDrawState(TextPaint tp) {
tp.setTypeface(typeface);
tp.setFlags(tp.getFlags() | Paint.SUBPIXEL_TEXT_FLAG);
}
@Override
public void updateMeasureState(TextPaint p) {
p.setTypeface(typeface);
p.setFlags(p.getFlags() | Paint.SUBPIXEL_TEXT_FLAG);
}
}
While creating the dialog you can substitute the strings with the SpannableString like so -
public static Dialog createDialog(Context c, String title, String message, String pButton, String nButton, AlertCallback callback) {
AlertDialog.Builder builder = new AlertDialog.Builder(c);
builder.setMessage(typeface(Fonts.Regular, message));
builder.setTitle(typeface(Fonts.Bold, title));
builder.setPositiveButton(typeface(Fonts.Bold, pButton),callback::onPositiveButtonClick);
builder.setNegativeButton(typeface(Fonts.Bold, nButton),callback::onNegativeButtonClick);
AlertDialog dialog = builder.create();
return builder.create();
}
I'd recommend to load the fonts into a cache, rather than calling createFromAsset multiple times.
Hope this helps!