Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android can't get message object on AlertDialog

Tags:

java

android

I'm trying to set the linktextcolor of the message in AlertDialog. But when I try to findViewById my app crashes. What am I doing wrong? Do I need to have message in the 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);
like image 606
williamsandonz Avatar asked Feb 12 '13 01:02

williamsandonz


2 Answers

I looked at the AlertDialog documentation and it appears that when you call its method, it searches an XML that you've processed in its onStart method (http://developer.android.com/reference/android/app/Dialog.html#findViewById%28int%29). Instead, simply call your activity's findViewById method (e.g. if this is in an activity class, just calling:

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

should work.)

like image 145
Halogen Avatar answered Sep 27 '22 18:09

Halogen


If you are using a DialogFragment, it is accessible after DialogFragment.onStart() method as eluded to in the prior answer.

@Override
public void onStart() {
super.onStart();
    final TextView textView = (TextView) getDialog().findViewById(android.R.id.message);
    //Do something!
}
like image 45
Dustin Avatar answered Sep 27 '22 16:09

Dustin