Question: im creating a translation application using android studio version 2.3 and im using google translate API. i have implemented the API but for some reason

im creating a translation application using android studio version 2.3 and im using google translate API. i have implemented the API but for some reason its not working.

THIS IS MY MAIN ACTIVITY

package ng.edu.baze.com313.qtrtwo2017.project; 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.view.View; import android.widget.Button; import android.widget.ImageButton; import android.widget.TextView; import com.google.cloud.translate.Translate; import com.google.cloud.translate.TranslateOptions; import com.google.cloud.translate.Translation; import java.util.ArrayList; import java.util.Locale; import android.os.Handler; public class MainActivity extends Activity implements TextToSpeech.OnInitListener { private static final int REQ_CODE_SPEECH_INPUT = 100; //definition of variables  private ImageButton button; // private Button tbutton;  private TextView textView, textView2, textView3; private Button button2; private TextToSpeech tts; final Handler textViewHandler = new Handler(); private static final String API_KEY = "AIzaSyC6L6iBWUlGrHrK0wg0NjHGyXJ9YazJVhk"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = (TextView) this.findViewById(R.id.textView); textView2 = (TextView) this.findViewById(R.id.textView2); textView3 = (TextView) this.findViewById(R.id.textView3); button2 = (Button) this.findViewById(R.id.button2); button2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { final Thread thread = new Thread(){ public void run(){ try{ Thread.sleep(500); } catch(Exception e){ e.printStackTrace(); } finally{ tts.speak(textView2.getText().toString(), TextToSpeech.QUEUE_ADD, null); }} }; thread.start();} }); button = (ImageButton) this.findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { startVoiceInput(); } }); tts = new TextToSpeech(getApplicationContext(), this); tts.setLanguage(Locale.UK); new AsyncTask() { @Override protected Void doInBackground(Void... params) { TranslateOptions options = TranslateOptions.newBuilder() .setApiKey(API_KEY) .build(); Translate translate = options.getService(); final Translation translation = translate.translate("Hello World", Translate.TranslateOption.targetLanguage("ha")); textViewHandler.post(new Runnable() { @Override public void run() { if (textView3 != null) { textView3.setText(translation.getTranslatedText()); } } }); return null; } }.execute(); } private void startVoiceInput() { Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault()); intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Hello, How can I help you?"); try { startActivityForResult(intent, REQ_CODE_SPEECH_INPUT); } catch (ActivityNotFoundException a) { } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); switch (requestCode) { case REQ_CODE_SPEECH_INPUT: { if (resultCode == RESULT_OK && null != data) { ArrayList result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); textView2.setText(result.get(0)); } break; } } } @Override public void onInit(int i) { } } 

THIS IS MY GRADLE

apply plugin: 'com.android.application'  android { compileSdkVersion 25 buildToolsVersion "27.0.3"  defaultConfig { applicationId "ng.edu.baze.com313.qtrtwo2017.project"  minSdkVersion 19 targetSdkVersion 25 versionCode 1 versionName "1.0"  testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"  multiDexEnabled true  } buildTypes { release { minifyEnabled false  proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'  } } packagingOptions { exclude 'META-INF/LICENSE'  exclude 'META-INF/io.netty.versions.properties'  exclude 'META-INF/INDEX.LIST'  } } android { configurations.all { resolutionStrategy.force 'com.google.code.findbugs:jsr305:1.3.9'  } } dependencies { compile fileTree(include: ['*.jar'], dir: 'libs') androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations'  }) compile 'com.android.support:appcompat-v7:25.+'  compile 'com.android.support.constraint:constraint-layout:1.0.2'  compile('com.google.apis:google-api-services-translate:v2-rev47-1.22.0') { exclude group: 'com.google.guava'  } compile('com.google.cloud:google-cloud-translate:0.5.0') { exclude group: 'io.grpc', module: 'grpc-all'  exclude group: 'com.google.protobuf', module: 'protobuf-java'  exclude group: 'com.google.api-client', module: 'google-api-client-appengine'  } testCompile 'junit:junit:4.12'  compile 'com.google.android.gms:play-services:11.0.4' } 

THIS IS MY ANDROID MANIFEST

xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"  package="ng.edu.baze.com313.qtrtwo2017.project"> <uses-permission android:name="android.permission.INTERNET" /> <application  android:allowBackup="true"  android:icon="@mipmap/ic_launcher"  android:label="@string/app_name"  android:roundIcon="@mipmap/ic_launcher_round"  android:supportsRtl="true"  android:theme="@style/AppTheme"> <meta-data  android:name="com.google.android.gms.vision"  android:value="@integer/google_play_services_version"> meta-data> <meta-data  android:name="com.google.android.geo.API_KEY"  android:value="@string/API_KEY">meta-data> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> intent-filter> activity> application> manifest>

THIS IS MY ACTIVITY MAIN

xml version="1.0" encoding="utf-8"?> <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:background="@drawable/images"  android:orientation="vertical" > <TextView  android:id="@+id/textView"  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:layout_alignParentTop="true"  android:layout_centerHorizontal="true"  android:layout_marginTop="100dp"  android:textColor="@color/colorPrimaryDark"  android:textSize="26sp"  android:textStyle="normal" /> <LinearLayout  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:layout_alignParentBottom="true"  android:layout_centerHorizontal="true"  android:layout_marginBottom="60dp"  android:gravity="center"  android:orientation="vertical"  android:id="@+id/linearLayout"> <ImageButton  android:id="@+id/button"  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:background="@null"  android:contentDescription="@string/say_it_back"  android:src="@drawable/microphone" /> <TextView  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:layout_marginTop="10dp"  android:text="@string/tap_on_mic"  android:textColor="@color/colorPrimaryDark"  android:textSize="15sp"  android:textStyle="normal" /> LinearLayout> <TextView  android:id="@+id/textView2"  android:layout_width="wrap_content"  android:textColor="#ffffff"  android:layout_height="wrap_content"  android:layout_alignParentTop="true"  android:layout_centerHorizontal="true"  android:layout_marginTop="31dp"  /> <Button  android:id="@+id/button2"  android:text = "@string/say_it_back"  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:layout_alignBottom="@+id/textView"  android:layout_centerHorizontal="true"  android:layout_marginBottom="17dp"  /> <TextView  android:id="@+id/textView3"  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:layout_below="@+id/textView"  android:layout_centerHorizontal="true"  android:text="TextView" /> RelativeLayout>

I WANT TO TALK TO THE APPLICATION IN ENGLISH USING SPEECH. THEN THE HAUSA TEXT WILL BE WRITTEN. HAUSA IS A NATIVE LANGUAGE IN MY COUNTRY. THEN THERE IS A BUTTON THAT SAYS 'SAY IT BACK' THAT WHEN I CLICK ON IT, IT SHOULD SAY THE TRANSLATED VERSION. THIS IS WHAT IVE DONE SO FAR

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!