I'm creating a translator app for android using fragments (because I have tabs).
I found some good tutorials for translation with Microsoft's API, but none of them worked, until I found this.
As an activity it works, when I change it to a fragment, it doesn't.
Here's the code after a small change:
public class HomeFragment extends Fragment
{
TextView text;
String translatedText;
View rootView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_home, container, false);
text=(TextView)rootView.findViewById(R.id.translatedtext);
text.setText("<This text should change after translation has occurred in AsyncTask>");
new MyAsyncTask() {
protected void onPostExecute(Boolean result) {
text.setText(translatedText);
}
}.execute();
return rootView;
}
class MyAsyncTask extends AsyncTask<Void, Integer, Boolean>
{
@Override
protected Boolean doInBackground(Void... arg0) {
Translate.setClientId("MicrosoftTranslatorJavaAPI");
Translate.setClientSecret("0VHbhXQnJrZ7OwVqcoX/PDZlyLJS9co3cVev1TPr8iM=");
try {
translatedText = Translate.execute("I should probably set this to something a little less profane", Language.ENGLISH, Language.FRENCH);
} catch(Exception e) {
translatedText = e.toString();
}
return true;
}
}
}
The XML currently is just a simple TextView (like the Github example).
What do I have to change to make asynctask work?
I figured the solution weeks ago, but just now thought of sharing this. The solution was somewhat simple and right under my nose. The API microsoft-translator-java-api-0.6.1-jar-with-dependencies in my project, I added manually and it was just "floating" inside my project folder. When I inserted it into the libs folder manually, it then appeared inside Android Private Libraries. Now the translation worked!