I am using ML kit's on device translation API to translate a text.Although the translation is working perfectly fine but I am unable to display the translated text in my text view.I tried using setText method but nothing is being displayed in text view.Everything is being displayed in the logcat.
I am working in Android Studio
edit: This shows the model being downloaded
TranslatorOptions options =
new TranslatorOptions.Builder()
.setSourceLanguage(TranslateLanguage.ENGLISH)
.setTargetLanguage(TranslateLanguage.BENGALI)
.build();
final Translator englishBengaliTranslator =
Translation.getClient(options);
//DOWNLOAD THE MODEL
DownloadConditions conditions = new DownloadConditions.Builder()
.requireWifi()
.build();
englishBengaliTranslator.downloadModelIfNeeded(conditions)
.addOnSuccessListener(
new OnSuccessListener() {
@Override
public void onSuccess(Object o) {
}
//@Override
public void onSuccess(Void v) {
// Model downloaded successfully. Okay to start translating.
// (Set a flag, unhide the translation UI, etc.)
Toast.makeText(getApplicationContext(), "Model Downloaded.Translation will start ", Toast.LENGTH_SHORT).show();
}
})
.addOnFailureListener(
new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
// Model couldn’t be downloaded or other internal error.
// ...
Toast.makeText(getApplicationContext(), "Model could not be Downloaded. ", Toast.LENGTH_SHORT).show();
}
});
In this snippet of the code I have called the translate function (as the model is now downloaded)
englishBengaliTranslator.translate(stringResult)
.addOnSuccessListener(
new OnSuccessListener() {
@Override
public void onSuccess(Object o) {
}
//@Override
public void onSuccess(@NonNull String translatedText1) {
textViewTranslatedText.setText(translatedText1);
}
})
.addOnFailureListener(
new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
// Error.
// ...
Toast.makeText(getApplicationContext(), "Text could not be translated. ", Toast.LENGTH_LONG).show();
}
});
I have used the setText function but still the translated text gets displayed in logcat and not in the textview(The translation works completely fine).How do I display it in my textView instead of logcat?Any leads would be helpful!
here is the image of logcat where translated text is displayed:enter image description here
Try this code,
englishBengaliTranslator.downloadModelIfNeeded(conditions)
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
// Model downloaded successfully. Okay to start translating.
// (Set a flag, unhide the translation UI, etc.)
Toast.makeText(getApplicationContext(), "Model Downloaded.Translation will start ", Toast.LENGTH_SHORT).show();
}
})
.addOnFailureListener(
new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
// Model couldn’t be downloaded or other internal error.
// ...
Toast.makeText(getApplicationContext(), "Model could not be Downloaded. ", Toast.LENGTH_SHORT).show();
}
});
The above code does nothing. I have just removed the unnecessary interface.
But the below code does something. I mean it specifies the return type.
englishBengaliTranslator.translate(stringResult)
.addOnSuccessListener(new OnSuccessListener<String>() {
@Override
public void onSuccess(String s) {
if(textViewTranslatedText!=null)
textViewTranslatedText.setText(translatedText1);
else
Log.d(TAG, "onSuccess: translation Done but text view null");
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
//Handle onFailure
}
});