Creating Custom Views Using OOP in Android

Creating Custom Views Using OOP in Android

Android applications are rich with a variety of user interface components, but sometimes the standard views provided by the framework are not sufficient to meet specific design requirements. In such cases, creating custom views becomes necessary. Leveraging Object-Oriented Programming (OOP) principles allows you to build custom views that are not only flexible and reusable but also maintainable. In this blog, we will explore how to create custom views in Android using OOP principles, with practical examples to help you get started.

Why Create Custom Views?

Custom views are necessary when you need to:

  • Achieve Unique Designs: Implement unique UI elements that are not available through standard views.

  • Encapsulate Complex Behavior: Create views that encapsulate complex functionality or behaviors.

  • Enhance Reusability: Develop reusable components that can be used across different parts of your application or in other projects.

  • Optimize Performance: Combine multiple views into a single custom view to reduce the view hierarchy and improve performance.

Understanding OOP Principles in Custom View Development

Before diving into the process of creating custom views, it's essential to understand how OOP principles apply to this task:

  • Encapsulation: Encapsulate the behavior and attributes of the custom view, providing a clear interface and hiding internal implementation details.

  • Inheritance: Extend existing Android view classes to create new custom views, inheriting useful properties and behaviors.

  • Polymorphism: Design custom views to be flexible and adaptable, allowing them to be used interchangeably with standard views.

  • Composition: Build custom views by composing existing views, combining their functionality into a single, cohesive component.

Steps to Create Custom Views in Android

1. Define the Custom View Class

The first step in creating a custom view is to define a new class that extends one of the existing view classes, such as View, TextView, ImageView, or any other view class that closely matches the desired functionality of your custom view.

Example: Creating a Custom Circle View

javaCopy codeimport android.content.Context;
import android.util.AttributeSet;
import android.view.View;

public class CircleView extends View {
    private Paint paint;
    private int radius;

    // Constructor
    public CircleView(Context context) {

    // Constructor with attributes
    public CircleView(Context context, AttributeSet attrs) {
        super(context, attrs);

    // Initialize the view
    private void init() {
        paint = new Paint();
        paint.setColor(0xFFFF0000); // Red color
        radius = 100; // Default radius

    protected void onDraw(Canvas canvas) {
        // Draw the circle
        int x = getWidth() / 2;
        int y = getHeight() / 2;
        canvas.drawCircle(x, y, radius, paint);

    // Setter for radius
    public void setRadius(int radius) {
        this.radius = radius;
        invalidate(); // Redraw the view

    // Setter for color
    public void setColor(int color) {
        invalidate(); // Redraw the view

In this example:

  • The CircleView class extends the View class.

  • The init method initializes the view's properties, such as the paint color and radius.

  • The onDraw method overrides the View class's method to draw a circle on the canvas.

2. Handle Custom Attributes

To make your custom view more flexible, you can define custom attributes that can be set through XML. This allows you to customize the appearance and behavior of the view without modifying the code.

Example: Adding Custom Attributes

  1. Define attributes in res/values/attrs.xml:

     xmlCopy code<resources>
         <declare-styleable name="CircleView">
             <attr name="circleColor" format="color" />
             <attr name="circleRadius" format="dimension" />
  2. Parse attributes in the custom view:

     public CircleView(Context context, AttributeSet attrs) {
         super(context, attrs);
         // Parse custom attributes
         TypedArray a = context.getTheme().obtainStyledAttributes(
                 0, 0);
         try {
             int color = a.getColor(R.styleable.CircleView_circleColor, 0xFFFF0000);
             float radius = a.getDimension(R.styleable.CircleView_circleRadius, 100);
             setRadius((int) radius);
         } finally {

3. Handle User Interaction

Custom views often need to handle user interactions, such as touch events. You can override the onTouchEvent method to respond to these events.

Example: Handling Touch Events

public boolean onTouchEvent(MotionEvent event) {
    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            // Handle touch down event
            return true;
        case MotionEvent.ACTION_MOVE:
            // Handle touch move event
            return true;
        case MotionEvent.ACTION_UP:
            // Handle touch up event
            return true;
            return super.onTouchEvent(event);

4. Optimize Layout and Measurement

For custom views, it's crucial to handle layout and measurement correctly to ensure they display properly in different contexts. Override the onMeasure method to define how the view should be measured.

Example: Overriding onMeasure

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int desiredSize = radius * 2 + getPaddingLeft() + getPaddingRight();
    int width = resolveSize(desiredSize, widthMeasureSpec);
    int height = resolveSize(desiredSize, heightMeasureSpec);

    // Set the measured dimensions
    setMeasuredDimension(width, height);

Using Custom Views in Layouts

Once you've created your custom view, you can use it in your layouts just like any other view. You can include it in your XML layout files or add it programmatically in your code.

Example: Using Custom View in XML

    app:circleRadius="150dp" />

Example: Using Custom View in Code

CircleView circleView = new CircleView(this);

LinearLayout layout = findViewById(;

Best Practices for Creating Custom Views

  1. Encapsulate View Logic: Encapsulate the behavior and attributes of the custom view, providing a clear and well-defined interface for interacting with it.

  2. Use Composition Wisely: Where appropriate, compose your custom view from existing views to leverage their functionality and reduce the amount of custom code you need to write.

  3. Optimize for Performance: Avoid unnecessary drawing operations and optimize the layout and measurement of your custom views to ensure they perform well.

  4. Handle Configuration Changes: Ensure that your custom views handle configuration changes, such as screen rotations, gracefully by saving and restoring their state as needed.

  5. Test Extensively: Test your custom views in different contexts and on various devices to ensure they behave as expected and provide a consistent user experience.


Creating custom views in Android allows you to design unique user interface components that are flexible, reusable, and maintainable. By leveraging Object-Oriented Programming principles, you can build custom views that encapsulate behavior, extend existing views, and provide a clear and consistent interface for interacting with them. Whether you’re developing complex UI elements or optimizing performance, custom views offer a powerful way to enhance your Android applications and deliver a superior user experience.