Welcome back.
And this video, I would like to add the functionality that allows us to see if an answer is correct
or not.
So, for example, this is the flag for Argentina.
If I select Australia here, for example, and I click submit, you can see that background color is
red.
And also the text is fact here because that's selected to answer.
And then this green one is the correct answer or would have been the correct answer.
You can also see that the submit button now works.
And once we click on Go to Next, it goes to the next question.
And also here we have, for example, let's say iSelect Australia here.
That's the correct answer.
So it's going to give me the correct background.
So what I need is a background which is going to be green, and then another background, which is going
to be red.
So green for correct and red for incorrect.
And that is something that we quickly can set up.
So I'm just going to go ahead and go to my resources inside the drawable folder.
We have this default option border, which I'm just going to copy and paste once again, and I'm going
to call this one correct option border PJI.
And what I'm going to change in here is that I don't need a stroke.
So it's still going to be a rectangle, but I don't need a stroke.
And the only thing that I want to have is another background color.
So here I'm going to use the default color holo on the screen, green light colour, which will be this
light green.
This is the correct border.
And now let me duplicate that correct option border PJI once again.
And this one will be the wrong option.
Border PJI.
And here, the only difference is that we are going to use hollow red.
So how low on the score red?
And here also the light color.
All right.
So I don't use a stroke, as you can see here, there's no stroke surrounding it, so no border, it's
just plain background.
And of course, I need to find a way to set those accordingly, so I want to set the buttons accordingly
and that's what has to be done in the quest questions activity.
So the first thing we can directly do is in the uncreate function, we can give our button submit.
The set on Click Listener here.
And we're going to pass this.
And then we could, of course, go ahead and assign to submit functionality in here directly so we could
just go ahead and say our I.D. dot and the button, what's called button submit.
So once we press that, what shall happen.
But the thing is, we have nothing prepared there yet.
So we will need to also be able to assign the right color for our background.
So for the background of the button and therefore I'm going to create a new function here, private
fun.
And I'm going to call this one answer to you and it needs to know the answer.
So the end of the answer and what it should draw and I'm going to call this one drawable view, which
is also going to be an integer because the troubles that we have are internally stored as integers.
OK, and now I can check when the answer is one.
Then I want to set my TV on this score, option one.
Background, to be context Campath.
Thought get drawable and where do I get the trouble from?
Well, first I need to pass in the context and then the trouble with you, and that is going to be to
interpret this pass to this function.
OK, so this function is really just going to take care of assigning the right color, so the right
background to our options.
So if the answer integer is one, then it will be setting the TV option one segment.
OK, now we need to do the same thing for the other options that we have.
So.
Same quote, pretty much TV option, only that it's not TV option one, but it's TV option to.
And so forth.
All right, so here is three and three, and here it's four and.
For.
All right, so that's our interview function.
And now when we press submit.
What we see here, so let's say this is, of course, Brazil, but I like police here and I press submit,
then the correct answer gets a color and the incorrect answer gets a color as well.
So this is incorrect.
In our case, if we had an incorrect answer selected, then it will be read and if we had a correct
answer selected, it will be green and nothing else will be colored.
But if we have an incorrect answer, then of course, the correct answer will also be colored in green.
So that's what we will need to do once we press submit.
So here inside of our unclick, that's where the our ID button submit functionality is to be implemented.
So here I check if the selected option position is zero.
Then.
I want to go ahead and increase that by one.
So increase the current position.
And why would I do that?
Well, why would I even have the situation where the selected option position is zero because the selected
option position by default is going to be.
Zero here.
It starts with zero because we haven't selected anything.
So once we have selected something, the value will be overridden, of course, and we will accordingly
move on with our code.
Right.
But if it's zero, then I want to go to the next question, so to speak.
OK.
And that is done with this position where it's handled with this position.
So I increase the position by one saying that.
I want to go to the next question.
OK, so the selected option position would be, of course, checked and changed once the user presses
on any of the other buttons and then will be assigned, but at the same time, we will reset that.
So the selected option position back to zero once we are done with coloring everything and letting the
user know if he was correct or not.
OK, it's a little confusing, but as soon as we get there, it will make a lot more sense.
All right.
So if we press submit, then there are multiple things that we need to check.
So first of all, we need to check when.
The current.
Position is less or greater than the questions list size, if that's the case.
Then we want to set the next question.
So we want to go ahead and pretty much reset the screen, so when we click on this button, you can
see now the question has been reset.
So the question text has been changed.
The image has been changed.
Of course, the answer options have been changed as well.
So that's what happens in this odd question function and then otherwise.
So if that's not the case, so else what I want to do is.
I want to.
Right, a toast here for now, make text, which is going to be.
You have successfully completed the quest.
So at this point, we later on want to display the finished screen, so to speak, our results screen.
So you have successfully completed the quiz, which should be displayed for only a short.
Period of time, that show.
All right, so that's our toast here.
Nothing fancy, but a general toast.
And we will replace that later on.
All right, so this is our one here, but we had another if surrounding it.
So here at this point, that's where I want to have an else.
So if we do not have a selected option, position zero, it means that we have an option position.
So the user has selected an option.
So what we need to do is we need to create a question here, which will be our questions list,
get em current position.
So we want to get the question at the current position, minus one.
All right, so as we know, the questions list is an array list that really started zero.
So we want to have the first entry or whatever and current position current is so if it's, let's say
five, then we want to have the fifth entry, which is our index for inside of our questions list.
All right.
And that's our question.
Now we can check if the correct answer that is inside of this question.
So question, correct answer is not equal to our selected option position.
And here it's correct answer and not correct answer, and if that's not the same, then we know that
we have selected the wrong answer.
So what all of you should do is it should take the selected option position and draw it accordingly.
So drawable thought and here it was, the wrong option.
Order background should be set.
So the button should be read pretty much.
So if we selected the incorrect answer, that's what happens.
And in any case, what we want to happen is that our answer view with the correct answer is also said
so question correct.
Answer our dot drawable dot.
Correct option border, because if you look at it.
This one here, for example, is Belgium.
So if I like Belgium, then Belgium will be green no matter if it's correct or not, OK, or if our
selection is correct or not, of course.
And in the next example here, let's say I select France, even though this is I think it was Fidge
then.
You can see that my incorrect answer was selected red or the background was said to Red as this if statement
here does, but the correct answer is said to green.
In any case, so it doesn't have to be inside of an IV and health statement, so to speak.
Of course, in the greater scheme of things, it is inside of this, if else, but not in terms of when
we are at the right spot in our code.
And then when we are at the last question, we want to make sure that the button here at the bottom
changes from go to next question to submit so we can check here is my current position is equal to the
M questions list size.
Then the button submit text should be finished and otherwise it should be said to go to next question.
Now, at this point, we have gone through the whole process of assigning the correct color and so forth.
And we go to the next question.
OK, so once we click on once again.
You can see we go to the next question, because here we set the question, but the thing is that will
never happen if we don't set our selected position back to zero.
So that's what I'm doing here.
I'm just going to assign zero here.
All right, and then there is only one last thing to do, and that is to set the submit button accordingly
so you can see we have got the next question and submit.
But this is not what we have in our program yet.
So we need to set that accordingly.
And that is each time that we set a question.
So we need to go to our function question, which is rather far up in my case.
So here in subquestion, after we set the default options to do what we can do is we can check if the
current position is equal to the questions list.
CISE.
And if that's the case, then change the text to finish and otherwise set to text to submit, so button
submit that text is going to be submit.
So each time we have selected the answer, you can see the Texas change to go to next question, but
we need to set it back to submit.
And that's what happens here, except, of course, we are at the last screen.
Then we just want to display finish.
All right, and now at this point, we can test our application and see if all of that is actually implemented
as we expected.
All right, there we are.
Let's test it.
So let's see Argentina, OK, answers, correct, go to next question, and as it seems, we're not
moving over yet.
So let me select Argentina once again.
So we need to make sure that we're actually moving over.
And you may be spotted.
It's exactly this one here and current position.
So we had that earlier for testing purposes, but now it's in our weight.
So we need to get rid of this line and I'll test it again.
Because what it did is it always when it set a new question, it set it back to question number one,
so it's never going to give us the right question, right.
So it's never going to get us the next question, so to speak.
All right.
Let's test it again.
Let's enter a name that's press start and now select Argentina.
Submit, go to the next question and you can see everything works as intended so we can go through our
application as intended.
Let's make a mistake here.
As you can see, still going to work.
And then, of course, here, if we make a mistake, let's say I say this is Georgia, I can still change
it back later on and go to the next question.
So we still have this functionality.
But I wanted to really keep it super simple.
So if somebody wants to trick the system, he can do so.
You could, of course, create an additional helper variable which takes care of the user just answering
once and not being able to change the answer afterwards.
And that can be done by just simply checking each time that the user presses a button if he already
pressed a button inside of this question.
So once that question is called, you could have this variable check here.
All right.
I'd say let's go to the next video.