‫Hello, my friends.
‫Now let's start adding some functionalities to the buttons and the Spinners.
‫So, first of all, let's add the translate button or the button.
‫BTN Dot set on Click Listener, New View and Click Listener.
‫And here we are going to add the functionality.
‫So the translated TV dot set text to NULL.
‫So whenever the user click on this button, we start by adding that the translated TV set text do not
‫then this is the initialization if the source source edit text does not get text to string dot is empty.
‫If the user didn't enter anything and he clicked on translate the button.
‫We need to display a toast message that tells him to add and enter some text.
‫So here please enter your text.
‫LS If we are covering all the cases that we have like errors, like empty text, like selecting the,
‫the user didn't select the source language or the, the user didn't select the target language.
‫So we need to cover all of these cases.
‫So from language code does is equal to is empty.
‫Then we need to display a toast message also telling him that we need to select please select source
‫language.
‫Else if the user didn't select the target language or two language code, so too language
‫is empty.
‫Then what we need to make is the display also a toast message telling him that the two please.
‫Select the language to translate or the targeted and language.
‫Otherwise don't make any toast message.
‫Just make it translate the text and call the translate.
‫Text method, we will pass the from language code and the to language code the source edit text not
‫get text to string.
‫So we are going to create a function that takes the from language code and the two language code and
‫the text that we need to display and to translate.
‫So we create the method inside the main activity and here we go.
‫It took three parameters the of type string, the two language, the from language and the string that
‫we need to translate.
‫I will name it as our RC.
‫And here we start translated to text.
‫View dot set text.
‫To downloading.
‫Language models.
‫And we start by translator option, translator options.
‫This is what we have talked about.
‫This is the options object that is created from the translator options.
‫And here we start by making like displaying and setting the data from the source language and the targeted
‫language.
‫So how to do that?
‫We start by making a new translator options dot builder
‫and we set here that try and catch and there is remove qualifier and set this is the here we should
‫put the qualifier new and everything is working fine.
‫Now the builder does set the source language to from language code that we are passing it from inside
‫the parameter set the target language which is the second parameter that we.
‫Are passing inside this method and here we use the build method.
‫I surrounded it by try and catch.
‫Which is good in order to prevent any error.
‫Now we start by translator.
‫Translator plus enter to import a package from Google ML kit and natural language translate and not
‫this.
‫So please select the ML equal to natural language.
‫And this is in the previous codes we put natural language.
‫It is in the deprecated API, epic or API.
‫So how to get it?
‫We start by translation.
‫Dot get client and here we pass the options that we have created.
‫This is the translation now.
‫Model download.
‫Manager.
‫We can make this and we can put conditions, conditions, download conditions, conditions equal to
‫new conditions.
‫Download conditions and we pass the builder
‫that builder dot built
‫now.
‫Translator dot download model.
‫If needed, we start past the parameter conditions and add on success listener new on success listener.
‫And we start making here the functionality that we have when when we get all of the data correct, like
‫left from language that do language code.
‫And the source code that we need are the source text that we need.
‫Let's continue with the own success method.
‫So translated DV, which is the text of you set text and we will set a translating temporary text.
‫Now after success make the translator to translate.
‫Give him a job to translate.
‫And here the parameter we need to pass is type string.
‫So what is the data that we need to translate?
‫It's a type string and we have passed it with the CRC parameter.
‫So CRC does add on success listener, new on success listener and on success listener.
‫Here also the translate the TV set text.
‫To se or which is the string as.
‫Now add on failure listener new on Failure on failure listener on failure.
‫We will display a toast message for the user telling him that it is failed to translate.
‫So here fail to translate.
‫It's very simple now.
‫There is an own success here.
‫We can also add add on failure listener here.
‫So if you want, I can add on failure listener because we have two success listeners, the first one
‫and the second one you see now we have on Failure Listener and the second on Failure Listener here in
‫order and in order to display for the user a message, just message telling him that it is failed to
‫download the language.
‫So this is this is a failure like related to the download stuff and this is related to the API and the
‫kit, the translate kit in the meal kit Firebase.
‫So this is for the download failure and this is for the translation failure.
‫Fail to download the language.
‫It's very simple.
‫Now everything I think is working fine.
‫Now let's save the project.
‫And in the next video, we are going to run the app and see if we.