Android comes with many built-in components, but some tasks are best accomplished by rolling your own. We will start from a simple example of enhancing the TextView class, and finish with a complete custom view with its own size measurement, drawing function, and custom attributes. We will also discuss the best practices for custom views. What is the difference between a custom view and a fragment? How about a compound control? When to use which? By the end of the course, you will know not only how to create reusable custom components, but when to use them.
Chiu-Ki is a mobile developer with a passion in speaking and teaching. Her mother tongue for mobile is
Android, acquired while working on Android Maps at Google. Now she runs her own mobile development company to produce delightful apps, and speaks at various conferences to share her knowledge.
Extend a Simple View Hello, this is Chiu-Ki Chan, and welcome to the Extend a Simple View module. The simplest way to build a custom view is to extend a simple view. To do that, you pick a view that is closest to what you need and extend that so that you can reuse the parent class functionality while only writing code for the parts you need. That way, you don't have to rewrite the whole view to handle all the layout and drawing and all those complexities. I'm going to show you an example where we extend TextView with some extra functionality modularized into the custom view. I call this the VersionView. We will start with a Hello World app, and instead of showing Hello World, we will change it to show the version name of the current app. After that, we're going to extract that bit of functionality and move that into a custom view, and I'll show you how we can use the custom view and also take advantage of the parent class functionality.
Compound Control Hello, this is Chiu-Ki Chan, and welcome to the Compound Control module. What is compound control? Compound control is a group of views, such as TextView, buttons, ImageViews, etc. , that got put together, often with some additional functionalities. A custom compound control allows you to modularize your code so that you can reuse the same set of functionality with the same set of views. In this module, I'm going to use a LengthPicker as an example. We will start with an activity with a LinearLayout with two buttons and a TextView. We will extract that functionality to a custom compound control. After we have created the custom compound control, we will add some functions to expose data, save state, and then I will also show you how to reuse the same custom compound control multiple times in the same activity.
onDraw Hello, this is Chiu-Ki Chan, and welcome to the onDraw module. In this module, we are going to learn how to override the onDraw function of a view so that we can customize its look. To draw in Android, we need two things, a canvas and a paint. Canvas is the place where the drawing happens. There are two groups of operations. One, you draw using functions such as drawLine and drawCircle. Two, you can transform the canvas itself before you draw. Think of the canvas as the piece of cloth you mount on an easel. If you rotate a canvas 90 degrees, draw an arc and a colon, then rotate it back, you will get a smiley face. Unlike a piece of cloth, you can also transform the canvas by scaling, skewing, and other kinds of operations. We draw on the canvas and we draw with paint. You can configure the paint style, change its color, adjust the stroke width, and customize a whole host of other properties. To understand how to use canvas and paint in the draw function, let's go through an example and make pizza together.