How to draw a custom view? Mateusz Budzar Blocked Unblock Follow Following Dec 30, 2017

In Android SDK we can use a lot of useful predefined and ready-to-go views like TextView, Button, CheckBox, ProgressBar and many others. However there are cases in which we have to do more than just use one of them — we have to create a custom one. If the view is very similar to one of the provided with the Android platform, we can try to extend it and customize it in a proper manner that meets our expectations. But yet, the day will come and there will be no escape — eventually we’ll need to create a totally custom view from the scratch. I was always terrified at the very thought of extending a View class and implementing everything on my own. But it turned out to not be that difficult. Let’s try to implement one together. Imagine that we need to create a custom volume bar. The first thing we’re going to do is to create a class that extends the View . Just one constructor is overridden. The one that we’re using when we’re creating a view from the xml file. For the purpose of the article it’s enough. Now it’s time to draw something! Let’s start from a line. How to draw a line? To draw something, we have to override the onDraw() template method. There is just one parameter that this method gives us — Canvas. We’ll be drawing on it. But how? Let’s see how the API for drawing looks like. Canvas gives as a lot of methods to draw something, like drawCircle() , drawPath() , drawRect() and so forth. We need a line, so we’ll use drawLine() . The method takes five parameters: startX, startY — the point from which the line should starts,

stopX, stopY — the point of the end of the line,

paint — the paint which we’re using to draw the line. The first four are pretty obvious and the last one we’ll cover in a minute. First, we should care about the warning.

Simply put, we shouldn’t create any new instances of objects inside the onDraw() method, because our UI won’t be smooth if we allocate a lot of new objects that eventually will be garbage collected. We need to move creation of the Paint object out of this method. The last thing is to add our view to the layout file. Now let’s run our project and see the results! Yeah! We’ve drawn a line 😊 But it’s not centered as we wanted to. Actually our view takes the whole space. Probably because we haven’t specified how much space the view should take. It’s time to override onMeasure() method. First, we need to decode the mode and size of the view. Why? Let’s look at what the documentation of onMeasure() says about these parameters. Parameters:

widthMeasureSpec — horizontal space requirements as imposed by the parent. The requirements are encoded with View.MeasureSpec.

heightMeasureSpec — vertical space requirements as imposed by the parent. The requirements are encoded with View.MeasureSpec. Parent view passes these parameters in the encoded form, so we need to decode them first — it’s probably connected with some kind of optimisations. Then we need to check the mode. There are three different modes: EXACTLY — when we set the exact dp value (like 300dp) on our view or it’s match_parent ,

, AT_MOST — when it’s wrap_content ,

, UNSPECIFIED — when it doesn’t matter, and a View can be as big as it want to be, because i.e. parent is a ScrollView (then the height in portrait mode is irrelevant). You can play with it and check if it’s true, for example by adding some logs inside the onMeasure() method and changing layout params. So as we can see, we take the given dp value if it’s provided, otherwise we take the default ones which are defined in the res/values/dimens.xml resource file. After all, remember to call setMeasuredDimension() method ⚠️ to apply the view dimensions. Now let’s change the parameters a bit to have a horizontal line and run the project! Cool! Again, now you should play a bit with different layout_width and layout_height parameters to see what’s changing. Turn on Show Layout Bounds option in Developer Options to see the exacts space that our view is taking. If you aren’t familiar with this option, you can find how to enable it for example in my article about margin and padding. Android Developer Beginner. FAQ #1 — Margin vs Padding

In this short article I’ll try to explain what is the difference between a margin and a padding.proandroiddev.com Ok, but we want to have a thicker line, kind of a bar, so we’ll be able to manipulate its height as well as the width. Let’s change the line to the rectangle How to draw a rectangle? Fortunately it’s pretty easy with our example. All we need to do is to change drawLine() method to drawRect() and apply the height value instead of 0.0F to the bottom parameter. We should also refactor the name of the linePaint to barPaint . Let’s also change the volume_bar_default_height to 12dp and run the project. Now we want to change a color, let’s say, to grey. But there is no color parameter on the drawRectangle() method. And it shouldn’t be, because through the Canvas we’re saying what to draw, however we use the Paint class to say how we’re going to do this. So we’ll use our barPaint to change the color. The next step is to draw a circle that will show the current volume level. How to draw a circle? To do this we’ll use drawCircle() method and we’ll create a new Paint object to set a different color for the circle.