6

I tried to change AlertDialog font by used this function

private void saveDialog(){

    AlertDialog.Builder builder = new AlertDialog.Builder(this); 
        builder.setTitle(res.getString(R.string.dialog_title))
               .setMessage(res.getString(R.string.dialog_saveconfirm)) 
               .setCancelable(false)
               .setNegativeButton(res.getString(R.string.dialog_cancel), new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                 }
               })
               .setPositiveButton(res.getString(R.string.dialog_ok), new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    //do some thing
            });
        AlertDialog alert = builder.create();
        alert.show();

        TextView tit = (TextView) alert.findViewById(android.R.id.title);
        TextView msg = (TextView) alert.findViewById(android.R.id.message);
        Button btn2 = alert.getButton(DialogInterface.BUTTON_NEGATIVE);
        Button btn1 = alert.getButton(DialogInterface.BUTTON_POSITIVE);
        tit.setTypeface(UtiliShare.getTf());
        msg.setTypeface(UtiliShare.getTf());
        btn1.setTypeface(UtiliShare.getTf());
        btn2.setTypeface(UtiliShare.getTf());

}

When I call function in Activity I had 02-25 17:59:04.759: E/AndroidRuntime(1014): java.lang.NullPointerException for tit when set typeface but when I remove tit dialog work good.

I think error in TextView tit = (TextView) alert.findViewById(android.R.id.title); it is return null.

How can I solve this??


Update This link contain answer of my question Answer

Thanks to Sam

Community
  • 1
  • 1
AwadKab
  • 3,054
  • 2
  • 17
  • 17
  • I don't believe Android let's you access these TextViews like this, but you should be able to use Styles and Themes to do what you want: [Change the style of AlertDialog](http://stackoverflow.com/q/8011899/1267661) – Sam Feb 25 '13 at 16:00
  • My post here may help you http://stackoverflow.com/questions/4025605/custom-dialog-on-android-how-can-i-center-its-title/13359573#13359573" – SingleWave Games Feb 25 '13 at 16:02
  • 3
    Hmm, this answer claims that you can use `alertTitle` instead of `title`: [Changing font size into an AlertDialog](http://stackoverflow.com/a/9183354/1267661). (I haven't tried this myself.) – Sam Feb 25 '13 at 16:05
  • Thanks Sam, its work for me now. you save my day. – AwadKab Feb 25 '13 at 16:31
  • @Sam. The alertTitle answer worked for getting the View, but still had a problem which I've noted in the answer. – Stephen Hosking Apr 19 '13 at 13:39

2 Answers2

1

In order to alter textviews the way you want, I'd recommend using a custom layout for dialogs. Have a look at

http://developer.android.com/guide/topics/ui/dialogs.html#CustomLayout

luuts
  • 652
  • 4
  • 12
0

If you're using android.support.v7.app.AlertDialog, you have to use android.support.v7.appcompat.R.id.alertTitle otherwise use android.R.id.title

Arturo Mejia
  • 1,862
  • 1
  • 17
  • 25