Welcome back.
The next thing that I would like to add to the app is the feature to undo something so you can see I
want to be able to undo something.
So I'm just going to add this little button which will undo whatever we drew as the last entry.
OK, so let's get started.
First of all, we, of course, need to have this little button in our activity, main XML.
So let's put it just in between the other two buttons or you can put it to the left as you prefer.
So with this linear layout called election buttons in which we had an image button and another image
button, so we have two image buttons there and I'm going to add a third one, which will be the I.B.
undo, and I'm going to use another image for it.
So I need to, of course, drag the image into my drawable folder.
So let's open that up here, drawable.
And here I call this one, I see undo, so that's pasted in here, you can, of course, download it
or you can use any other image if you want.
And now we can replace this gallery with I see undo and of course, the description on the image.
Now, let's see if that works.
And we are so we have this little button, let's draw something, pressed the button, and of course,
it doesn't do anything.
That's what we need to implement in this video.
Maybe you already have an idea how to do that.
And, of course, you can possibly untried yourself.
But I'm just going to show you how it's done if you don't want to try it yourself.
So, first of all, we will need to have another array here, array list, to be precise.
Private Vul and undo paths.
So I'm going to call it and it's going to be an array list of custom parts.
So the idea is that it's just going to take the last path that we had in past and it's going to store
that and on new paths.
So if you later on want to redo parts, you could do the same approach that you're going to learn in
this chapter or in this video and then redo it with another array list of redo parts or something like
that.
OK.
So that's step number one.
Then we need to go ahead and add the function, which will undo stuff.
So that's had an undue.
Function here, fun on Click Undo and here if I'm paths.
That size is greater than zero.
So if there is anything to undo, what I want to do is I want to add to current path to the underpass.
So I'm on two paths and paths.
Dot remove at.
And how do I get the last entry of Empathy's?
Well, I just get the size and subtract one so empath that size.
Minus one.
So at this point, we get the last entry of empathy where we move it, but at the same time we get it
as a result.
So when calling this remove at function, what you will get back is E!
Well, he is a little generic here because it's a generic that we're getting back.
But it's basically just going to be the element that is going to be the old value that we delete, so
to speak.
OK, so that's what this remove does.
And then once we on the path, what we need to do is we need to call invalidate because invalidated
will then redraw, so it will call this on draw once again.
And then when Endre is called again, it will again draw to pass onto the canvas.
But now the paths that are in impasse are one less because we deleted the last one.
OK, so that's the whole point of this thing.
OK, now of course, this method that we created, this unclick undo, it's never called, so it's never
used.
What would be a good point to use it?
Well, in the main activity.
So once we click on our image button.
Wando.
So in here, we can just say drawing view.
Dot on Click Undo, and that's it.
So now we can go ahead and test it.
Let's see if it will actually undo whatever we've drawn so that we are.
Let's draw something.
And let's undo and undo again.
Thylacine and andu and onto again.
OK, so now we have an undue burden and we are able to finally undo any errors that we had in our drawing.
And if we weren't happy with the result, then we can get rid of it.
Okiku.
The next thing we will need to do is to export the image, so if we want to export the image to, for
example, our storage, then we, of course need to add a functionality for that.
So let's do that in the next video.