Junior — Middle
Can you explain what is meant by a custom view in Android?
sobes.tech AI
Answer from AI
In Android, a custom view is a user interface component created by the developer that extends standard widgets or fully implements its own UI element. This is needed when standard elements are insufficient for implementing the desired design or behavior.
A custom view is created by inheriting from the View class or its subclasses (e.g., TextView, ImageView) and overriding methods such as onDraw() for drawing and onMeasure() for measuring dimensions.
An example of a simple custom view that draws a circle:
public class CircleView extends View {
private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
public CircleView(Context context) {
super(context);
paint.setColor(Color.RED);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int radius = Math.min(getWidth(), getHeight()) / 2;
canvas.drawCircle(getWidth() / 2, getHeight() / 2, radius, paint);
}
}
Thus, a custom view allows creating unique interface elements with the desired behavior and appearance.