3

I must change default font of alertDialog.setMessage and alertDialog.setTitle but I can't use a custom layout, so I have to change it by code in Kotlin. I tried using typeface, but he doesn't detect the font in res/font/product_sans_bold.ttf

I also used

content.text = Html.fromHtml(resources.getString(R.string.centripetaFormule))
content.typeface = ResourcesCompat.getFont(applicationContext, R.font.product_sans_bold)
alertDialog.setView(content)

and it works, but not as i want.

I have a string

<string name ="centripetaFormule>![CDATA[<h5>(...)</h5><p>(...)</p> 

where h5 is bold, so using variable content there will be no differences between h5 and "normal" text, except for the size.

So, I want to change font just for normal text, I can't use a custom layout, I have to use strings with CDATA.

How to do that?

Hope I explained it well

forpas
  • 160,666
  • 10
  • 38
  • 76
Arfmann
  • 353
  • 1
  • 7
  • 18
  • Possible duplicate of [How to set custom font for alert dialog in android?](https://stackoverflow.com/questions/13051956/how-to-set-custom-font-for-alert-dialog-in-android) or [Multiple TypeFace in single TextView](https://stackoverflow.com/q/10675070/6334037) – Jitesh Prajapati Jan 25 '19 at 09:31

2 Answers2

0

Android comes with a nice default font but in some cases you might want to use a different font.

Fastest Way to use custom font android

Put your font file in asset folder

enter image description here

Create Typeface Object

 Typeface customFont = Typeface.createFromAsset(getAssets(), "fonts/Roboto-Bold.ttf")

Set Typeface to TextView/EditText/Button etc…

TextView textView = (TextView) findViewById(R.id.my_textview);
textView.setTypeface(customFont);
Jose Ribamar
  • 341
  • 2
  • 5
  • Thanks for the answer, but the problem is that i have an Header and a "normal" text and they must have different fonts, so this method doesn't works well – Arfmann Nov 07 '18 at 13:44
0

I get around the problem like this :

Customize the view

dialog_custom_nunito.xml :

 <?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="match_parent"
              android:orientation="vertical">

    <TextView android:id="@+id/custom_dialog_tv" android:layout_width="wrap_content"
              android:layout_margin="12sp"
              android:textSize="18sp"
              android:layout_height="wrap_content" android:text="@string/warning_recherche_no_found"/>

</LinearLayout>  

Kotlin Code :

val builder = AlertDialog.Builder(this)
builder.setView(R.layout.dialog_custom_nunito)
builder.setNegativeButton("OK") { dialogInterface, i -> }
                    builder.show()

I hope it will help other people,

NonowPoney
  • 982
  • 4
  • 14
  • 30