Welcome back.
And this video, we are going to implement the button functionality, and therefore I'm working to implement
this alarm button, as well as the on click button functionality for all of the other buttons, except
for, of course, the operator buttons.
So we have this equal sign displays and so forth.
All of those will get different treatments later on, but for now, we had this function on digit rights
and what it did is it just displayed something on the toast.
Well, now what I want to do instead is I want to write something into my text view input field.
So what is my text input field?
Well, it is my very top text view.
So this text view doesn't have an idea.
So let's give it an ID, and I'm going to call it TV input.
Because, well, that's what it should do, right?
This is where the input should be shown and the results as well, but you could, of course, calls
it a qualitative result or whatever.
But I'm just going to call the TV input.
So TextView input.
Now let's go ahead and use that text you input and in order to use it, of course, we need to find
it by its idea.
So let's go ahead and create a new entry here.
Therefore, let's create a private Avar TV input, which will be of type, text, view Nullarbor and
said that null and then in the create method, we can now initialize it, which means we can find it
by its ID.
So find view by ID.
And the idea was TV inputs.
OK, so that's how we can get a text view, as we have seen before.
And now accordingly, in our own digit, we can now use this TV input.
And what I would like to do is I would like to append to it, which means to the existing char sequence
or to the existing text that is displayed in there already.
I would like to add something.
So what I'm going to do in this case is I'm just going to add the one to it.
So append one like so now the problem with that is that our TV input is a Nullarbor, so it can be null,
which means we need to add the question mark here for this code to work.
Otherwise, it just well, this place the error, as you see.
So it's another bill and we're trying to execute a code that would not work if it is null.
So we're adding this question mark.
So now it is null, it's not going to execute that code, but if it's not null, it's going to execute
the code.
So that just means that if TV input actually exists in the context and works, then it will work.
Otherwise it won't do nothing.
That's the beauty about using knuckleballs here with question marks because we don't have to do all
of the testing with if statements, if text input exists, then do this if it doesn't exist and do something
else and so forth not would just add this question mark.
And it's it really does the trick for us, which is amazing.
So now let's test this in our application.
Let's see if it actually works by pressing any button you see.
It just depends to the existing text.
It depends on one that's cool and all, but I would like to append the actual number that was entered.
So how do I get the number that was entered?
Well, I get it from the button that was clicked and how do I get the button that was clicked?
And that is something that I said in an small sentence before.
Like, I just it was a side statement.
But basically, this view here is the actual button.
So whenever it is on digit is cold, the view will be the button that called this method.
So here when we have this button on, click here the button itself with its ID and its text and all
of its properties will be given to us in this method.
So we get the view, which means now we can use this view as a button.
So here I can just say view as button that text.
So what is going on here?
Well, first of all, we need to import button here and what is going on here.
I have these additional brackets here and so forth.
Well, the thing is, I'm trying to access the views text, but I cannot just say vue dot text because
then what?
I get an error because Vue doesn't have this text property, but the button has a text property.
So I need to say, OK, I know that the view is a button, so I'm going to convert it into a button
and then I can access its text, which is what we can do.
This we say the view as a button.
So when it's a button, I want to get its text.
And while it will be in our case, because that's how we designed our code.
So let's test this.
Let's actually therefore go over here and make sure that the text in our text view is not going to be
this calc text one, two, three and so forth.
But it's just going to be zero for now.
Or we could even go as far as keep it empty, so you could even just get rid of this thing entirely
here.
OK, then the text will be empty, and let's run it again and see how we can now assign the values.
So seven, eight nine and you see all of the values are now given to our text.
You?
All right.
So that's how we can get the text in there.
And then we have this clear button and you see display.
Still, our CRC are what I would like to do in that particular case, when the clear button is clicked,
I would like to reset the text to be an empty string.
So in order to achieve that, I'm going to create a new function, which I'm going to call unclear.
And it will also require a view because we're again working with buttons that can be pushed to us.
And when that happens, I'm just going to set the TV input because it's an option or another.
I need to add the question mark and I will set it text property to an empty string.
And that's it.
So that's how we can implement the unclear method.
Of course, we have to assign this unclear method.
Inside of the unclear on click event, you can now click on this button clear, and then it will jump
to the button directly.
And here I can now say that instead of on digit, it should execute on clear.
So just enter on clear here and then run the application again just to see if this is going to do the
trick for us.
So let's add a couple of values and click clear and you see all of them are deleted.
Yeah, and that's pretty much it for this video.
Now, you know how to connect methods directly and so forth, and you can also see that this is a significant
the easier way rather than having to have created an on click listener for every single button because
let me just quickly show you what it would have meant, because what we need to do in that case is create
the private bar, and I'm going to call this one button one, which will be of type button Nullarbor,
which I need to assign null to.
And then here inside of the on create, I need to go ahead and say Button one will be found via its
ID and the odds are I.D. thought button one.
And then I can add an oncolytic listener for Button one, so button one on click listen there and then
I can say what should happen if button one is clicked and I could just say, OK, well, in that case,
at the one as a string like.
So append that to the text of your input.
And because button one is another, well, I need to add the question mark.
OK, so this all of this code, so one two three four five lines of code.
I need to add now for every single button.
That means I have over 50 extra lines of code that I would need to create instead of just having this
on digit, which really makes it super simple.
I have this on digit, I assign it to each button and that's what I meant earlier when I was talking
about why clean code is very opinionated.
So Android Studio has decided that we shouldn't use this on click thingy, but I'd still want to show
you this trick because in some occasions it's really useful.
And that is a particular occasion, a situation where this is going to be very useful.
I'm not saying that this approach that I just showed you is bad.
I'm just saying that in a particular case, like ours here, where we're just building a quick and dirty
application, it's a lot faster.
When you are using best practices, however, you should always use to on click event that I've just
show you alternatively.
OK, so that's it for this video and by the way, we're going to, of course, do that for all of our
future applications.
So this approach, we're only going to use it for this particular application.
All right.
So see in the next video.