Welcome back.
And this video, we're going to implement the under your button, so basically undo button here, you
can see once you click on that, it will delete the last entry on our drawing board.
And yeah, let's just implement it.
Therefore, let's go over to our project.
And the first thing that I'm going to do is I'm going to duplicate this image button here, and I'm
going to call this one image button undo.
And then according to I need to use another image or another trouble, and I have this trouble.
You can just use any trouble you want.
I will just use this icy undo.
You can download the project and get it from there.
So this will be the page that I'm going to use and I'm going to assign it to my image.
But and so the idea is that it's going to look like this where we have the gallery here, the image
button and then the brush size selector, so to speak.
OK, now let's go ahead and use it.
In order to use the button, we need to do the same thing the image button.
We need to do the same thing as we did here with the image button.
So let me just copy the I b brush.
And accordingly, this will be I undo.
Here on do like so fine view by ID and accordingly, we want to assign the on click event for it as
well.
So but I'm not going to call the show brush size chooser dialogue, but instead what I want to do is
I want to call a method that will undo the drawing.
And here, by the way, this is not ibey brush, but I be undue.
So that was the idea of our image button, which is this button here?
OK.
So where could we do something that has to do with drawing well and our drawing view?
That's where we created all of our paths, right?
So there is this empaths variable that is an array list of custom paths and custom paths we saw.
It's just our own class that we created here as well.
And now we are going to require is basically going to be the same thing, but one for undue puffs.
So an ArrayList, that will be a list of custom paths, but we'll just collect or store to undo paths.
Now accordingly, we just need to create a method that will take care of the undoing.
And how do we do that?
Well, basically what we want to do is we want to delete an entry from empaths and we want to store
it in undo, perhaps.
OK, so let's create a method for that.
So fun on click undo.
So I'm going to call it, and I want to check first if there are any paths even to delete from.
So only if there are paths, which means if the imperfect size is greater than zero, only then do I
want to add something to my undo.
Perhaps.
And what is it that I want to add to it?
Well, the path that I'm removing from my empaths, we can do it in one line where we say and perhaps
remove that and then the index and what is the index that I want to use?
Well, I basically want to delete the last entry.
And how do we get the last entry of an array list?
Well, by using the size of the array list minus one.
So empaths that size minus one.
Because what that remove add method does is it returns the item that it removes.
OK, so we are basically receiving the item that it removes and we are adding it to our undo paths.
So this by itself would not call the on draw method.
This one here, which takes care of all of the drawing right, which takes care of drawing all of the
paths.
So here and perhaps that's basically all of the custom paths that we created, and that's where it actually
draws them.
So what we need to do is we need to call this method once again.
So this on draw method once again, and we don't call it manually by saying on draw like so.
But what we can do instead is we call the invalidate method, which will then internally call on draw.
OK, because this is an override method and this is done for views, right?
This is a method that exists inside of views because our drawing view is of class type view.
So it's just going to redraw the entire page.
So that's what our Click Undo method will do.
I know we just need to call it.
And the best spot would be once we click on the button.
So here in the set on Click Listener, we can just use this method.
But because this method is in this drawing view class, we need to make sure that we use the drawing
view here in order to get access to it.
So here I'm just going to say drawing view dots on click undo.
So I'm just going to call this method.
But because drawing viewers and Nullarbor, we need to add the question mark here.
OK, so now let's go ahead.
And test this and see if it's going to work, so draw and draw and you see the undo works perfectly.
And withdraw some more.
And also with different colors and stuff, and I'll click the back button and you see it works as intended.
So now you could implement a redo button very easily, you know how to create the undo button.
Basically, you just need to add back the.
Here, the last, Andrew, from undue payoffs to impasse, basically with the same approach that you
have here and then call Invalidate, so I would say try it by yourself.
Implement this as a little challenge for yourself.
This will really help you to understand this a little better.
And good luck with it.
So in the next video, we're going to look at cold routines, which is a little demo that you will need
to understand how to run cold in the background.
And then we're going to look at how we can make a sandwich out of our images because we actually have
two images here, right?
The image in the background and what we are drawing.
All right, so see you in the next video.