How to create a custom View?

A View in Android is a class that represents a UI block. It is the base class for UI widgets such as buttons, text fields, and input boxes. Additionally, View is the parent class of ViewGroup, which is the base for all layouts.

Sometimes, the existing View subclasses are insufficient, and you need to create a custom View. In general, to do this you need to:
  1. Inherit from View or a subclass of View (e.g., Button);
  2. Override the View class constructors. Constructors are used by the system when creating a View described in an XML layout, so it is necessary to override them even if they are not explicitly called;
  3. Create new or override existing event listener methods, such as onTouchEvent();
  4. Override the onDraw() and onMeasure() methods;
  5. Override other on… methods if necessary.

This list of actions is not mandatory but shows what is most often required to create a custom View.