Advertisement
Guest User

mariss

a guest
Dec 18th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.95 KB | None | 0 0
  1. package com.example.xps.watsonlanguagetranslator;
  2.  
  3. import android.graphics.Bitmap;
  4. import android.graphics.BitmapFactory;
  5. import android.os.Handler;
  6. import android.support.v7.app.AppCompatActivity;
  7. import android.os.Bundle;
  8. import android.view.View;
  9. import android.widget.Button;
  10. import android.widget.EditText;
  11. import android.widget.TextView;
  12.  
  13. import com.ibm.watson.developer_cloud.language_translator.v2.LanguageTranslator;
  14. import com.ibm.watson.developer_cloud.language_translator.v2.model.TranslateOptions;
  15. import com.ibm.watson.developer_cloud.language_translator.v2.model.TranslationResult;
  16. import com.ibm.watson.developer_cloud.language_translator.v2.util.Language;
  17.  
  18. import org.json.JSONException;
  19. import org.json.JSONObject;
  20.  
  21. public class MainActivity extends AppCompatActivity {
  22. Button button;
  23. TextView textView;
  24. EditText editText;
  25. Handler handler;
  26.  
  27. @Override
  28. protected void onCreate(Bundle savedInstanceState) {
  29. super.onCreate(savedInstanceState);
  30. setContentView(R.layout.activity_main);
  31. button=(Button)findViewById(R.id.b1);
  32. handler=new Handler();
  33. editText=(EditText)findViewById(R.id.text1);
  34. textView=(TextView)findViewById(R.id.text2);
  35.  
  36. button.setOnClickListener(new View.OnClickListener() {
  37. @Override
  38. public void onClick(View view) {
  39. String textTo=editText.getText().toString();
  40. new Thread(new ImageLoaderTask(textView,textTo)).start();
  41. }
  42. });
  43.  
  44. }
  45. class ImageLoaderTask implements Runnable {
  46. TextView textView;
  47. String textTo;
  48. public ImageLoaderTask(TextView textView,String textTo) {
  49. this.textView = textView;
  50. this.textTo=textTo;
  51. }
  52.  
  53.  
  54.  
  55.  
  56. @Override
  57. public void run() {
  58. // Message msg=handler.obtainMessage(1,progressBar);
  59. final LanguageTranslator languageTranslator=new LanguageTranslator();
  60. languageTranslator.setUsernameAndPassword("99f0937c-0708-4992-9982-aca94c96bcf1","tReUbJaGNEbb");
  61. TranslateOptions translateOptions=new TranslateOptions.Builder().addText(editText.getText().toString()).source(Language.ENGLISH).target(Language.ARABIC).build();
  62. final TranslationResult translationResult=languageTranslator.translate(translateOptions).execute();
  63. handler.post(new Runnable() {
  64. @Override
  65. public void run() {
  66.  
  67. try {
  68. textView.setText(new JSONObject(translationResult.getTranslations().get(0).toString()).getString("translation"));
  69. } catch (JSONException e) {
  70. e.printStackTrace();
  71. }
  72. //Message msg=handler.obtainMessage(2,progressBar);
  73. }
  74. });
  75. }
  76.  
  77.  
  78.  
  79. }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement