Android cannot get message object in AlertDialog

I am trying to set the linktextcolor of a message in an AlertDialog. But when I try findViewById my app crashes. What am I doing wrong? Do I have to have a message in XML for the activity?

final AlertDialog d =  new AlertDialog.Builder(new ContextThemeWrapper(SplashPage.this, R.style.Theme_Sherlock_Light_Dialog))
            .setIcon(android.R.drawable.ic_dialog_info).setTitle(getString(R.string.termsTitle))
                //.setView(message).setCancelable(false)
                .setMessage(Html.fromHtml(getString(R.string.terms))).setCancelable(false)
                .setPositiveButton(getString(R.string.accept), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        app.setTermsAccepted(true);
                        dialogInterface.dismiss();

                        Intent intent = new Intent(SplashPage.this, LoginPage.class);
                        startActivity(intent);
                    }
                }).create();

        //FAILING: TextView TV = (TextView)d.findViewById(android.R.id.message);
        //TV.setLinkTextColor(Color.MAGENTA);

      

+3


source to share


2 answers


I went through the AlertDialog documentation and found out that when you call its method, it looks for the XML you processed in its onStart method ( http://developer.android.com/reference/android/app/Dialog.html#findViewById%28int% 29 ). Instead, just call the findViewById method of your activity (for example, if it's in the activity class, just call:

TextView TV = (TextView) findViewById(android.R.id.message);

      



must work.)

+4


source


If you are using the DialogFragment, it is available after the DialogFragment.onStart () method, as was accelerated in the previous answer.



@Override
public void onStart() {
super.onStart();
    final TextView textView = (TextView) getDialog().findViewById(android.R.id.message);
    //Do something!
}

      

+2


source







All Articles