So now that we've got the layout change
to include our extra button, it's time to
write the code to make the button work.
Now remember that the solution I'm going
to show you is only one solution, so if
your solution works then that's fine
and you've completed the challenge. So
what I'm going to do first is start by
deleting all the commented out code from
a few videos ago. So I'm going to open my
project pane. I'm going to come down to
our MainActivity, and I'm going to
delete all this code, as I mentioned,
that has been commented out. Now I don't
need to delete the imports, but depending
how you've configured Android Studio, you
might have two import lines here, that are
now showing as unused - are sort of greyed
out. So if that's the case, they've probably
been commented out if you've followed, been
following a previous video, so delete
those now. But in my case, because I've
configured Android Studio to
automatically remove unused imports, I
don't need to do anything there. But I'm
going to go ahead and delete this block
over here, that's been, that I've commented out;
the three private definitions Then also
down here, this block of code, which we
discussed the reasons why we don't need
that anymore in the previous video, a few
videos ago. I'm gonna delete that as well,
and just clear up some empty lines there
as well. Alright, so you may have decided
to change the listener for the operator
button, and include the Neg button as
just another operator. For my solution,
though, I decided to create a new
listener, in the same way as we used in
the last section. So what I'm going to do
is add that to the end of the onCreate
method, right down here down the bottom -
basically, here on line 58. So I'm going
to type buttonNeg.set
OnClickListener,
and setOnClickListener
needs the code. So we're going to start
with a left curly brace view and an arrow.
Then on the next line, val value is equal
to a newNumber.text.toString.
Then we're going to do a test here to see
whether it's empty, so if value dot
isEmpty.
If it is empty, then we're going to open a code
block and we're going to set the new
Number, or set number, the edittext, dot
setText parentheses, and we're going to
set that to the minus sign - the
negative sign. Otherwise, we're going to
do some more processing, so we're gonna
have an else block there. And we're going to
start the else block with a try, and
we're going to catch e : and it's going
to be a NumberFormatException, Number
FormatException. And what we're trying
to do here is, we're going to put a
comment here, indicating what would have
happened for a Number Format Exception.
So in in this case, the new number was
set to that already, or a period. So clear
it, if that's the case; newNumber.set
Text parentheses and two double quotes,
just to an empty string. So that's what
happened if the newNumber Format
Exception is thrown, but assuming it's not
thrown, we want to add some code there
to process the number accordingly. So
we're gonna put var doubleValue is
equal to value.toDouble. Then we're
gonna type doubleValue star equals minus
1, so we're multiplying the value by
negative 1. Then we're gonna type newNumber
dot setText doubleValue.toString,
so converting it back to a string so
that we can set the EditText back with
a value - and that should, effectively, be
it. So I've set the onClickListener,
basically, in the same way as we did for
the Button Click Counter, using a lambda.
Now, no other buttons will be using this
listener, so there's no need to create a
named instance, but there's no problem if
you have in your solution. And we'll
probably get a warning from Android
Studio, because one of our layouts
doesn't have a buttonNeg. So if you see
that, don't worry too much about that.
And come over here and have a look, and I've
got a semicolon there from my Java days, so
let me delete that, because it's warning
us about that - which you don't need
because, of course, this is Kotlin and not
Java.
And this is the other one I was talking
about; The resource is missing in some of
layout versions. Parameter view is never
used, and it's talking about the fact here,
that we don't, basically, don't have a
buttonNeg button on both our layouts
because, of course, we've only, at the
moment, updated the landscape version. So
the code itself is pretty simple. If the
text in the widget is empty, then we're
setting it to the minus character and
we're done - that's this code up here, on
line 61. Now the four, the other digit
buttons will add their digits after the
minus, so negative numbers can be
entered. However, if the input text isn't
blank, we go into the else code block.
The code then tries to negate any
number that may have been typed into the
widget. Now if that works, the number is
multiplied by negative 1 to negate it,
here on line 65. Then the widget is
updated with the new value. So positive
numbers in that scenario become negative,
and negative numbers become positive, and
that's exactly what we want here. If
however, there wasn't a valid number, then
that's going to be picked up by the
try/catch block. Now we've already
checked for an empty string, and we're
actually setting the text to a minus
sign if that is the case. So we don't
need to check for that again, in the
catch block. So the only other values
that could cause the catch block to be
entered, are the minus symbol by itself,
if that's been entered, or a decimal
point by itself, or a minus decimal. So
all we really have to do is clear the
text in the widget, in that scenario. So
that's it - the app should still work but
I do have to be careful now, how I test it
at this stage though.
Remember that we haven't modified the
portrait layout yet, so the layout
won't have a buttonNeg, and the code
will crash if I run it in portrait mode. So
I'm going to start the emulator up. I'm
going to run the app, and I'm going to
switch to landscape before doing
anything else, just to make sure it
doesn't crash - and that's probably too
slow to do that now, but I'll try. So I'll
switch to landscape, basically, to avoid
the portrait code from running, and
luckily the program is still compiling,
so I'll just wait and speed this up.
Okay, so you can see the app's running.
Because we're in landscape mode, we
didn't crash, and we can see our neg
Button over here. So let's see whether it
works. I'm going to start by typing 58 x
9, pressing neg, + 22 / neg, and the
negative sign there, 5 equals. So
basically, we end up with the value of
100, which is correct in this case. So
basically, you saw that it successfully
negates an existing number 9, and also
allowed for a negative number - negative 5 -
to be entered. Now of course, if I go trying
to go back now to portrait mode, we're
going to get a crash, as we see straight
away, and it said, actually, a
NullPointerException, if we have a look
in logcat; a NullPointerException,
attempting to evoke virtual method
for the Button.set
OnClickListener.
And that is, of course, because we haven't
added the button to the portrait layout
yet. Now sometimes you have to create
different layouts, so that your app looks
ok at different screen sizes. Doing that
does create extra work because you have
to maintain more than one layout. Now you
can test the app before modifying all
the layouts, just like I did here, to make
sure it all works, before putting in the
effort to modify both layouts. Just be
careful to set your emulator into the
working mode before running the app, and
don't cause the other layout to be used,
until you've gone ahead and created, or
modified it. Alright, so I'll end this
video here. In the next one, we'll make
changes to the portrait layout, and add
the negButton to that portrait layout
to get it working, and we'll then have the
app completed. See you in the next video.