Android Custom Views #0 - Foundations
Mobile
2
 min read
February 23, 2022

Android Custom Views #0 - Foundations

Jan Kandráč
Jan Kandráč
Android Developer
LinkedIn Logo

What is Android Custom View? Android as a platform offers several visual components (referred to as Views), whose task is to draw the pixels that represent this View on the screen correctly and in the right place.

A "good" View looks good, is easy to style, takes into account the limitations / recommendations of the framework and provides a simple interface for the programmer. This series is supposed to help us gradually immerse ourselves in the secrets of such Views, which are beyond the scope of the platform. Android as such does not directly provide them. We call this Views Custom Views.

As examples, we can imagine various graphs, equalizers or navigation and auxiliary elements such as page indicators and the like.

This series is intended for programmers of native Android applications (Java / Kotlin), who master at least the broader basics of Android and they want how to know write they own elements. Jetpack Compose will not be part of this series and will be mentioned only marginally.

Mind map - terminology

The Mind Map Diagram - Blue Peaks will be the subject of this series

We only need to create a Custom View if none of the available Views is sufficient for our needs. So:

  • can't look the way we need
  • it can't provide the functionality we need
  • forces us to write repetitive code

To create custom views, we can follow such a mind map. So if:

  1. I can create a view using existing Views and it has no potential to be used more than once - there is no need to use a custom view. If it is too large we can use the structure <include /> or <merge>. We will not address this alternative in this series.
  2. I want to extend the functionality of the existing View. So ... will I ... expand it? We will not address this alternative in this series because it concerns inheritance rather than Custom Views.
  3. I can create a view using multiple existing Views and it has the potential to be used more than once - Compound ViewGroup. The correct procedure is surprisingly described in this StackOverflow post (the highest rated post and the post marked as correct are not correct)
  4. View cannot be created using existing Views and can contain childViews - Custom ViewGroup. I haven't found a good article online yet.
  5. View cannot be created using existing Views and must not contain childViews - Custom View. A good start can be found right on the Android Developers website.

We can also consider working with ViewBinding and Kotlin as a special case, when extension functions can often suffice. We will also be very interested in JetPack Compose in the very near future.

Custom Views features

To give our Custom View enough power and possibly share it with the world, it should also provide the following features:

  1. It should be configurable using XML attributes (example XML attribute: android: text = "...")
  2. It should expose the right number of methods.
  3. It should handle configuration changes, such as screen rotation restores, etc.
  4. It should be styled using XML styles files In the case of ViewGroup, it should distribute click events correctly.
  5. It should not depend on the application logic (so that we can copy it straight into a new project), etc.
  6. etc.

The list can be huge and some of the points mentioned are not necessary. We simply want to create a better quality and more widely usable View.

Next steps

Gradually, we will go through all the mentioned alternatives. Let's see how to create Compound ViewGroup, Custom ViewGroup and Custom View. We will stick to Best-Practices and try to answer the most frequent questions associated with creating these components. Stay Tuned and Buckle Up !!!

Like what you see?
Join our newsletter.

Great! Welcome to newsletter.
Oops! Something went wrong while submitting your email.
High quality content once a month. No spam, we promise.
Your personal data is processed in accordance with our Memorandum on Personal Data Protection.

Páči sa vám náš content?
Odoberajte newsletter.

Great! Welcome to newsletter.
Oops! Something went wrong while submitting your email.
Vaše osobné údaje sú spracované v súlade s našim Memorandom na ochranu osobných údajov.