Cheok Yan Cheng Cheok Yan Cheng - 1 month ago 16
Android Question

Change font typeface of ProgressDialog within DialogFragment

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

Answer

One of the suggested solution is as follow. But I don't think this is a good way. Any further suggestion are very much welcomed.

public class ProgressDialogEx extends ProgressDialog {
    public ProgressDialogEx(Context context) {
        super(context);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View view = this.findViewById(android.R.id.message);
        if (view != null) {
            // Shouldn't be null. Just to be paranoid enough.
            Utils.setCustomTypeface(view, Utils.ROBOTO_LIGHT_FONT);
        }
    }
}

public class LoadFromCloudTaskFragment extends DialogFragment {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        this.progressDialog = new ProgressDialogEx(this.getActivity());
        this.progressDialog.setMessage(progressMessage);
        this.progressDialog.setCanceledOnTouchOutside(false);

        return progressDialog;
    }

Utils.setCustomTypeface

public static final Typeface ROBOTO_LIGHT_TYPE_FACE = Typeface.createFromAsset(MyApplication.instance().getAssets(), "fonts/Roboto-Light.ttf");

public static void setCustomTypeface(View view, Typeface typeFace) {
    if (view instanceof TextView) {
        ((TextView)view).setTypeface(typeFace);
    } else if (view instanceof EditText) {
        ((EditText)view).setTypeface(typeFace);
    } else if (view instanceof ViewGroup) {
        ViewGroup viewGroup = (ViewGroup)view;
        int count = viewGroup.getChildCount();
        for (int i = 0; i < count; i++) {
            setCustomTypeface(viewGroup.getChildAt(i), typeFace);
        }
    }
}
Comments