Change the typeface of ProgressDialog in Android

Here, below example uses CustomFontDialog style to change the font of progressBar message from default to custom.
This example uses setCustomTitle() method to change progressDialog title font to custom.

MainActivity.java
package com.example.espl.changeprogressdialogfontdemo;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.Typeface;
import android.support.v4.content.res.ResourcesCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.view.ContextThemeWrapper;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    private ProgressDialog pDialog;
    Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button=findViewById(R.id.btn);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showProgressDialog(MainActivity.this,"Please wait..","Loading data in page",true);
            }
        });
    }

    public void showProgressDialog(Context context, String Title, String Message, boolean isCancelable) {
        try {
            if (pDialog == null)
                //                pDialog = new ProgressDialog(context);
                //use the Custom theme
             //To change android Progress Dialog message typeface
                pDialog = new ProgressDialog(new ContextThemeWrapper(context, R.style.CustomFontDialog));

            if (Title != null && Title.length() > 0){
                //                                pDialog.setTitle(Title);
                TextView tv_title = new TextView(this);
                tv_title.setText(Title);
                tv_title.setTextSize(dpToPxl(10));
                tv_title.setPadding(dpToPxl(15),dpToPxl(5),0,dpToPxl(5));
                tv_title.setTextColor(Color.RED);
                Typeface typeface = ResourcesCompat.getFont(context, R.font.work_sans_regular);
                tv_title.setTypeface(typeface);
//                To Change progressDialog title font to custom
                pDialog.setCustomTitle(tv_title);
            }

            if (Message != null && Message.length() > 0){
                pDialog.setMessage(Message);
            }

            pDialog.setCancelable(isCancelable);

            if (context instanceof Activity) {
                if (!((Activity) context).isFinishing()) {
                    pDialog.show();
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static int dpToPxl(int dp) {
        return (int) (dp * Resources.getSystem().getDisplayMetrics().density);
    }
}

styles.xml
<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

    <style name="CustomFontDialog" parent="Theme.AppCompat.Light.Dialog">
        <item name="android:fontFamily">@font/work_sans_regular</item>
    </style>
</resources>

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rl"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp"
    tools:context=".MainActivity"
    android:background="#f9fff5">

    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Show progressDialog with custom font for title and message"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</RelativeLayout>

Default theme for ProgressDialog title and messag font
Default theme font for title and message

Change android Progress Dialog message typeface and custom font for title
Custom font for title and message


No comments:

Post a Comment

Popular Posts