How to customize a font in AlertDialog using the support library 26

I am using version 26.0.1 of the Android Support Library to install custom fonts in my application. In my application thread, I added:

<item name="android:fontFamily">@font/my_font</item> 

It worked like a charm, converting text in my application into my own font. EXCEPT for my dialogs - in particular their headers, messages, and also their NumberPickers . Fonts were not updated in these places. (Radio buttons and check boxes worked, as well as yes / no buttons)

Is there something that I forget to add to my themes or styles? Or is it not yet supported by the support library?

A little more detailed: I use AppCompatDialogFragment to implement all of my dialogs. In my onCreateDialog() methods, I create a dialog using AlertDialog.Builder , and then return it.

Thanks for the help!

+13
source share
6 answers

Thanks to everyone who answered, but, unfortunately, none of these solutions worked for me. Hope they will work for someone else.

I came to the conclusion that this is a bug in the support library, and I hope Google fixes it. In the meantime, I developed this hacker way:

 public static void applyCustomFontToDialog(Context context, Dialog dialog) { Typeface font = ResourcesCompat.getFont(context, R.font.my_font); if (font != null) { TextView titleView = dialog.findViewById(android.support.v7.appcompat.R.id.alertTitle); TextView messageView = dialog.findViewById(android.R.id.message); if (titleView != null) titleView.setTypeface(font, Typeface.BOLD); if (messageView != null) messageView.setTypeface(font); } } 

This works by scanning the dialog box tree for the headers and message views by identifiers provided by the support library. If the support library was supposed to change these identifiers, this will no longer work (and that is why it is cracking). I hope Google fixes this problem and I wonโ€™t have to do this anymore.

+5
source

I found a way that only requires a one-line change to the Java code every time you create an AlertDialog.

Step 1

Create a custom, reusable layout containing a TextView with the correct font set. Name it alert_dialog.xml:

 <?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" style="@style/SomeStyleWithDesiredFont" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="@dimen/spacing_2x" /> 

Step 2

Create a reusable helper function somewhere that will inflate this layout and set the text to the desired line

 public static TextView createMessageView(String message, Context context) { TextView messageView = (TextView) LayoutInflater.from(context).inflate(R.layout.alert_dialog, null, false); messageView.setText(message); return messageView; } 

Step 3

In each AlertDialog.Builder chain in your code, replace this line:

 .setMessage(messageString) 

with this line:

 .setView(createMessageView(messageString, context)) 

(Note that the same approach should work for a TextView header. You can use a custom view for the header by calling setCustomTitle () in your builder)

+2
source

When creating a dialog builder, you should use ContextThemeWrapper . Like this

 ContextThemeWrapper wrappedContext = new ContextThemeWrapper(context, R.style.mystyle); AlertDialog.Builder builder = new AlertDialog.Builder(wrappedContext); 

If you only support SDK 11 and above, you can use

 ContextThemeWrapper wrappedContext = new ContextThemeWrapper(context, R.style.mystyle); AlertDialog.Builder builder = new AlertDialog.Builder(wrappedContext, R.style.mystyle); 
+1
source

Perhaps this is not the case, I had a similar problem, and I found that fontFamily would not be affected by AsyncLayoutInflater . This also occurs if the AlertDialog is nested in AsyncLayoutInflater . I had to convert to a regular inflator to show my own font. For instance,

It did not show fontFamily called from TextView XML.

 AsyncLayoutInflater inflater =new AsyncLayoutInflater(activity); inflater.inflate(R.layout.dialog, null, new AsyncLayoutInflater.OnInflateFinishedListener() { @Override public void onInflateFinished(@NonNull View view, int resid, ViewGroup parent) { final TextView tv = view.findViewById(R.id.textview); tv.setText("Text with custom fontFamily called in XML, but won't work"); } }); 

It showed that fontFamily from TextView XML.

 final ViewGroup nullParent = null; final View view = activity.getLayoutInflater().inflate(R.layout.dialog, nullParent); final TextView tv= view.findViewById(R.id.textview); tv.setText("Text with custom fontFamily called in XML, and will work"); 
0
source

You can use a custom Alert Dialog and set the font using Typeface . See the code snippet below.

  AlertDialog dg = new AlertDialog.Builder(this).setMessage("Your Message").show(); TextView tv = (TextView) dg.findViewById(android.R.id.message); // Your TextView of custom Alert Dialog Typeface fc=Typeface.createFromAsset(getAssets(),"fonts/FONT"); // This is your font file name. tv.setTypeface(fc); 
-1
source

you can inflate a custom layout in your dialog as follows:

 final android.support.v7.app.AlertDialog.Builder alertDialogBuilder = new android.support.v7.app.AlertDialog.Builder(context, R.style.LimitAlertDialogStyle); LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); View alertLayout = layoutInflater.inflate(R.layout.date_layout, null); TextView tv= (TextView) alertLayout.findViewById(R.id.tv); Typeface fc=Typeface.createFromAsset(getAssets(),"fonts/FONT"); tv.setTypeface(fc); 
-1
source

Source: https://habr.com/ru/post/1270947/


All Articles