0

I created a alertDialog in olders api is working but on API 23 my applications is crashed here:

int alertTitleId = resources.getIdentifier("title", "id", "android");
TextView alertTitle = (TextView) dialog.getWindow().findViewById(alertTitleId);
alertTitle.setTypeface(alertTitle.getTypeface(), Typeface.BOLD); //BOLD

In logs I see that alertTitle in null how I can fix this?

Cœur
  • 37,241
  • 25
  • 195
  • 267
jpok
  • 143
  • 2
  • 2
  • 11
  • 1
    `getIdentifier` is risky cause over the time libraries changes. why don't you Create a custom dialog? . Its just few lines of code . If you want to stick to the default one you can try [CustomTypefaceSpan](https://stackoverflow.com/questions/6612316/how-set-spannable-object-font-with-custom-font/10741161) or Simply `TypefaceSpan`. – ADM Feb 15 '19 at 10:12
  • You said that you have created custom dialog. So I guess layout is custom as well so package will not be android. Instead of `resources.getIdentifier("title","id","android")` try `resources.getIdentifier("title","id",{yourpackageName})`. [This](https://stackoverflow.com/questions/15488238/using-android-getidentifier) should help – Shadow Droid Feb 15 '19 at 10:19
  • @ShadowDroid it does not work – jpok Feb 15 '19 at 10:37

1 Answers1

0

A simple way to change the title is to use the API available. Use .setTitle()

Using the default identifier is risky since the Android code could change over time. It's better to do it in the provided way.

 new AlertDialog.Builder(mContext)
                        .setTitle("THIS IS THE TITLE")
                        .setMessage("Hello World")
                        .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                CaptureFragment.this.initView();
                            }
                        })
                        .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                dialogInterface.dismiss();
                            }
                        })
                        .create().show();
d4c0d312
  • 748
  • 8
  • 24