Sobes.tech
Junior — Middle

What base classes need to be extended to create your own custom view component?

sobes.tech AI

Answer from AI

To create a custom view component in Android, you typically extend one of the base classes:

  • View — if you need a fully custom element from scratch.
  • ViewGroup — if the component should contain other Views (for example, a custom container).
  • Existing widgets (such as Button, TextView) — if you need to extend or modify the behavior of a standard element.

Most often, for simple custom components, you extend the View class and override methods such as onDraw() for drawing and onMeasure() for measuring dimensions.

Example of a simple custom View:

public class MyCustomView extends View {
    public MyCustomView(Context context) {
        super(context);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        // Draw something
        canvas.drawCircle(50, 50, 20, new Paint());
    }
}

Thus, the choice of the base class depends on the functionality you want to implement.