-2

What I'm trying to do is to change the font of progressBar message from default to custom. I've already tried many techniques and failed.

I've tried to change it via Styles and then apply this style to my dialog:

<style name="StyledDialog" parent="@android:style/Theme.Panel">
    <item name="android:background">@android:color/transparent</item>
    <item name="android:alertDialogStyle">@style/CustomStyle</item>
    <item name="fontPath">fonts/Lato-Regular.ttf</item> // not working
</style>

<style name="CustomStyle">
    <item name="fontPath">fonts/Lato-Regular.ttf</item> // not working
</style>

Then I tried to get the message in TextView, but I'm getting null

dialog = new ProgressDialog(getContext(), R.style.StyledDialog); //tried with styles here.
dialog.create(); //yep, create the dialog, see that dialogs's onCreate method was executed;
TextView view = (TextView) dialog.findViewById(R.id.message); // getting null
Typeface face = Typeface.createFromAsset(getAssets(),
        "fonts/epimodem.ttf");
view.setTypeface(face); //null pointerException

I'm using Calligraphy library to change the font of all the textViews I have. So do you have any idea how to change the fond of the progressDialog I have

E_net4
  • 27,810
  • 13
  • 101
  • 139
nutella_eater
  • 3,393
  • 3
  • 27
  • 46

2 Answers2

3

Worked like a charm:

 dialog = new ProgressDialog(getContext());
        String message = getContext().getString(R.string.building_route);
        SpannableString spannableString =  new SpannableString(message);

        CalligraphyTypefaceSpan typefaceSpan = new CalligraphyTypefaceSpan(TypefaceUtils.load(getContext().getAssets(), "fonts/Lato-Regular.ttf"));
        spannableString.setSpan(typefaceSpan, 0, message.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        dialog.setMessage(spannableString);
        dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        dialog.setIndeterminate(true);
        dialog.setCancelable(false);
        dialog.show();
nutella_eater
  • 3,393
  • 3
  • 27
  • 46
  • Thank you and It is better to link the source. Need to add dependencies dependencies { compile 'uk.co.chrisjenx:calligraphy:2.2.0' } Custom fonts in Android the easy way... https://github.com/chrisjenx/Calligraphy – Eftekhari Mar 26 '17 at 18:13
0

You can use SpannableString without using stiles.xml

ProgressDialog dialog = new ProgressDialog(this);
dialog.create();

SpannableString ss=  new SpannableString("Your message text here");
Typeface typeFace = Typeface.createFromAsset(getAssets(), "fonts/epimodem.ttf");
ss.setSpan(new RelativeSizeSpan(1.0f), 0, ss.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
ss.setSpan(new CustomTypefaceSpan("", typeFace), 0, ss.length(), 0);

dialog.setMessage(ss);
dialog.show();

CustomTypefaceSpan.java

@SuppressLint("ParcelCreator")
public class CustomTypefaceSpan extends TypefaceSpan {

    private final Typeface newType;

    public CustomTypefaceSpan(String family, Typeface type) {
        super(family);
        newType = type;
    }

    @Override
    public void updateDrawState(TextPaint ds) {
        applyCustomTypeFace(ds, newType);
    }

    @Override
    public void updateMeasureState(TextPaint paint) {
        applyCustomTypeFace(paint, newType);
    }

    private 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);
    }
}
Sergey Nikitin
  • 807
  • 8
  • 23