Welcome back.
And this video, I would like to add the following functionality, so once we go to U.S. units, we
see feet and inch here.
And once we go to metric units, we see the height and centimeters.
Now you could go as far as to also change the kilograms to pounds here.
So changed.
Hint it will be displayed for our first edit text.
But basically, I want to just add two more edits texts which I'm going to make visible once I'm inside
of the U.S. units and make sure that this one is invisible.
But then once I'm in the metric units, I make sure that those two are invisible and this one is visible.
OK, so I do that based on clicks on this radio button or any of the two radio buttons, so you will
need to implement that yourself.
I would recommend that at least you just need to add an anklet listener.
You can use a few binding here once again to get access to this radio group onto the radio buttons and
then just make the right elements visible or invisible.
And you can work on the property here for your weight.
You could, of course, also add an entire linear layout around it and you could say, OK, I'm making
this linear layout for metric units invisible when I'm in the US units and making it visible when I'm
in the metric units at the other way around.
OK, so there are many different ways to solve this, and I would recommend you try it yourself.
You will potentially have a different solution that I have, but yeah, I would recommend to just pause
the video and try it.
OK, so let's look at it.
So what I'm going to do is I have these two text input layouts, right?
So one that is just the hi, what is it?
The height?
I believe the first one is the height.
And the second one is the weight.
OK, so here metric unit weight and now so let's go ahead and implement that.
Therefore, what I'm going to do is I'm going to add a text input layout here.
Like so which will be below below metric unit weight.
We'll have an idea of text input layout, metric units, height feet.
It has a stroke around it.
A hint text color, which are both green, has a style of outline box, as we have used before and with
will be rap content.
I have a margin towards top of 10.
I say that the layout should be aligned to the start, so align towards the left.
The visibility is set to go on and in towards.
It's visible.
Then I have a layout margin of 10 density pixels and a height of rap content by then and side of it.
And I have to make sure that this app composite added text is inside of the text input layout inside
of it.
I have this at its text with and it takes you as metric unit height ft as the idy width of apparent
height of rap content.
The hint is feed, and the input type is no decimal, so we can only enter decimals.
So that's our first text input, and I'm going to add another one in here, which will also be pretty
much the same.
The only difference will be the idea and its position.
So if we look at it now, we can see that it is direct.
One of them is direct towards the left, the other two towards the right.
OK.
And you can, of course, download the code to get the same results here.
Let's see.
Well, actually, we cannot even get to that yet.
So first of all, let's go over to our code and that would be the BMI activity here.
We will need to get access to our view binding objects quite a bit.
I'm also going to work with a companion object that's probably something that you didn't set up yourself
when you tried it.
That's totally fine.
So I'm going to have this companion object, which is going to have the constant value of metric units
view, which we can then use.
It will just contain the string and then the constant value, which will be US units view.
OK, I want to know which one of the two is active and based on that, make a changes and the adjustments.
So let's, first of all, create a method that will make sure that our metric units view is visible.
So private fun make metric or make visible metric units view, you could call this method differently
as you like.
And then I'm going to need another variable.
By the way, let me set it up here because I need to know which of the views is currently visible,
and therefore I'm going to create the variable that will help me with that.
OK, so this variable will be called current view visible view, which will be of type string, and
I'm going to assign it as metrics units view as a default.
So by default, that will be this variable to hold the value to make a selected view visible.
OK, and now in here, I can now use it.
And as a as I said, there are multiple different ways to solve this.
So if you solve it in a different way, congratulations, that's totally fine.
So I'm going to say that the current of the visible view will be the metrics units view when I make
the matrix units view visible.
And then I need to make sure that my text input layout for the UM weight metric unit weight and the
metric unit height are both being active and being visible.
So I make them both visible and then I make the American versions invisible.
OK, so here the metrics unit weight to GM making their US metric unit weight invisible or gone, and
the U.S. unit height for feet and inch.
I make them gone as well.
So about this metric units weight, you can leave it out.
As I said, if you are find just changing here, just changing the hint would have been an alternative.
But in this case, what we will do is we will basically create another text input layout, which will
take care of the US units for the weight.
OK.
So let me create this in here.
I believe I should put it on top of this.
Metrics US unit might fit like so.
OK, and here it's going to save weight in pounds, I don't know, in pounds, something like this.
And now we should be able to use this to us metric units or white.
OK, now what I'm going to also do is I'm going to clear the high value and the weight value if I had
it, so that when we are changing to the other view that we're making sure that there's no entry left,
OK, and then I'm also setting my linear layouts display BMI result to be invisible.
So even if we have results here at the bottom, once I click on any of the two, I want to make sure
that the results are not displayed any more.
So here, if I enter something and I calculate, you see I got an insane BMI here.
And once I click on this button, I want to make sure that this bottom part is made invisible.
So this is our linear layout that we call display BMI results.
OK, so it's this entire container that holds all of those four text views.
What you can see here for taxpayers are inside of it.
That should happen when we want to make the metric units visible.
Now we need to do the same thing with the US units, so I'm going to just call it may make visible U.S.
units view and you can see that I'm just saying that the units view should not be visible.
I'm basically doing the opposite of what we did up here, where I'm making sure that my metric values
are invisible.
And maybe you should use gone here as well.
And make the US units visible.
So now we want to call these two methods depending on which of the options is activated.
So depending on which of the buttons is activated, so I'm going to set up by default that my metric
unit is going to be visible.
So the metrics option and then I'm going to add a binding thought RG units.
So here I am, accessing our radio group units and activating a unchecked, changed a listener.
So here I'm going to use this one where I get a radio group, which I'm not going to use.
I'm going to say it should be unused and the is fine here in this case, and I'm going to call this
integer that I get the checked ID.
OK, so now what I can do is I can check which of the options is currently active.
So if checked ID is equal to our Dot ID Dot RPI metric units, then I'm going to make the.
Metric units visible and otherwise, I'm going to make the.
You assume that's visible.
OK, so if you managed to get this done, congratulations because we have not seen this part yet.
So here we are using a check changeless in the not a on click listener.
So here you could have tried it with binding dot org units.
Thought set on click listener.
But it would have been very tricky, so you would have to check the state manually and so forth, so
you shouldn't use that for radio groups for them, you should use this on check change listener, which
will give you the idea of the item that is currently active, which is this second entry that you get
from a check on change listener.
So we are using one of the overload, so let's try this once again so that I can explain this a little
better, I guess.
So set uncheck listener.
You see, you get a radio group optional, the one that is used for the on the checking, basically.
And then you get an integer which contains the ID of the item that was clicked.
So this is going to be especially useful if you have multiple radio buttons.
In our case, it's just to right.
We have the metric units and we have the other option.
So we don't even have to have an if statement here or anything we can just use.
And if and if it's not one, then it has to be the other.
So if we're not making the metrics part visible, we're making the U.S. part visible.
So at this point, let's try this.
And let's see if we can make one visible and invisible.
OK, so it does work, as you can see, but we have one little issue here and that is that those items
are not correctly positioned.
Let me double check that.
And therefore, in order to fix it, we need to use a little trick, OK, because we are working with
a relative layout out here, Ken.
And in the relative layout, items are positioned relatively to each other.
Now you could, of course, go ahead, and let's jump to the U.
S. Height feet.
So let me see.
I think feet was the left one.
Yes.
So here there is this layout.
To start off, so here we would have to define to what it is to the left off and then to what it is
to the right of and how we're going to solve this is by creating an additional view in here.
OK, so let me show you, I'm creating this view with the idea of you and has a width of zero, but
a height of one density pixels.
And then it's below our metric unit weight.
Actually, let's use it below our radio group.
So here are the units and the layout center is horizontal, said true.
OK, now we can use this view where we can say that our metric units height feet, it is going to be
to the start of our view.
So I d view.
And the other one is to the end of the view, so the HMU as unit height inch, this ID here is going
to be to the end.
That's why I have this line parent to attend and hear a line parent at start.
Set to true.
OK, so this unit height inch will also need to have this layout and of layout to end off with the idea
of view.
When I do that, however, I also need to make sure that it's not going to be made in gone.
So not this one, but this one not gone, but invisible.
OK, so here, invisible instead, let me run it again.
And now you see it works as I have intended, so you can see every layout has its advantages and disadvantages,
so in this case, working with this relative layout.
So that just to quickly go over so that you will fully understand it, hopefully.
So we have this text input layout, which is our metric units weight and we keep it in the UI.
And I will show you in the next video what that means.
We what invisible versus con means.
But generally speaking, we must have this view so that we so that we can make sure that even though
this view only has a height one density pixel and zero density pixel of width, it's really not visible
at all.
We are adding our own text input layouts next to it.
So we're saying it's two to start off.
So to the left side of this view.
And we are aligning it to the apparent start as well, so we're really making sure that it's to the
left side.
And then this other text input layout, we're making sure that it's to the right side.
So here we're saying to the end of and we are aligning the parent to the end to true as well.
So this allows us to make sure that this view is going towards the left and this view is going towards
the right.
Now, seeing that, I believe it would have been easier to just create a container and make the container
visible, invisible, visible.
I hope you did that and then you have an even smoother solution that I have here.
Either way is fine as long as you're happy with the UI results.
You should be good to go.
OK, so see you in the next video where I'm going to quickly show you what invisible versus con means
and a little more.