Bob Ueland
Bob Ueland

Reputation: 1834

What is this dot notation in android?

Could someone explain the dot notation used below. Is that a single or several statements or shorthand for something else?

AlertDialog.Builder builder = new AlertDialog.Builder(activity);
        builder.setTitle("ALERTTILESTRING")
        .setMessage("alertNameString")
        .setCancelable(false)
        .setNegativeButton("Close",new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                dialog.cancel();
            }
        });
        AlertDialog alert = builder.create();
        alert.show();
    }

Upvotes: 0

Views: 275

Answers (2)

Sunny Onesotrue
Sunny Onesotrue

Reputation: 152

It's just a shorthand for all the methods called on the AlertBuilder object builder. It's the same as:

AlertDialog.Builder builder = new AlertDialog.Builder(activity);
builder.setTitle("ALERTTILESTRING");
builder.setMessage("alertNameString");
builder.setCancelable(false);
builder.setNegativeButton("Close",new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int id) {
        dialog.cancel();
    }
});

AlertDialog alert = builder.create();
alert.show();

Upvotes: 4

Doug Stevenson
Doug Stevenson

Reputation: 317692

That's the Builder Pattern implemented in Java. All that's happening here is that the builder object of type AlertDialog.Builder is being returned from each method called on it, so you can chain the calls together in sequence. In your example, it's no different if you just called the same methods repeatedly on the builder instance. But that would be more typing.

Upvotes: 4

Related Questions