O'Reilly logo

Building Android UIs with Custom Views by Raimon Ràfols Montané

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Builder pattern

In the previous example, we used the setFillColor() method to set the fill color of the custom view, but suppose we will have many other parameters, the code might get a bit messy with all the setters.

Let's create a simple example: instead of having one single background color, we'll have four different colors and we'll draw a gradient on our view:

Let's start by defining the four different colors and their setters as follows:

private int topLeftColor = DEFAULT_FILL_COLOR; 
private int bottomLeftColor = DEFAULT_FILL_COLOR; 
private int topRightColor = DEFAULT_FILL_COLOR; 
private int bottomRightColor = DEFAULT_FILL_COLOR; 
private boolean needsUpdate = false;public void setTopLeftColor(int topLeftColor) { this.topLeftColor = ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required