1
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      Translate.main(null);
      TextView textElement = (TextView) findViewById(R.id.this_is_id_name);
      textElement.setText(message);
      //leave this line to assign a specific text
    }
    // Separate Class calling Translate
    public final class Translate extends YandexTranslatorAPI {
      public final static String message = com.mohsen.transl.Translate.translation";
      //prevent instantiation
      private Translate(){};
      public static void main(String[] args) {
        try {
          setKey(ApiKeys.YANDEX_API_KEY);
          String translation = Translate.execute("The quick brown fox jumps over the lazy dog.", Language.ENGLISH, Language.SPANISH);
          System.out.println("Translation: " + translation);
        } catch (Exception e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
      }
    }

I'm trying to call the translate method and store the string translation return value into a public string message and display using my main oncreate in MainActivity Class.

I'm thinking that's the simplest form. Please help. enter image description here

  • If `message` is final you won't be able to change it. And there's a quote missing in the declaration of `public final static String message`, – laune Aug 23 '15 at 12:54
  • public final static String message="com.mohsen.transl.Translate.translation"; wouldnt it change as .translation variable changes? – Mohsen Mouralli Aug 23 '15 at 13:08
  • public final static String message=........ should be in the main class file. i corrected that. Its compiling but im getting the tag : com.mohsen.transl.transl.Translate.translation as my text.. I want whats in that tag – Mohsen Mouralli Aug 23 '15 at 13:24

1 Answers1

2
class Translate extends YandexTranslatorAPI {
    public static String message;
    //prevent instantiation
    private Translate() {
    }
    public static void main(String[] args) {
        try {
            setKey(ApiKeys.YANDEX_API_KEY);
            String translation = Translate.execute("The quick brown fox jumps over the lazy dog.", Language.ENGLISH, Language.SPANISH);
            message = translation;
            System.out.println("Translation: " + translation);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

and in your onCreate() method

textElement.setText(Translate.message);
Kashyap Kansara
  • 405
  • 4
  • 10
  • I think that works. only im not getting anything in return. looks like i have to check translate class again. and debug. but for clarification: the other way i was trying is declaring public final static String message = "com.mohsen.transl.translate.Translate.translation"; i was geting the following on the display : com.mohsen.transl.translate.Translate.translation --obviously not what i wanted . changing it to Translate.message i think works . i will be posting another question regarding calling Translate (null) – Mohsen Mouralli Aug 23 '15 at 13:36