Question

Perhaps I'm missing something obvious here, but I'm having a hard time setting a custom view for the body of an AlertDialog. Here's what I'm doing to set the custom view:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.setContentView(View.inflate(getContext(), R.layout.dialog_body, null));
}

Instead of setting the content of the body of the AlertDialog, the view is placed over the entire screen. How do I get the view just to replace the AlertDialog message body?

Was it helpful?

Solution

You are setting the activity's view by calling setContentView which is why it takes up the entire screen. You are also doing it in the onCreate method of what I assume is the activity, you need to do it in the onCreateDialog method.

Heres a link to the docs and an example.

public Dialog onCreateDialog(int id) {

            Dialog dialog = null;
            AlertDialog.Builder builder = new AlertDialog.Builder(app);
            AlertDialog alert = null;

            builder.setTitle("A title") 
                   .setCancelable(true)
                   .setView(myView);
                alert = builder.create();
                return alert;

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top