Setting Up an Alert Dialog Builder with a User View

Let's combine steps 2 through 4 from our instructions to create a prompt dialog: loading the XML view and setting it up in the alert-dialog builder. Android provides a class called android. view.Layoutlnflater to create a View object from an XML layout definition file. We will use an instance of the Layoutlnflater to populate the view for our dialog based on the XML layout file (see Listing 5-23).

Listing 5-23. Inflating a Layout into a Dialog

Layoutlnflater li = Layoutlnflater.from(ctx); View view = li.inflate(R.layout.promptdialog, null);

//get a builder and set the view

AlertDialog.Builder builder = new AlertDialog.Builder(ctx);

builder.setTitle("Prompt");

builder.setView(view);

In Listing 5-23, we get the Layoutlnflater using the static method Layoutlnflater. from(ctx) and then use the Layoutlnflater object to inflate the XML to create a View object. We then configure an alert-dialog builder with a title and the view that we just created.

0 0

Post a comment