Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set custom font for alert dialog in android?

In my android application an alert dialog appears after clicking on a button. I want to set custom font for the alert. I searched the web and found some tutorials and questions about this subject, but none of them works for me.

How can I change the font?

Thanks

like image 721
IndieBoy Avatar asked Oct 24 '12 15:10

IndieBoy


2 Answers

To do this you use alert builder to build your alert. You then get the TextView from this alert and then you set the typeface for the alert.

AlertDialog dialog = new AlertDialog.Builder(this).setMessage("Hello world").show(); TextView textView = (TextView) dialog.findViewById(android.R.id.message); Typeface face=Typeface.createFromAsset(getAssets(),"fonts/FONT");  textView.setTypeface(face);  
like image 51
mikeswright49 Avatar answered Oct 04 '22 23:10

mikeswright49


The above answers didnt work for me.

I used the following approach

// Initializing the alertDialog AlertDialog alertDialog = new AlertDialog.Builder(QuizActivity.this).create(); alertDialog.setTitle("Warning"); alertDialog.setMessage("Are you sure you want to exit?"); alertDialog.show(); // This should be called before looking up for elements   // Getting the view elements TextView textView = (TextView) alertDialog.getWindow().findViewById(android.R.id.message); TextView alertTitle = (TextView) alertDialog.getWindow().findViewById(R.id.alertTitle); Button button1 = (Button) alertDialog.getWindow().findViewById(android.R.id.button1); Button button2 = (Button) alertDialog.getWindow().findViewById(android.R.id.button2);  // Setting font textView.setTypeface(FontHelper.getFont(Fonts.MULI_REGULAR)); alertTitle.setTypeface(FontHelper.getFont(Fonts.MULI_REGULAR)); button1.setTypeface(FontHelper.getFont(Fonts.MULI_BOLD)); button2.setTypeface(FontHelper.getFont(Fonts.MULI_BOLD)); 

Tested on 7.1.1

NOTE: Make sure you get the element after showing the dialog. Without this you will get NullPointerException

like image 41
Cerlin Avatar answered Oct 05 '22 00:10

Cerlin