May I know is it possible to change the font typeface of ProgressDialog
's message display, within DialogFragment
?
public class LoadFromCloudTaskFragment extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
this.progressDialog = new ProgressDialog(this.getActivity());
this.progressDialog.setMessage(progressMessage);
this.progressDialog.setCanceledOnTouchOutside(false);
return progressDialog;
}
Create a custom class by inheriting from ProgressDialog
might be one of the ways. However, I wish to know is there any better alternative? Sadly, we do not have ProgressDialog.Builder
.
One of the alternative I had tried is
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
this.progressDialog = new ProgressDialog(this.getActivity());
this.progressDialog.setMessage(progressMessage);
this.progressDialog.setCanceledOnTouchOutside(false);
Utils.setCustomFont(this.progressDialog.findViewById(android.R.id.message), Utils.ROBOTO_LIGHT_FONT);
return progressDialog;
}
But this will give me error
android.util.AndroidRuntimeException: requestFeature() must be called before adding content
Typeface font = Typeface.createFromAsset(getContext().getAssets(), "fonts/Roboto-Regular.ttf");
SpannableStringBuilder spannableSB = new SpannableStringBuilder(getString(R.string.text_please_wait));
spannableSB.setSpan (new CustomTypefaceSpan("fonts/Roboto-Regular.ttf", font), 0, spannableSB.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
loadDialog = ProgressDialog.show(this, null, spannableSB, true, false);
public class CustomTypefaceSpan extends TypefaceSpan {
private final Typeface newType;
public CustomTypefaceSpan(String family, Typeface type) {
super(getContext(),family);
newType = type;
}
@Override
public void updateDrawState(TextPaint ds) {
applyCustomTypeFace(ds, newType);
}
@Override
public void updateMeasureState(TextPaint paint) {
applyCustomTypeFace(paint, newType);
}
private static void applyCustomTypeFace(Paint paint, Typeface tf) {
int oldStyle;
Typeface old = paint.getTypeface();
if (old == null) {
oldStyle = 0;
} else {
oldStyle = old.getStyle();
}
int fake = oldStyle & ~tf.getStyle();
if ((fake & Typeface.BOLD) != 0) {
paint.setFakeBoldText(true);
}
if ((fake & Typeface.ITALIC) != 0) {
paint.setTextSkewX(-0.25f);
}
paint.setTypeface(tf);
}
}
As seen from documentation: http://developer.android.com/reference/android/app/DialogFragment.html
public static class MyDialogFragment extends DialogFragment {
static MyDialogFragment newInstance() {
return new MyDialogFragment();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.hello_world, container, false);
View tv = v.findViewById(R.id.text);
((TextView)tv).setText("This is an instance of MyDialogFragment");
return v;
}
}
I'd suspect you can to provide custom layout XML for the DialogFragment.
After I'd proceed with setting typeface with this utility class:
import java.util.Hashtable;
import java.util.Map;
import android.content.Context;
import android.graphics.Typeface;
import android.widget.TextView;
/**
* Taken from bug on b.android.com
* https://code.google.com/p/android/issues/detail?id=9904
* <p>
* Optimizes way to work with typefaces and avoids context related memory leak
* */
public class Typefaces {
private static final Map<String, Typeface> cache = new Hashtable<String, Typeface>();
public static Typeface get(Context c, String name) {
synchronized (cache) {
if (!cache.containsKey(name)) {
Typeface t = Typeface.createFromAsset(c.getAssets(),
String.format("fonts/%s.ttf", name));
cache.put(name, t);
}
return cache.get(name);
}
}
public static Typeface _default(Context c) {
return get(c, "verdana");
}
public static void setFonts(Context c, TextView... tvs) {
for (TextView t : tvs) {
if (t != null)
t.setTypeface(_default(c));
}
}
}
Which assumes you have custom font placed in assets/fonts/verdana.ttf
(if using _default()
method)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With