Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set custom font (TypeFace) in dialog in Android

I'd like to change the font in textview which is in dialog:

dialog = new Dialog(MyActivity.this);
dialog.setContentView(R.layout.my_dialog);
dialog.setCancelable(true);
((TextView)findViewById(R.id.dialog_box_title_text)).setTypeface(Typeface.createFromAsset(getAssets(), "font.ttf"));

But every time I get the runtime exception:

E/AndroidRuntime(4475): java.lang.IllegalStateException: Could not execute method of the activity

Do you have any idea what is wrong? Because normally it works fine. Problem is only when try to change the font in dialog.

like image 831
obrien Avatar asked Nov 21 '11 11:11

obrien


2 Answers

Try this, and let me know what happen.

((TextView)dialog.findViewById(R.id.dialog_box_title_text)).setTypeface(Typeface.createFromAsset(getAssets(), "font.ttf"));
like image 118
user370305 Avatar answered Oct 16 '22 12:10

user370305


For a DialogFragment this will work (I'm targeting SDK 19, and I have minSDK 14) as long as you put the font file in your assets() folder.

So if you are running ice cream sandwich (ICS) and later try this :

@Override public void onActivityCreated(Bundle savedInstanceState)
{
    // Call to the Super Class, performing the default behavior
    super.onActivityCreated(savedInstanceState);
    // Change the Dialog Title Text Typeface
    ((TextView)getDialog().findViewById(android.R.id.title)).setTypeface(
        Typeface.createFromAsset(getActivity().getAssets(),"Roboto-Thin.ttf"));
}
like image 2
kandroidj Avatar answered Oct 16 '22 13:10

kandroidj