9. Feb 2022
AndroidAndroid Custom Views #0 - Foundations
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
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:
- 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.
- 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.
- 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)
- View cannot be created using existing Views and can contain childViews - Custom ViewGroup. I haven't found a good article online yet.
- 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:
- It should be configurable using XML attributes (example XML attribute: android: text = "...")
- It should expose the right number of methods.
- It should handle configuration changes, such as screen rotation restores, etc.
- It should be styled using XML styles files In the case of ViewGroup, it should distribute click events correctly.
- It should not depend on the application logic (so that we can copy it straight into a new project), etc.
- 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 !!!