In this video I complete the Android Translation app! I provide both an Android Text to Speech Example as well as a Speech to Text example. I also review how to use Spinners.
By the end we will be able to say a phrase, translate it into multiple different languages and then press a button and have those translated phrases speak. All of the code follows the video below.
If you value videos like this, help others find out about them by sharing on Google Plus with a click here [googleplusone]
Code From the Videos
MyActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 |
package com.newthinktank.derekbanas.xmltranslates; import android.app.Activity; import android.content.ActivityNotFoundException; import android.content.Intent; import android.os.AsyncTask; import android.os.Bundle; import android.speech.RecognizerIntent; import android.speech.tts.TextToSpeech; import android.text.method.ScrollingMovementMethod; import android.view.View; import android.widget.AdapterView; import android.widget.EditText; import android.widget.Spinner; import android.widget.TextView; import android.widget.Toast; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.params.BasicHttpParams; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserException; import org.xmlpull.v1.XmlPullParserFactory; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.StringReader; import java.io.UnsupportedEncodingException; import java.net.MalformedURLException; import java.util.ArrayList; import java.util.Locale; public class MyActivity extends Activity implements TextToSpeech.OnInitListener { // Define the spoken language we wish to use // You must install all of these on your phone for text to speech // Settings - Language & Input - Text-to-speech output - // Preferred Engine Settings - Install voice data private Locale currentSpokenLang = Locale.US; // Create the Locale objects for languages not in Android Studio private Locale locSpanish = new Locale("es", "MX"); private Locale locRussian = new Locale("ru", "RU"); private Locale locPortuguese = new Locale("pt", "BR"); private Locale locDutch = new Locale("nl", "NL"); // Stores all the Locales in an Array so they are easily found private Locale[] languages = {locDutch, Locale.FRENCH, Locale.GERMAN, Locale.ITALIAN, locPortuguese, locRussian, locSpanish}; // Synthesizes text to speech private TextToSpeech textToSpeech; // Spinner for selecting the spoken language private Spinner languageSpinner; // Currently selected language in Spinner private int spinnerIndex = 0; // Will hold all translations private String[] arrayOfTranslations; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_my); languageSpinner = (Spinner) findViewById(R.id.lang_spinner); // When the Spinner is changed update the currently selected language // to speak in languageSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> adapterView, View view, int index, long l) { currentSpokenLang = languages[index]; // Store the selected Spinner index for use elsewhere spinnerIndex = index; } @Override public void onNothingSelected(AdapterView<?> adapterView) { } }); textToSpeech = new TextToSpeech(this, this); } // When the app closes shutdown text to speech @Override public void onDestroy() { if (textToSpeech != null) { textToSpeech.stop(); textToSpeech.shutdown(); } super.onDestroy(); } // Calls for the AsyncTask to execute when the translate button is clicked public void onTranslateText(View view) { EditText translateEditText = (EditText) findViewById(R.id.words_edit_text); // If the user entered words to translate then get the JSON data if(!isEmpty(translateEditText)){ Toast.makeText(this, "Getting Translations", Toast.LENGTH_LONG).show(); // Calls for the method doInBackground to execute new GetXMLData().execute(); } else { // Post an error message if they didn't enter words Toast.makeText(this, "Enter Words to Translate", Toast.LENGTH_SHORT).show(); } } // Check if the user entered words to translate // Returns false if not empty protected boolean isEmpty(EditText editText){ // Get the text in the EditText convert it into a string, delete whitespace // and check length return editText.getText().toString().trim().length() == 0; } // Initializes text to speech capability @Override public void onInit(int status) { // Check if TextToSpeech is available if (status == TextToSpeech.SUCCESS) { int result = textToSpeech.setLanguage(currentSpokenLang); // If language data or a specific language isn't available error if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) { Toast.makeText(this, "Language Not Supported", Toast.LENGTH_SHORT).show(); } } else { Toast.makeText(this, "Text To Speech Failed", Toast.LENGTH_SHORT).show(); } } // Speaks the selected text using the correct voice for the language public void readTheText(View view) { // Set the voice to use textToSpeech.setLanguage(currentSpokenLang); // Check that translations are in the array if (arrayOfTranslations.length >= 9){ // There aren't voices for our first 3 languages so skip them // QUEUE_FLUSH deletes previous text to read and replaces it // with new text textToSpeech.speak(arrayOfTranslations[spinnerIndex+4], TextToSpeech.QUEUE_FLUSH, null); } else { Toast.makeText(this, "Translate Text First", Toast.LENGTH_SHORT).show(); } } class GetXMLData extends AsyncTask<Void, Void, Void>{ String stringToPrint = ""; @Override protected Void doInBackground(Void... voids) { String xmlString = ""; String wordsToTranslate = ""; EditText translateEditText = (EditText) findViewById(R.id.words_edit_text); wordsToTranslate = translateEditText.getText().toString(); wordsToTranslate = wordsToTranslate.replace(" ", "+"); DefaultHttpClient httpClient = new DefaultHttpClient(new BasicHttpParams()); HttpPost httpPost = new HttpPost("http://newjustin.com/translateit.php?action=xmltranslations&english_words=" + wordsToTranslate); httpPost.setHeader("Content-type", "text/xml"); InputStream inputStream = null; try{ HttpResponse response = httpClient.execute(httpPost); HttpEntity entity = response.getEntity(); inputStream = entity.getContent(); BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8); StringBuilder sb = new StringBuilder(); String line = null; while((line = reader.readLine()) != null){ sb.append(line); } xmlString = sb.toString(); XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); factory.setNamespaceAware(true); XmlPullParser xpp = factory.newPullParser(); xpp.setInput(new StringReader(xmlString)); int eventType = xpp.getEventType(); while(eventType != XmlPullParser.END_DOCUMENT){ if((eventType == XmlPullParser.START_TAG) && (!xpp.getName().equals("translations"))){ stringToPrint = stringToPrint + xpp.getName() + " : "; } else if(eventType == XmlPullParser.TEXT){ stringToPrint = stringToPrint + xpp.getText() + "\n"; } eventType = xpp.next(); } } catch (MalformedURLException e){ e.printStackTrace(); } catch (UnsupportedEncodingException e){ e.printStackTrace(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (XmlPullParserException e) { e.printStackTrace(); } return null; } @Override protected void onPostExecute(Void aVoid) { TextView translateTextView = (TextView) findViewById(R.id.translate_text_view); // Make the TextView scrollable translateTextView.setMovementMethod(new ScrollingMovementMethod()); // Eliminate the "language :" part of the string for the // translations String stringOfTranslations = stringToPrint.replaceAll("\\w+\\s:","#"); // Store the translations into an array arrayOfTranslations = stringOfTranslations.split("#"); translateTextView.setText(stringToPrint); } } // Converts speech to text public void ExceptSpeechInput(View view) { // Starts an Activity that will convert speech to text Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); // Use a language model based on free-form speech recognition intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); // Recognize speech based on the default speech of device intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault()); // Prompt the user to speak intent.putExtra(RecognizerIntent.EXTRA_PROMPT, getString(R.string.speech_input_phrase)); try{ startActivityForResult(intent, 100); } catch (ActivityNotFoundException e){ Toast.makeText(this, getString(R.string.stt_not_supported_message), Toast.LENGTH_LONG).show(); } } // The results of the speech recognizer are sent here protected void onActivityResult(int requestCode, int resultCode, Intent data){ // 100 is the request code sent by startActivityForResult if((requestCode == 100) && (data != null) && (resultCode == RESULT_OK)){ // Store the data sent back in an ArrayList ArrayList<String> spokenText = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); EditText wordsEntered = (EditText) findViewById(R.id.words_edit_text); // Put the spoken text in the EditText wordsEntered.setText(spokenText.get(0)); } } } |
strings.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">XML Translates</string> <string name="hello_world">Hello world!</string> <string name="action_settings">Settings</string> <string name="translate_text_view">Enter the words to translate above</string> <string name="translate_button">Translate</string> <string name="speech_input_phrase">Say the Phrase</string> <string name="stt_not_supported_message">Your Device Doesn\'t Support Speech to Text</string> <string-array name="language_array"> <item>Dutch</item> <item>French</item> <item>German</item> <item>Italian</item> <item>Portuguese</item> <item>Russian</item> <item>Spanish</item> </string-array> </resources> |
activity_my.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MyActivity"> <EditText android:id="@+id/words_edit_text" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Speak" android:id="@+id/button" android:layout_below="@+id/words_edit_text" android:layout_alignParentLeft="true" android:layout_marginTop="16dp" android:onClick="ExceptSpeechInput"/> <Button android:id="@+id/translate_button" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="70dp" android:text="@string/translate_button" android:onClick="onTranslateText" android:layout_below="@+id/words_edit_text" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" /> <Spinner android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/lang_spinner" android:layout_below="@+id/translate_button" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:entries="@array/language_array" android:layout_marginTop="10dp"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Read" android:id="@+id/button2" android:layout_below="@+id/lang_spinner" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:onClick="readTheText"/> <TextView android:id="@+id/translate_text_view" android:text="@string/translate_text_view" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:layout_below="@+id/button2" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" /> </RelativeLayout> |
AndroidManifest.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.newthinktank.derekbanas.xmltranslates" > <uses-permission android:name="android.permission.INTERNET"/> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MyActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> |
Hi Derek,
Great tutorial and nice little app. I watched it but have not built it locally yet.
I was wondering why you said Android does not support all the languages in the TTS list, and you chose only those 6 in question. Why for example the Chinese was not implemented for this tutorial? Sorry if I’m asking a dumb question.
Thanks
Hi Moj,
The Chinese language shows up in the Local class, but not in TTS. They show all the available tts languages here.
I suppose it’s because your web service translator does not extend to these other languages?
Yes you are correct
Sir,can image be send to android via json?
It is best to reference it by url
Derek,
Thank you for all the work you’ve put into the Android Studio tutorials. I am having trouble embedding videos into my app and getting them to play on a device. I have working examples of playing videos from a remote server, but need to include videos in an app. If you can help me get this going, I will save you a warm spot in heaven.
Thanks,
Dan
Derek,
Never mind. I got it working. Thank you so much for all the great tutorials.
You still get the complimentary warm spot in heaven.
Thanks,
Dan
Great I’m glad you fixed it. Sorry I couldn’t get to you quicker.
I’ll try to make a video on that topic this week.
Could you make a Navigation Drawer tutorial. I’m having trouble taking the info from the selected button and inflating the appropriate fragment.
That video will come out very soon