Defining Custom Views in Android
Explore the process of creating reusable custom views in Android by defining custom attributes, inflating layouts, and managing memory efficiently. This lesson helps you build flexible UI components, like an InfoView with customizable text properties, that can be used across multiple screens with different styles.
Introduction
In our previous lesson, we talked about layouts and views provided by the framework that can be used to render the desired layout. While in most cases we’ll create layouts using a combination of views and view groups to achieve the desired UI, we might come across a use case where we need to create customized views based on our requirements. For this lesson, let’s take an example of a view that accepts two strings as input and renders them as title and subtitle, as shown in the image below.
You might be wondering why we don’t create a layout with two TextView classes to render this kind of UI. That’s what we would usually do, but consider a case where we need something like this on more than one screen with minor customizations. For example, on some screens, we ...