Skip to content Skip to sidebar Skip to footer

Android Set Text In Alert Dialog

I'm trying to create an alert dialog with an image header across the top, 2 buttons at the bottom and I want to set the text in the middle from code. However, I can't get the set

Solution 1:

Don't set the message with the way you do it.

In your custom layout the textview you have to set is the "dialogtext" TextView. Try this...see that i get the custom view that i inflate and from that view i get the custom dialog (which you didn't actualy set before) and set the message to show after the build has taken place. Actually you can have whatever custom view you want and set each element of the view as you prefer or even handle events on it

AlertDialog.Builderbuilder=newAlertDialog.Builder(this);
LayoutInflaterinflater= getLayoutInflater();

ViewcustomView= inflater.inflate(R.layout.custom_dialog, null);
TextViewmessageView= (TextView)customView.findViewById(R.id.dialogtext);


builder.setView(customView)
        .setPositiveButton("Share", newDialogInterface.OnClickListener() {
           publicvoidonClick(DialogInterface dialog, int id) {

               if (isNetworkAvailable()){

                    if (......;

                    } else {
                        ......
                    }

               } else {
                   .......;
               }    
           }  
       })
       .setNegativeButton("Shake again!", newDialogInterface.OnClickListener() {
           publicvoidonClick(DialogInterface dialog, int id) {
               // User cancelled the dialog

           }

    }).setOnCancelListener(newOnCancelListener() {

        @OverridepublicvoidonCancel(DialogInterface dialog) {

        }
    });

messageView.SetText("Message" + message);



AlertDialogalert= builder.create();

alert.setOnDismissListener(newOnDismissListener() {

    @OverridepublicvoidonDismiss(DialogInterface dialog) {


    }
});
alert.show();

Post a Comment for "Android Set Text In Alert Dialog"