Welcome back in this video, we're going to implement calculating the BMI using US units, which means
we will need to use a different formula.
You see a dividing weight in pounds by height in inches squared.
Multiplying by conversion factor of 703.
So yeah, it's quite complex.
The metric system makes it a lot easier, but let's just implement that.
You can try to do this yourself, of course, but basically speaking, you need to get the data first.
You need to validate whether those added text are empty or not.
If they're not empty, then do the calculation at then displays.
OK, so let's go over to our code, and something that would be very easy will be to validate.
Basically, we need to do the same thing as we did here, but with the other added texts or with three
added texts now instead of just two added texts.
OK, so I'm just going to paste it in here and go over it really quickly.
So I'm validating the U.S. units now.
This is a new method I'm going to set is valid to true by default.
Then I'm using this one statement.
When this added text metric unit weight, which is this one here at the top is empty or this one is
empty or this one is empty.
In either case, I'm going to set is valid to false and I'm going to return is valid.
So it's going to be true by default.
But then, depending on whether one of them is empty, we're going to overwrite it.
OK, so now we need to do the calculations.
So let's go ahead and create this new function that I'm going to call calculate units.
And first, I need to check which view is current divisible.
So if it's the matrix view, I just want to do the code that we did once we clicked on the button,
I believe.
Let me check.
So we have this, yes on click if!
Validate metric units here.
That's what we're doing.
OK, so let me take all of that and put it into this method.
Well, we calculate.
The units I would calculate BMI, basically.
OK, so we validate if the metric units is true or not, and only then are we setting up all of those
values, right?
And then we calculate the BMI and we display the BMI result.
Otherwise, we're displaying that there is nothing to see.
OK, so that's one thing.
But then if we are in the other view, which means we are not in the metrics units view, but in the
US doing its view, then we want to validate once again if the Validate Yes unit, so you validate us
units is true and only then do we want to get the height, value and feet.
So here you as unit height value ft, which will be a string.
We get that from our edit text.
US metric units hide feet and we convert its tax property value into a string.
Then we do the same thing with the hide value.
So I'm going to do that with both height values.
So we're doing it with both values here, with the feet and inch, and then we're also getting the weight.
So he had a weight value would be a float.
We're getting it as just a metric unit, weight text to string and then we're converting it into a float.
So here, let me put it like this.
And put this into the next line, I think that we should be good to go.
Okay, so.
Like this?
OK, now we have the values.
The problem is we now need to calculate the height value and the height value is in inches and also
in feet, but one feet.
Or one foot is a 12th of an inch, so we can calculate it using the following following formula so we
say the height value is going to be the US unit's value, an inch, which is this string.
We convert it into a float, plus the generate height value in feet, which is in a string, and we
convert it into a float as well, and we multiplied with 12.
OK, so this will give us the height value because in order to calculate the BMI, we need to, as you
have seen, multiply with 703 and what do we need to multiply while the weight divided by the height
squared?
Okay, so the height value times the Heidfeld.
You will give us the high value squared and we take the weight value and divide it by this.
And then we take the 703 value to get the BMI.
So whoever came up with this?
Congratulations.
So then we can now take this BMI and display the BMI results with that BMI.
And if we do not have the valid US results, we can display this house block that we have here as well,
which will just give us a toast text.
Like so, which says, please enter a valid values.
OK, so now we just need to call this method calculate units once we click on the calculate button.
So all the way in our own create, we have this button calculate units set on Click Listener.
Here, we need to calculate the units.
So I believe there should be it, let's just try this.
So let's go over to the BMI calculator.
Wait.
Which would be one hundred and fifty six, in my case, fee heights.
I'm not very tall.
What is it?
It's five point five for like four, five and six, five five something like this.
Calculate them.
We see we have a similar BMI than I had in the metric units.
So if I enter a 71 with one 69, then we see, well, it's and it's not exactly the same more or less,
right?
So that's the thing with feet, an inch and everything.
But yeah, basically, you see, our application works now flawlessly for both ways.
And now, instead of just having the BMI calculator here to bottom, I would like to also add a history
of my exercise.
So that's what we're going to do in the next video.