Welcome back in this video, we're going to look at how we can select colors directly in our application.
OK, so this will be.
What we need to implement, we can draw over items and so forth.
So let's get started implementing that into our application and therefore, instead of my drawing view,
I'm going to create a new function.
So I'm going to go to a spot where I want to put it, and I'm going to put it just at the very bottom.
And here I want to have a function which will allow me to set the color.
So fun color, which will require a new color that we put in and it will be of type string.
So we pass this parameter and you color.
And we're going to override the color that we have here at the very top, which is this global variable
here, private of our color that we had created and we assign color black to it.
We're going to take that and we're going to get the color from the string.
So the thing is, if you look at any given color, this year is a string.
OK, so hashtag five zero zero zero zero will be a string.
OK, so it's not surrounded by quotations, but it's treated like a string.
So when we get that value, we can now parse that value into a color.
So there is this thing you can see here color, string of type string so we can actually pass a string
into a color.
So that's pretty cool, right?
And that's what Android Studio offers us for Android development offers us.
And now we just need to change to draw paint color to be the color that we just created.
So this drop paint you might recall the paint class called style and color information about how to
draw geometries, text and quite a bit maps.
So we're using that in order to now draw on to the screen with the color that we have now inside of
our main activity.
We will need to create another method that will take care of actually clicking on the paint.
OK.
So on any of the given colors here, because now we have a function and drawing in view, which allows
us to set the color.
But we also need to call this method and we are going to call it inside of this method here that we're
going to create inside of the main activity.
So I'm going to call this one paint clicked and it will get a view because I want to assign that to
all of my buttons as an on click event.
OK, so first of all, what we can do is we can start with a toast here.
So make text.
This.
Collect paint.
And length long.
So you're toast, length long and short.
OK, so let's just test this by assigning it to our image button here, so I'm going to sign it.
And on the on click position paint clicked like so.
OK, now the same thing goes for this other button on click and paint clicked.
So basically, we will require that for all of our image buttons.
So let me add that in here for each of those buttons.
So on, click paint clicked on, click paint clicked and the last button doesn't need that.
OK, so here now pink click will be used.
Let's test as real quick.
And see if we can click on the color and you see here.
Click to paint.
That's great.
So now, instead of showing the toast went, we tested that it works right.
You can, of course, test it for all buttons just to see if that works.
So the first thing I need to check is if my.
Image button current paint here is not the view that I am clicking on, so only if it is a different
view that I'm, erm, well, that I'm clicking on another button and another image button, then I want
to change the colour.
Otherwise I don't need to do anything right.
So I'm saying if the view is not equal and equal here, image button current paint.
So if that's the case, only if that's the case, I want to add some coat.
So what is it that I need to add here?
Well, first of all, I need to have an image button, which will be the view as an image button.
So I'm taking this view, which is the thing that we are clicking on, and it could be a text view.
I button an image view and so forth.
But in our particular case, we're making sure that only our image buttons have this paint clicked method
assigned to it.
Okay, we have used this approach before with the click event, right?
And what was you're saying is, OK, the view is always going to be an image button, so let's convert
that into an image button and store that inside of this variable.
And then I need the colour text from that image button.
So I'm going to call this variable colour tag and they can get that from my image button that TAC property
and I need to make a string out of it.
So.
You might recall that we created this tag for every single image button, and now I want to read that
tag.
And here does this just the location where the tag is actually at?
It's actually this string here or this value here.
So we are reading this tag color, which is just a string.
And we are then we're assigning it to the colored tech variable that we created here and now we can
just go ahead and call that set color method that we just created and passed the colored tag to it.
All right.
And of course, we also need to make sure that the image is set accordingly.
So the image that is now selected here displayed pressed will be executed.
So we can actually just take this whole part here.
Copy that and put it here with a couple of changes.
So now it's not going to be the current paint where we assign that, but we're going to assign that
to our image button that we currently are working with.
So now we are assigning the public press to it.
And then to the one that we had pressed before.
So the M button or image button current paint that we had assigned before.
We also need to set it Strobl.
But because this is a Nullarbor, we need to add the question mark and we can just do the same thing.
So let's copy that part here and then just not use palette press, but pellet normal.
So what we're saying is the button that would just press on should now become the press button.
So we'll use this trouble that says pellet press and the one that is unselected should be said to pellet
normal.
Now, of course, we need to also store that we made this shift, right?
So now our M image button, the current paint needs to be overwritten with the view that we are getting,
which means with the image button that we currently press on so that this will work the next time again
because otherwise the button would go by step by step.
Once we click on them, each of them would be ending up pressed.
So what we're doing here is we are setting the current button that we clicked on to be pressed.
We are deactivating or making the old button that we had on press, and we are making sure that this
property, which will just hold the current button that we clicked on.
Be ready so that the next time we do this, execution will still do the trick and will allow us to make
this run.
So let's check it out.
Let's see if the paint clicked will be executed and we can see that it actually is being executed.
So all of those beautiful colors are working flawlessly.
So now a little challenge for you.
Please go ahead and add a white color here as a final option or is another option here on the right
hand side and then make it clickable and accordingly.
While you won't need to make a clickable because our code works perfectly already, so we can basically
just copy the other image buttons and then just use it to basically draw white paint on top of what
we have here.
So it's not going to you the other paint, but it's going to be like somebody draw drew, for example,
graffiti on the wall and you need to paint over it with white color.
So that's what would happen here.
OK, so I hope you pass the video and you tried it.
So interactivity, man, I'm just going to add another image button here, and I'm going to use the
color white here.
And here I'm going to use White as well, and then I just need to create this color white inside of
my colors.
So let's jump over to colors.
Let's create another color.
Let's call this one white.
And White will just be a bunch of F's six f's here will do the trick, you see, we have white now and
then this error disappears.
That's great.
And that should pretty much be it.
So let's check it out.
If we can now have this additional color, you see we have this additional color here.
Let me draw something and I'll let me use white and draw over it.
So you can see is as simple as that.
That's how easily you can add new colors to it.
And now, by the way, let's say you wanted to add a color picker to your application.
I would recommend you follow this tutorial.
All right.
So geeks for geeks how to create a color picker tool in Android using color, wheel and slider.
So it's going to really allow you to basically just select a color, see the tax code and all of the
good stuff, and then imported into your application where you will then use this color that you're
getting from there.
You just need to get the string color and then use it in the format that we have used in our applications,
or basically just assign it at the point where you set the color.
OK.
So that's pretty much it.
I hope you enjoyed this video and the next one, we're going to add the background image to our application
so that we can try to draw on top of it because we're going to be such amazing draws.
All right, so see you there.