Welcome back.
And this video, we are going to add the feature that you can see here, so we are going to have two
different uses for the metric units and the U.S. units.
OK, so the difference really is that the hint is different for that first edit text.
And then instead of just having one edit text here for the height, we will now have two one four feet
and one four inch.
OK, so you can do it with different linear layouts, but we are going to just created in the same UI
and just make them individually invisible or visible.
So we're going to talk to them individually.
So we're going to prepare methods for that.
But of course, you can also use the approach of putting them into a container such as a linear layout
and then just making the whole linear layout visible or invisible.
But as we also need to do something else.
So if somebody enters the wait, for example, and the height, we want to reset them once someone clicks
on the radio buttons.
OK, so let's get started.
What we all need to do is pretty much the same layout that we had before.
So instead of our linear layout called units view inside our activity by example, we had this text
input field with an app compared added text, and then we have another text input layout with another
text input layout.
So what we will need is the same thing, except for that we of course need to change the IDs.
So let's do that.
So the first one will be called T'ai El.
US unit wait, so instead of Metrick, it's going to be US unit wait, the rest stays the same even
though I'm going to set the visibility.
To go on, so only one of them should be visible at the same time, then we have this one will be also
called U.S..
Unit, wait, so that's the actual text and then here it should be weighed in at least and pounds,
then we have this text input layer.
But the problem here is not only that the IDs are incorrect, but we also need to put that in the linear
layout.
So let's put that into a linear layout because we need to have two of them.
Right?
We need to have one.
Which I'm going to call here all U.S. units, right, because we need to have the feet and the inches.
All right.
And this is, of course, not the width, but this is the ID.
So I'm gonna call this one like that.
And then we need to have a with I'm going to say it should match the parent and the height should wrap
the content.
I'm going to set the margin top here so that we have some distance towards the top.
The base line should not be aligned.
So base line aligned.
I'm going to set that to false.
And the orientation should be horizontal.
Visibility should be also set to go on.
So this linear layout should be invisible.
Now, this text input layout that we have here is going to cut that out.
I'm going to paste it twice inside of this linear layout because we need it or we're going to need it
twice.
OK, it's the two layouts which are next to each other.
That's why I'm using horizontal orientation here.
So those two text input layouts, they should be next to each other.
And that's what I'm achieving here by.
All right.
So now let's change the names of those.
The first one should be called L.
So text input, layout units, height feet or use unit height feet.
And this one should be you or yes.
U.S. unit height feet, so this is the edited text and this is the text input layout.
All right.
And here I'm going to set the weight.
To one.
OK, so both labels should have a weight of one, this one and that one here at the bottom also.
So layout weight should also be said to one.
Now, the second text input layout should have a different name, of course.
So this one should be pretty much the same name as we had here.
Only that instead of feet.
Now it says inch.
Now, the with, of course, should not match the parents, but should just be zero density pixels,
the same for this one here, this with because it's just going to take the with that is available.
So it's going to spread it as much as possible, but it's going to share it with the other text input
layout.
All right, now, of course, we also need to change the hint here, so height for the feet is going
to be.
Just feet.
All right, I'm just going to say.
Ft.
Or maybe like this.
And here, it's not a numbered estimate, so you cannot have three point five feet, so it will just
be a number and the same goes for the other one.
So this one will just say inch.
And we'll also just be a number and of course, the name should also be different, so I'm going to
use.
Ettie.
US unit height inch.
Now, there's another something that I'm going to add for the text input layout, because I want to
have a little bit of margin towards the sides, so I'm going to set the margin start.
To be five density pixels, so those two text input layout fields should be a part of each other just
a little bit.
All right, and now we can go to the code, so let's go over to the BMI activity, because that's where
the magic happens.
And I'm going to, first of all, create two.
Variables here, so to Constance, to be precise.
I'm going to call this one metric units view and I'm going to give it the name metric units or unit.
And this case you.
OK, is this going to be the metric unit view and then we have this other one, which is the US unit
view, which I'm going to get the value here and I'm going to call it unit.
View.
All right, the next thing that we need to do is go to our BMI activity and here we need to have these
three variables.
So one of them is called metric units view.
It's a constant, which I'm going to say to metric unit view as the text.
And then we have this other one that is US units view, which I'm going to say to us unit view, and
I'm going to set the current visible view, which is of type strength to metric units to you.
So the idea is that this one will always know which of the two is currently active.
So it will say either the metric one is visible or the other one is visible.
Then I'm going to create methods which will take care of visibility, so I'm just going to add a new
one in here and you private function, so make visible metric units view.
So what it should do is, of course, make the metric units view visible.
And here we're going to set the current visible view to being the metric units view and then we're going
to set the tail metric unit weight.
To visibility equals true, more equals visible.
So if you thought visible.
And then the same thing goes for our metric height, so visibility is going to be set to visible view
that visible.
And of course, the other ones have to be said invisible.
So we need to go ahead and say, OK, t i l.
U.S. unit height.
Actually, it's the weight that one should be set to visibility, you dotcom.
Which makes it invisible.
And then we called this El Al U.S. units height, visibility is said to view that.
So the thing is, we don't need to take care of them individually.
So of the height elements because they are inside of this linear layout.
So we can just make the whole linear layout invisible.
OK, so once again, here they are inside of this linear layout, which is called El Al US units height,
and then we need to make the whole text views.
So if we go over to our activity BMY here and this linear layout called display BMI results, I want
to make this one also invisible that because if we are changing, then we need to add new value.
So this whole thing here, we can make it invisible.
We've done that before.
Right?
So we have done it right here.
So we made it visible once we entered a text.
But here, of course, we could have summed it up so we could make this whole text a lot less complicated,
so we could make the whole thing visible from the get go and then just make it invisible by using.
The following code, so I'm just going to say hello.
And this one was this L'Alpe display, PMI results, that visibility.
View dot gone, so I'm just going to make this whole thing invisible.
But then, of course, we need to go over here and instead of having it like this, we should also just
go ahead and say, Al.
This result.
That visibility is equal to visible, so view that visible, all right, and then, of course, we won't
need those lines here anymore, those four.
So I'm just commenting them out, but now when we do that, we also may not forget to also look at our
XML file and make the adjustments, because here we made every single one of them invisible.
So now we would need to need to make sure that the.
Linear layout itself is invisible.
OK, here, invisible for the actual visibility and then where do we have anything else?
OK, as it seems, all of the others didn't have this in their setting, so that's good.
Now, we can go back to our PMI activity here.
And make a similar function to make the U.S. units visible.
So make visible U.S. units view.
And of course, we need to.
Make the U.S. units view the current visible view.
And this one should be set to go on.
Gone, this one too visible.
And this one as well.
In both cases, there's one more thing that we should do, so I told you that we should take care of
all of those edits, texts, they should be cleared.
So what I'm going to do is I'm just going to say, OK, clear them all.
So, Ed.
US unit height, weight and so forth cleared them, so we should have them, as you can see here, and
when I created this, I see that I called this one feet instead of feet, so I should definitely check
that one out again.
So it's this added text here.
This one here, so it used, you know, its height and feet and 18 feet.
OK, and now the same thing goes, of course, for our metric units.
And in here, of course, we also need to make our metric at its text's clear so we should empty them.
OK, so clear the text, make them empty.
That's pretty much what we're doing here.
All right.
And now what I'm going to do is I'm going to go over to my uncreate method and in here I'm going to
add the following thing.
So in here, I'm going to add this call to this method called make visible metric units view.
And now the cool thing is, if you wanted to make this dependent on the user's settings, then you could
do that here so you could have a check.
Is the user and American then showed the American view with the feet and inches.
And if the person is in the metric using country, then you could display this one.
But I'm just going to by default, use the metric system as a default setting and then we can go ahead
and set the radio buttons.
OK, so here how you can do it is you can use the radio group and the radio group was called Units and
add an unchecked listener.
So set unchecked changed listener.
And I want to use this third one.
All right, so we have this group and the check I.D. and then we want to execute the code.
So I'm just going to check if the one that is active is the metric one.
If that's the case, then make the visible metric units view visible and otherwise make the American
view visible.
All right.
So if checked ID.
So this variable right here that is passed to us have checked it is equal to our Arbie metric units.
So if the radio button that is checked is the one that is metric, then called this make visible metric
units view function.
And otherwise called the other function so else called make visible US units.
You all right?
And that's it.
So you can see how you can use radio groups and radio buttons.
So you have this set unchecked change listener, which is similar to a click listener.
But here you have to take care all of the different check IDs.
So we only have two.
That's why I can use this approach of if and else.
Otherwise it could also have if and else if at another else.
If so, if you have multiple options then you would have to consider all of them.
But we only have two.
So I'm just fine with just having if for the metric units and otherwise it's just going to be the US
units which will be visible.
All right, that should be it.
So now let's run this code and see if it actually works for our application.
All right, so there we are.
Let's go to the calculator.
Let's change the US units and you can see it works.
But what I don't like is that it moves a little bit, so we should to look at that, but other than
that, you can see it works.
So we fit an inch here.
We have to wait here.
And this must have to do with the margins somewhere, so let's look at that real quick.
And as it seems, it's this margined top that we have, so I had a margin top for my text input layout,
the first one, but I don't have one for the second one.
So that is still used for the US units height.
So I'm going to get rid of this margin top here and the same for the FT here as well.
And then this text input layout, so actually for the first one we actually needed for the first entry
here.
So.
You can see we had that for our Metrick unit height.
But wait, not OK.
Let's check in.
So now they are even close to each other, so we need to put it out for our.
Bottom text, so we need to give it this linear layout, so this linear layout needs to have this margin
top because that's the one this allows US units height that has both components in their.
All right, there we are.
So, no, it's fine.
OK, so in the next video, we are going to finalize this because if we enter anything in here, it's
not going to do anything because we haven't connected our code to these attacks yet.
And that's what we're going to do in the next video.
So see you there.