Hello there and welcome back.
So in the previous media, we have changed in the style of our contextual action mode.
So, for example, when we along press on one of those recipes, you will see that we have changed the
status bar and this action bar for our contextual action mode, and it works perfectly fine.
So in this video, we want to change the style of our recipe when it is selected.
So, for example, when we long press on one of those recipes, we want to be able to change the style
of of that same recipe.
And after that, when we select another one, for example, we can change its style as well.
So basically, we want to create something similar to this design, which you can see here.
Okay, so let's open up our Android studio.
And here on top of our favorite recipes, Adaptor, I want to create two new variables.
So the first variable should be named multi selection.
So private var Malti selection and its default, the value will be false.
So this multi selection will have a type of a Boolean and Downbelow.
I need to create one more.
So private var and this one will be named selected.
The recipes are okay.
And here I'm going to use a realist and the type will be a favorites entity.
Okay, so this one.
OK, so now I need to create two functions which will basically change the style of, of a one year
old recipe.
So let me scroll down below and here I'm going to create those two functions.
So the first function should be named appli selection.
So private fun applier selection.
This function will have two parameters.
The first one is my view holder.
So holder of a type, my view holder.
So this one in the second parameter will be the type of favourite's entity.
So I went to name that current recipe of a type favourite's entity.
OK, so inside this function, I'm going to call this selected the recipe variable from above and basically
inside this new function.
I'm going to use this selecting the recipes to actually add or remove items from this area list.
OK.
OK, so let me scroll down below and here I'm going to write if blog.
So if selecting the recipes contains this current recipe, then we want to basically remove that current
recipe from our selected recipes.
So not remove in here type current recipe.
And in our blog we want to add a new item inside our selected recipes array.
So add current recipe.
OK, so don't worry if you're still confused, I need to create a few more things here and everything
will make more sense to you.
So we're going to recap that at the end of this video once more so down below.
I need to create one more function and that function should be named the change the recipe style.
So private fun, change the recipe style and at this function will take three parameters.
And the first one will be my view.
Holder.
So Holder, my view holder.
I came.
The second one will be a background color arc of a type integer and the third one a strong color of
a type integer as well.
So inside this function, I want to change the color of Favorita roll background.
So let me first open this out.
So favorite recipes roll out.
So basically I want to change the color of this constraint layout and I want to change the color of
this material kind view.
So as you can see, this material kind view has a light gray color for this stroke and this constraint
layout basically have a white color for its background.
And we're going to change that here.
So I'm going to use a holder to get the reference of item view then, uh, favorite recipes roll out.
And that's the same idea from our, uh, constraint layout here.
So favorite recipes roll and the idea for our material review is a favorite roll kind view.
OK, so first let's here change the background color.
So dot set background color in here.
We need to pass our actual background color.
So for that I need to use the context compared though to get color.
So here require activity.
And here let's pass our background color, OK, and down below we need to change the straw color.
So hold the item, view that favorite role, current view in here.
I'm going to use a straw color, OK, and belonging to the context.
But again and here I'm going to pass a straw color.
OK, so this is a change.
Recipe style function will be called inside our applied selection two times in if and L's block.
OK, so now I'm going to call this first here.
So change recipe style.
And here is the first parameter.
I'm going to pass this holder from this.
Selection function, then the second parameter is the actual background color.
And here I'm going to choose our dot color and dot car, the background color.
And this is actually a white color and is a strong color.
I'm going to yours are dot color, dot straw color.
And this actual color is a light medium gray.
So basically the same color as we have inside.
Our favorite recipes are all out.
So those are different colors.
And now I'm going to call this function inside this Alice block in here.
I'm going to type a new colors, colors, which will be similar to this example right here.
So that color will be this color primary and this background quality of our constraint layout will be
actually a 10 percent capacity of our color primary.
OK, so you will see it.
Now, let's call this a change.
The recipe style right here.
Let's say there is a first parameter is a second that we need to pass a new color or color that car,
the background, a light color.
So I'm going to create that color inside a color or sex symbol.
So I'm going to create that color right here.
So the background, light, color.
And for this color, I'm going to choose color primary code.
So I'm going to copy that code right here and I'm going to add the number ten in front of that.
So it will mean that I'm going to take only 10 percent opacity of that color.
OK, and as straw color, I'm going to choose our color, not color primary.
So this one.
And finally, we need to call our Uplay selection function inside those two single click and long click
listener.
So first, we're going to handle this single click listener.
And here I'm going to add if Borke and I'm going to say if Morty's selection is true and only if that's
true, I'm going to call this applied selection function in here.
I'm going to pass the actual holder.
And here is the second parameter.
I need to pass their current recipe.
So I'm going to rename this, selected the recipe into our current recipe and it will make more sense.
And we're going to avoid confusion with this selecting the recipes.
So that's why I'm going to refactor that.
So press shift plus five, six, and let's rename this to current recipe.
OK, press enter.
And now we're going to pass this current recipe right here.
And I'm going to also add that ALS blocker right here and in this block.
I'm going to cut this call from below and paste inside it.
So basically we're going to apply the selection or that style for our recipe only if a multi section
is true.
And we're going to change this multi selection value to draw a little bit later.
But before that, I'm going to handle this line click listener as well.
So here inside, I'm going to also create your block.
I'm going to say if Malti selection is false and if it is false, then I want to set the selection to
true and I want to cut those two lines of code inside here.
And also I want to apply selection function right here.
So I'm going to pass Holder here and the current recipe as well.
And of course, in our block, I'm going to set multi selection to false and I'm going to return false
here.
So basically, as you can see, our default value for our multi selection is false.
So that's its default value.
And whenever we press or a long click on our recipe, we're going to change its value to draw.
And as you can see here in our F block says, if our multi selection is false and the value of this
multi selection by default is false, and basically whenever we along press, we're going to change
its value to true, then we're going to open up our action mode and we're going to apply selection.
And in our block, of course, if our model selection is true, then we're going to return this to false.
And inside our a single click listener, we are also applying this style only if our multi selection
is true.
So that means that we are not going to basically accidentally navigate to our details activity when
when we are making a selection on our in our list.
And you will see that right now.
But before that, I just want to add a few lines of code here.
And first, that inside is on this Troy action mode.
I need to select multi selection to to false.
And also I need to clear our area named selected the recipes.
So let's select the recipes dot clear.
And basically we're going to remove all items from our selected recipes array.
And that's only going to happen when we actually close our action mode.
We're going to continue working on our favorite recipes adapter furthermore.
But now let's run our application and let's check it out.
OK, so let's go to favorites here.
Now, let's long click on our recipe and you will see that we have changed the style of our recipe right
here.
So as you can see, the straw Kweller is now the primary and the background color of this recipe is
actual 10 percent of our color.
Primary, as we specified that inside our caller, a sex symbol right here, so 10 percent of our primary.
And as you can see now, if we try to go back there, you can see our action mode there will disappear.
But the style of this recipe will not change.
So we need to change that as well.
So for that to let me create a new variable on the top.
So here I am to create a new private via my view holders in here, I'm going to create an array list
of my viewer holder.
So basically in this on a binder view holder, I want to add this holder into my my view holder as a
realist.
So my view Holder is not add in here.
I'm going to pass Holder arc and now I'm going to go to this one destroyer action mode.
And here I'm going to call my if you're hoarder's and I'm going to use the foreach and here I'm going
to name this holder.
So for each holder, I'm going to call this change recipe's style and I'm going to Pesquera.
Holder is the second parameter I want to pass are dot color, dot com, the background color.
And as a parameter, we need to pass our color strong color.
So now let's run the app again.
And now basically whenever we press that button from our contextual action mode, we should be able
to change the style of our.
So let's go to Favourite's here.
Let's click on this recipe.
And now let's press this BECCARO And now you will see that the selection has gone.
OK, so as you can see, when we long click, this style changes.
And when we press this back error, it changes back.
And even if we a long click on this recipe and then instead of this BECCARO, we press this button,
it will work again.
OK, so now let me go to recipes and I'm going to add one more recipe inside our favorite recipes table.
So I'm going to edit this one.
So go back, OK?
OK, so now I'm going to click on this one and now I'm going to single click onto this one.
And now, as you can see, both of those recipes got this and your style.
And now if I press a single click again on this one, you will see that this style disappeared.
And now when I click again, it will appear and now it will disappear.
So it works perfectly fine.
And now if I want to untracked or I did select this recipe, which isn't the best recipe, there is
a selected right here, as you can see, it will change its style back, but this contextual action
mode will not disappear.
And then we're going to figure that in the next videos.