How to make an android custom view.

Engine Bai
5 min readNov 30, 2016

--

Today I’m going to create an android custom view to implement circular SeekBar like the mockup below, make it as a library, open source at GitHub, and publish it to JCenter. In this post, I will cover several fundamental concepts of android custom view, write a helpful README on GitHub, and how to publish your library.

(It would be nice to give my project a star, thank you.)

Before you start…

Try to understand the requirement or design specifications, take a look at this widget first. Here is some requirements:

  1. The user can touch the indicator icon through this arc to set the current progress level.
  2. The center text display the current progress in integer.
  3. The indicator icon will stop dragging when exceeding max or min progress.

It works like an extension of circular SeekBar with customized appearances and motion event.

Before we start to implement the custom view, we have to understand the fundamental.

Get Started:

View Lifecycle

All the android view widgets are based on View, to implement custom view, you will start a subclass of View and override several lifecycle callback methods of view, so you need to understand the view lifecycle at first. The following diagram shows some important methods to override:

Create view

To get started, the first thing we have to do is to create a class that extends View, and provide two default constructors which allows us to create the view programmatically (1st constructor) or in XML layout (2nd constructor). The SwagPoint(Context context, AttributeSet attrs) constructor is more important here because it is used when Android inflates the view from XML layout file, otherwise you will get exception.

Then, there are several things we have to control or modify in our custom view:

  1. Attributes: What things are customizable in your view? Determine custom attributes that allow developer to change its appearance and behavior in XML layout file according to their design.
  2. Size: Determine the dimensions of the view and every components in this custom view on the screen.
  3. Drawing: Determine how the view and components to render on the screen which contains the shape, location, appearance.
  4. Touch: Determine the way the user can interact with the view by touching.

Customization

1. Attributes:

Here we provide several customizable attributes for developer: the initial progress points, the range of progress max/min, the interval when user change the progress step, the color and size of progress/arc/text. To define custom attributes, we create res/values/attrs.xml file and define custom attributes for your view in a <declare-styleable> resource element as below.

After adding res/values/attrs.xml file, we use TypedArray to retrieve attribute value in Java class and define instance variables (the following variables with m as prefix name) to store. Here we add a init() method to put inside the constructor:

2. Size:

In order to control the view dimension, we have to override View.onMeasure()method and calculate the size of each components. Here we have to define the arc radius according to the width/height of our view.

3. Drawings:

Give you a pen and a paper, you can draw what you want. To draw the view, you have to override the onDraw(Canvas canvas) method.

Before that, we have to know what to draw and how to draw. Android provides two classes to do this job:

  • What to draw, handled by Canvas which is provided as parameter in onDraw() method.
  • How to draw, handled by Paint.

Before you use Canvas to draw anything, it’s necessary to createPaintobjects. For performance optimization, creating the Paint objects ahead is quite important, because the onDraw() method is called when redrawn is needed at anytime, we won’t create the Paint objects inside the onDraw()method, we should avoid any allocation in onDraw() method. Here we define these objects (one for arc, another for progress and the other for text) as instance variables and initialize at init() method:

Once we have Paint objects defined, we can start to implement the onDraw(Canvas canvas) method, here we’re going to draw the text to display current progress number, the arc and current progress:

After overriding onDraw() method, there is one more important method about drawing to introduce: invalidate(), this method is used when any redrawing is needed, we won’t call onDraw() directly, we just call this method instead, you can use this method anywhere inside your custom view, however, for performance optimization, make sure it’s called as infrequently as possible.

4. Touching:

When the user touch the screen, Android calls the onTouchEvent() method, so we override View.onTouchEvent() to handle the user input gestures:

There are several things we have to control when user touches the indicator or other place of view:

  • Update the indicator icon position and progress text.
  • Draw the current progress on the arc.
  • Stop when reaching max or min.

To know the indicator icon position and the current progress to draw, we have to convert the touch coordinate on the screen to the angle of arc.

In our custom view, we consider the center of arc as origin (0, 0), and use trigonometric functions to transform the touch coordinate into the angle of the arc in degree (0, 360), and map to the current progress value of given range (min, max).

Here we add a method to convert from touch coordinate to the arc angle:

Final Demo

Happy Coding!!

It would be nice to star my project, thank you!!

--

--

Engine Bai

白昌永 (大白) Software Engineer, Athlete, Learner. Focused on Mobile / Backend / AI + ML