6

How can we customize the fonts of android alert dialog using styles

I found many solution using setTypeFace() method. But I want to customize the entire application alert dialog using styles.

I would like to change the title, message, buttons fonts.

I was able to change the message font using the following codes.

My style declaration for alert dialog

<style name="MyAlertDialougeTheme" parent="@android:style/Theme.Material.Light.Dialog.Alert">
    <item name="android:textAppearanceSmall">@style/MyTextAppearance</item>
    <item name="android:textAppearanceLarge">@style/MyTextAppearance</item>
    <item name="android:textAppearanceMedium">@style/MyTextAppearance</item>

</style>

Java code to display alert dialog

 AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this,
                R.style.MyAlertDialougeTheme);
        builder.setTitle("Warning")
                .setMessage("My message here")
                .setPositiveButton("yes", null)
                .setNegativeButton("no", null)
                .show();

Review below screen

enter image description here

Please help me to change the title and buttons fonts using styles and also I would like to customize the font color for the negative & positive buttons.

Advance thanks for your time and help!!

Md. Zakir Hossain
  • 1,082
  • 11
  • 24
Anju mohan
  • 183
  • 2
  • 10

3 Answers3

0

First of all create a CustomDialog class which will extend Dialog class of Android. Following is the code for the same -

public class CustomDialog extends Dialog implements
        View.OnClickListener {

    Activity context;
    private Button mBtnOK;

    public CustomDialog(Activity context) {
        super(context);
        this.context = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.custom_popup_dialog_box);
        mBtnOK = findViewById(R.id.btn_ok);
        mBtnOK.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_ok:
                dismiss();
                break;
            default:
                break;
        }
        dismiss();
    }
}

Now here whenever you want an instance of Dialog box you just have to create the instance of your CustomDialog class and the xml here custom_popup_dialog_box will be having all the customization's like Font family, text size, color etc. You just need to set the property inside the xml or programmatically. Hope you find the solution. Let me know in case of further information. Thanks.

Md. Zakir Hossain
  • 1,082
  • 11
  • 24
Salman Khan
  • 2,822
  • 5
  • 32
  • 53
0

Java code to display alert dialog code replace this code

final Dialog dialog = new Dialog(activity);
            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            dialog.setCancelable(false);
            dialog.setContentView(R.layout.lay_alertdialog);  


     Objects.requireNonNull(dialog.getWindow()).setLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);


// view initialize

//sample 
    Button dialogButton = dialog.findViewById(R.id.btn_ok);
    dialogButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            dialog.dismiss();


        }
    });
//


    dialog.show();

layout save lay_alertdialog

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="24dp"
    android:gravity="center"
    android:orientation="vertical">


    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:gravity="start"
        android:padding="4dp"
        android:text="Warning"
        android:textSize="18sp"
        android:textStyle="bold" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:gravity="start"
        android:padding="4dp"
        android:text="your message"
        android:textSize="16sp"
        android:textStyle="bold" />


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="end"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="end"
            android:layout_margin="8dp"
            android:text="NO"
            android:textColor="#911907"
            android:textSize="18sp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="end"
            android:layout_margin="8dp"
            android:text="YES"
            android:textColor="#00a851
            android:textSize="18sp" />
    </LinearLayout>

</LinearLayout>
0

This is what you're looking for: https://stackoverflow.com/a/10741161/10126669

All you need to do is to put your font in assets and change

SpannableStringBuilder SS = new SpannableStringBuilder("My message here");
SS.setSpan (new CustomTypefaceSpan("", font2), 0, 4,Spanned.SPAN_EXCLUSIVE_INCLUSIVE);

to match the size of your SpannableStringBuilder

SS.setSpan (new CustomTypefaceSpan("", font2), 0, SS.length(),Spanned.SPAN_EXCLUSIVE_INCLUSIVE);

and then add the SpannableStringBuilderto the dialog as

.setMessage(SS)
RipperJugo
  • 85
  • 4