I want to use custom font in my android plugin library while using AlertDialog. Now here is the code to display AlertDialog using custom font.
AlertDialog alertDialog = new AlertDialog.Builder(mainActivity).setTitle("Title").setMessage("Message Text").create();
alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE, "Cancel", dialogOnClickListener);
TextView alertMessageTextView = (TextView) alertDialog.findViewById(R.id.message); // I am getting error here
TextView alertTitleTextView = (TextView) alertDialog.findViewById(android.R.id.title); // I am getting error here
Typeface customFont = Typeface.createFromAsset(mainActivity.getResources().getAssets(), "fonts/lobster-regular.ttf");
alertMessageTextView.setTypeface(customFont);
alertTitleTextView.setTypeface(customFont);
alertDialog.show();
I am getting error:
Error Unity AndroidJavaException: java.lang.NoSuchFieldError: No static field message of type I in class Lcom/cptech/pluginexample/R$id; or its superclasses (declaration of 'com.cptech.pluginexample.R$id' appears in base.apk)
I have placed my font file in Unity's "Assets/Resources" Folder. Hence in final apk the font resources appear in "assets/fonts" folder. But however, this doesn't matter as, I can use custom font in other TextView(s).
The only problem is that it cannot find AlertDialog's message and title, as I believe. Any help would be appreciated.
Few changes:
Use show() instead of create() when building the AlertDialog:
AlertDialog alertDialog = new AlertDialog.Builder(mainActivity).setTitle("Title").setMessage("Message Text").show();
Then, replace this line:
TextView messageTextView = (TextView) alertDialog.findViewById(R.id.message);
With android.R.id.message instead, like so:
TextView messageTextView = (TextView) alertDialog.findViewById(android.R.id.message);
And make sure you're not accidentally doing import com.android.R at the top of your file. You need this change because the id message is not an asset in your module, it's a default asset from an android library. So the R you're using right now is coming from your package - you need to specify R from android.R.
Last thing quick, watch out for the variable name of the textview when you set the text, should be changed to messageTextView