Custom Views as Components

Vedant Agarwala
2 min readMay 5, 2018

Why?

How will you organize your android app’s 10k+ lines of code written by 10+ people? In a couple of months, before you know it, it’ll cross a 100k lines. Wouldn’t it be great if when a new feature requirement comes, you create a new class and start working on it, rather than worrying about what other things might break if you edit this 5k LoC Activity/Presenter class?

Create a new component!

Components let you split the UI into independent, reusable pieces, and think about each piece in isolation.

- react js

A typical example:

without components v/s with components

The goal- isolation

In the example, each of the components (the background map, the chat head, the cards) have very little to no interaction with their sibling components or with the Activity. (The Activity is just a top-level component). This is the secret to writing clean code in UI heavy apps.

Each of the components has 0 to 3 public methods. Yes, some even have zero! (“how?” is a topic for another time). Just add your newly created component to your activity_layout.xml and you’re done. The component doesn’t care where it is placed, and its parent doesn’t care what the component is doing- the perfect relationship.

The lesser the number of public methods, the lesser the WTFs/minute.

(inspired by “Clean Code” by Rober C. Martin)

In conclusion, use components with very few public methods to achieve a low WTFs/minute.

How?

Views are the best components.

Developers don’t realize that, rather than fragments, they can create custom views and use them as components. Views are light-weight and don’t blow up in your face with the infamous IllegalStateException.

Do not even try to read the Fragment + Activity lifecycle diagram on the right

The way I create components is that I create a new class that extends the FrameLayout. In it’s constructor, I inflate a layout.xml and add the inflated view as the only child of this FrameLayout.

A component with 1 public method

Now your custom component class is ready to be added in another activity’s xml, in a recycler view, or even in another component!

So what are you waiting for? You can get started with components right away! No need to install new libraries or teach yourself (and your team) a new programming style.

I’d love to know if you are already using components or, even better, started using components after reading this. Let me know in the comments!

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Vedant Agarwala
Vedant Agarwala

Written by Vedant Agarwala

Sr. Engineering Manager | Startup enthusiast, Public Speaker, Tech Blogger | 1 startup was acqui-hired.

No responses yet

Write a response