Welcome back and this video, we are going to add the functionality for the BMI calculator and for now
just one for the metric system.
So you can see once I click on it, this window pops up so I can enter the weight and I weigh six to
nine kilos and then the height in centimeters and I'm on sixty nine and then we can calculate it.
Then you can see your BMI, so on, so normal.
Congratulations.
You're in good shape.
All right.
And that's what we're going to build this video.
And of course you can go ahead and try to build that yourself.
What you will need to have is of course the formula for BMI and which values mean what.
But if you build everything else except for this specific functionality, then it's going to be fine
already as well.
You can, of course, Google how to calculate the BMI and what the different metrics are there and try
to build everything yourself.
But otherwise you can, of course, follow along.
I still think that is a great exercise.
So if you want to challenge yourself, please go ahead and build that.
All right.
Let's go ahead and build that UI first.
So that's something that we, of course, need to do in our BMI X amount.
So here activity underscore BMI, XML, and here I'm going to put that underneath my toolbar and I'm
going to use a linear layout here so you can see we have a relative layout as the surrounding container,
so to speak.
And then inside of that, I have a linear layout and there is a lot going on there.
So I'm just going to paste it in here and then I'm going to go through it because this is a lot of text
that we need to use here.
And when pasting the activity in here, you might get this problem here.
This little arrow, it says, cannot resolve come that ad, so forth.
And what we need to do is we need to use this comma, Google, that Android, the material, and add
that to our build cradle.
So the module app.
So this one, we need to add a dependency in here.
So implementation.
And then apostrophe Google material and here colon material.
Kolan.
1.0 dot zero.
All right, and then you need to sync it, and by the way, this is within the dependancy, so built
a cradle and the app one.
So there are two, as you can see, the project one at the app one.
So the app one, you need to add that under dependency.
So this line here implementation.
Cool, that android, that material and then sync now and then you will be able to use it.
This is something that is the case because we are only using empty activities.
If we were using blank activities, then that would be a different story because then our basic activities,
then this would be there automatically.
OK, but now coming back to this line here, you can see now it's not a.
Complaining anymore.
So what we have is a linear layout, and I called this one the units view that's matching the parent,
so it's taking the whole with height and it is underneath our toolbar.
This one here.
So our toolbar, BMI activity.
Because we are.
In a relative layout, and then we set the orientation to vertical, we have a little padding, and
then I have this text input layout which has an idea I call the telemetric unit wait.
Then we set a particular style and I set the style to outline box.
But that allows us to do is to have this pretty cool style that you can see here.
So we have this outline box and once I click on it, you can see now the hint or the predefined text
goes up towards this line here.
So this is a pretty cool interaction.
And you get that by adding this text input layout and adding this particular style here.
So I'm saying the width should match parent that the height should only be as much as required.
And then in there I have this compact added text.
So that's just the latest version of an edit text.
I called it metric unit height weight and then match parent in with.
So take the all with only as high as you require and then the hint should be weight in kilograms.
So you can see that this is the hint that you can see here, which is pretty cool, and that's also
what will be pushed towards the top.
So that's something you can play around with.
This value here will change what people will see there.
Then the input type should be no decimal.
So if you do that, then this keyboard comes up where you only can enter decimal values.
You cannot enter any alphabetic values, but really just numeric once.
And that's what we get with no decimal.
All right, textiles, the 16 S.P..
And then I have another text input layout, the same thing.
I called this one till unit height and then it uses the same style.
So this outline box style, all the values are the same except for the ID here.
And a hint now is height in centimeters.
OK, underneath it I have this a linear layout which is the display BMI result layouts or display BMI
result.
It should match parent in terms of with but it should prep its content in terms of height.
Emergen top should be twenty five.
So a little distance towards top vertical again.
So stuff is put on top of each other.
Visibility is such a visible so you might guess it's already we will change the visibility.
We even said Tool's visibility too visible, so this is just for.
Debugging and developing purposes, then we have this text you and another text to you and to more text
use.
And if we have this button, so let's look at those four text views.
One is called TV, your BMI.
Again, Metropia and Web content, growthy centered text this your BMI text.
All CEPS is such a true.
So all the text will be in capital letters.
Visibility is said to invisible and then we have the text size to 16.
Now the same thing for this one.
So here the BMI value is said to match parent content.
Margin is five density pixels with a little gravity.
We have some text US settings here and the text itself in the tools just says 15, even though we're
not using it like that.
So Tool's is here what we can see here.
So just did we see anything in there?
Right.
So basically these are the four text fields that I have there.
And I want to let the user later know what his actual BMI is.
And these are the four text fields or text views which do that.
OK, so we have this text you, which is the TV, BMI type.
And it's set centered to Texas normal, so are you overweight or are you normal, are you on the way
to those kind of things?
So this is going to be this text view and then the final text, you can see a little description.
So are you fit?
Are you healthy, in danger?
So, you know, you need to really take care of your health and lose some weight or gain some weight.
So this is the kind of text that will be instead of this text, you and then on the need that we have
this button, which is called button calculate units, and it's going to be gravitating towards the
bottom.
The margin will be set to thirty five density pixels.
We have a little background color here, which is our green background color.
So it's basically this calculate button here.
And then the next caller is said to White.
All right, so that's the UI.
Now let's go into the cutline coat.
So here I'm going to create a new function.
So inside of RBM I activity, I create this private function, private fun called validate metric units,
and it's going to return a boolean.
So the idea is that we want to check that the user enter something, correct or not.
So I'm going to create a variable called is valid and I'm going to set that to true.
So expecting that the entries that the user entered are true or are correct and valid and then I'm going
to return is valid.
Now, of course, I need to set Isabelita false at one point if it is false.
So I'm going to say if the heat metric.
United.
And wait, so those two, I think it was the top one, so let's start with this one, the tech start
to string is empty.
So if it is empty, then said is valid.
To false and else.
If the.
IT metric unit.
Height in this case has.
The same story, so text to string.
Is empty, then also said is valid to false, so I guess I'm just checking.
Is any of the two fields empty?
Because if one of the two fields is empty, then I don't want the calculation to do anything.
So the buttons shouldn't do anything.
Or maybe we should inform the user about having to enter a valid entry in both fields.
All right.
The next thing we need to do is we need to add an on click listener for this calculate button.
So in order to do so, we're going to go into our uncreate here because that's where we handle our unscrupulousness
for our buttons on the highest level.
And I'm going to say button calculate units.
And that's, by the way, this button here calculate units to one at the very bottom.
Of course, if that one is pressed.
So set on Click Listener.
Then I first want to check if the entries are correct, so if the user entered something useful, so
in both fields there is a value in there and then I will store the highest value.
So value, height, value.
Here, height value is going to be of type float, and I'm going to say it's going to be metric height.
Thought text.
The two string.
And then transform that into a float.
Why can I do that?
Well, I can do that because the entry itself can only be a number here, as you can see.
OK, so now I can.
Transform that or convert that string into a float.
Now, here's what I also want to do, is I want to convert that into meters, so I'm going to divide
that by one hundred.
And then.
I also need to have a value for the weight, so weight value is also going to be a floating value and
it's going to use it metric.
Wait, the text to string.
To float.
And here, I don't need to calculate through anything or divide by anything, and then we can calculate
the BMI.
So the BMI is very basic.
It's just a weight divided by the height squared.
So while BMI is equal to weight value, divided by.
Heights multiplied with height.
That's it, and now we can go ahead and display the BMI results about in order to do so, we need to.
Create another function, because I don't want to do all of that code inside of our BMI activity.
I would be too much.
So let's create another method here.
Which I'm going to call this place of private fund display.
The result?
And of course, it needs to have a float in here, so I'm going to call this one BMI, which is of type
float, and then in here I need a label.
So Volle will be am I a label which is going to be of type strength and a value which will be the BMI
description.
Also of type strength, and then what I will need is the comparison.
So the thing is, I'm going to paste it in here because it's a lot of very.
Boring, but let's look at it, so it's always comparing.
So if this is a comparison and it goes all the way down there.
OK, so I'm checking to be BMI.
Compared to 15, less than zero.
Or equals zero.
So what does compared to the competitors value with the specified value for order return zero if this
value is equal to the specified other value, a negative number?
If it's less than the other.
Or a positive number if it's greater than other, so we're comparing two values to each other, so we're
comparing BMI now.
So the value that was passed to us, we're comparing that to 15.
If that's the case, it's going to set BMI level to very severely underweight BMI description will be
obese.
You really need to take better care of yourself, eat more, and then we compare it.
Is it between 15 and 16?
So that's what this code here does.
And if that's the case, then we're going to ride severely underweight.
Oops, you really need to take care of each other or of yourself.
So the text is the same.
But we are also, of course, going to write the BMI to him as well.
But as you can see, the BMI label is actually changing, so this description is the same what the BMI
label is a little different.
I can't then we check is it between 16 and eighteen point five?
If that's the case, then you are still underweight.
Is it between eighteen point five and 25, then you are in a normal shape.
Congratulations.
You are in a normal shape.
And then.
We have between 25 and 30, then you're overweight.
Between 30 and 20 or 35 here, you can see the Europese plus one moderately obese, oops, you really
need to take care of yourself, work out more or maybe.
And then we have this other statement here.
Where we compare it, 35 to 40, so is it between 35 and 40 your BMI, then you are obese, class,
too severely obese.
And then if none of the beforehand is true, then you are severely or obese.
Class three, very severely obese, and then you really need to act because that's it's really time.
OK, so that's basically the whole thing here.
You see, it's rather boring.
So I hope you're full of forgive me for not entering everything or typing everything.
So then the next thing that I want to do is I want to make all of those text fields visible, because
if you look at the text fields again.
So here.
The text views actually the whole linear layout visibility is set to visible, but the text you hear
is invisible.
And actually, let me check the other text fields.
They are set to visible, but they are never used.
So.
What we can do is we can make sure that they all are set to be invisible and we do that with these lines
of code here and we need to import you in order to use this code.
So you can see I'm using TV, your BMI visibility to visible BMI value to visible BMI type to visible
and BMI description to visible.
All right, now, the next thing I do is I calculate the BMI or.
Now, the next thing I do is I round, so I have to tell you, BMI value, which takes in the BMI here
and makes it a big decimal.
So what that does is it is used to round the result value to two decimals after the decimal point.
OK, so I'm setting the scale to two and I'm using the rounding mode how even and I make a string out
of it.
So this is what this line of code does.
It looks complicated, but that's basically what it does.
So big decimal, say, a decimal hour of the or takes our big decimal, then brings it into the right
format, as we do here with setting the scale and rounding it and then making a string out of the whole
thing.
All right.
And then the next thing is we need to set our text so we have multiple text.
Right.
So we have, for example, the TV, BMI value.
We can set the text to be the BMI value.
So we create this additional variable here.
This one here.
Then we have the other variables, which we haven't set yet, so I type the text should be said to be
my label.
And the TV, BMI description should be said to the BMI description.
So at this point, you can see this code is not gray anymore.
It's white now.
So we are using it now.
All right.
Already, so now there's one less thing left to do, and that is to actually ever call this function
that we just created, this display PMI result, and that is in here.
So we pass in the BMI, which we just calculated based on the height and weight.
And by the way, if the user didn't enter anything useful, then I'm just going to.
Prayers and a toast here saying, please enter Veliz values and the length of the toast is short and
then it should show this toast.
All right, so this is the coat that we need in order to test it, so let's test it on our emulator.
There we are, so BMI calculator, we can see Texas looking good.
Let's set the weight now again to 69, the height to 169 and calculate and you can see your BMI twenty
four point one six a.m. congratulations.
You are in good shape.
So you can see that here, twenty four point one six is what was created by this line here.
So where we scale it to two, we rounded it off evenly and.
That's pretty much it, because the calculation itself probably returns a different value.
So I entered it in a calculator, and as you can see, this is the value that I get twenty four point
one five eight eight, eight and so forth, super long value.
That's not what you want to display to the user.
You should always limited to a much shorter number that is still useful to the user.
OK, so that's it for now.
In the next two videos, we are going to do the same thing for the BMI calculation, for the US system,
so with inches and pounds and all those kind of things, and we will, first of all, prepare that and
then afterwards we will finalize it.
So we're going to require two videos for that.
So see you in the next video.