javaandroidstringsettextyandex-api

How to setText of a Public String


   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


Solution

  • 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);