Writing code for custom views can be tricky business. But making the decision to write custom views or go with default ones is a bigger dilemma.

Occasionally, when required to build a complex layout, we get to a point where we find ourselves at a clear crossroads. We are faced with two options, and choosing one is never easy.

One option is lucrative - easier to code, and quite possibly faster. This route requires using a bunch of default android views.

The other route looks more complex - requiring more coding prowess, taking up more time and seeming more difficult. This is the route of writing your own, custom views.

As you stand at this decision junction, the first route might seem like the logical choice, after all, time is an extremely valuable resource, and if you can do something in a simple manner, why wouldn’t you? Moreover, a lot of confusion and fear revolves around building custom views.

I’m going to discuss the pros and cons of each option here, in the hope that it will help you make a more informed and logical decision on which path to take.

Let’s handle the two most important questions here — Why and Why not?

Why should you use custom views? and Why should you not use custom views?

Why should you use custom views?

Or as I like to call it, the pros of writing custom views.

1. Code Readability

So let’s take an example scenario, one that I was actually faced with.

I needed to develop a Bar, which would be segmented into sections, and some of those sections would be activated while others remained gray, based on the user progress.

ProgressView

One way to do this was to create multiple Views during Run Time. The code might look something like this:

Using Default Views

And the other option:

Using Custom Views

The second option here looks much cleaner and easier to understand. The major factor to understand here is that the code to generate your views does not belong to your activity. Segregating it makes your code more manageable.

This was a simple example, but with more complex layouts, the code can become very complex and messy, if you do not use custom views.

A counter-argument here can be that I’ve not mentioned the coding that goes into creating the ProgressView class, which will be technically challenging and not easy to achieve. Well, the idea here is not to bypass challenges, but to make the Activity code cleaner, easier to understand and more maintainable.

2. Reusability

When developing large scale apps, code reusability is extremely common. Similar UI components will be used in multiple places very frequently, to maintain a standard user experience across the app. With this in mind, building custom views might eliminate code redundancy in a lot of places.

Once you create a particular custom view, it can be easily used in multiple locations across the app, and also across multiple projects.

Taking the above example of ProgressView, we can now create multiple instances of ProgressView in our app, without needing to write the code to generate multiple views every time. So even though creating a custom view takes more time once, as a whole it might reduce the amount of effort and time needed to develop the application.

3. Improved Performance

This might sound counter-intuitive at first, but building custom views can be an effective way to improve your app’s performance. If you’ve written applications with complicated RecyclerViews, then you must have come across the problem where when you scroll on the RecyclerView, you notice a prominent jitter.

How do RecyclerViews work? As you scroll, the app draws each row of the RecyclerView as they come into focus. So if a particular row has a lot of views, then that many iterations need to be made to draw the entire row. The more complicated the layout, the more time is required to do the drawing. And this is what results in the laggy behaviour.

For such scenarios custom views can come as a great rescue for developers. If you can flatter your hierarchy with a single custom view, you can reduce the drawing process to a single iteration. This can greatly reduce the app’s performance, especially in cases of RecyclerViews or other such lists.