Hello Everyone !!!
Today we are going to implement Three functionality in android application.
First functionality : Voice to Text
Second functionality: Text to Voice
Third functionality: Search Text Meaning
Problem Statement:
Develop an android app in which user will enter a word via keyboard or voice to find meaning of word ,help to pronounce the word.
Final Output:
So, Let’s Start with building layout for App :
List down the functionality we are providing in App–
- Taking input text from keyboard.
- voice to text
- Text to voice
- Search meaning of word
Preparing Layouts:
- Taking input Text from keyboard: Using EditText View ,we can have text as input
- Voice to Text, Text to Voice, Search the word: We need to have a Button View for the user to press to use functionality .
- Now we will do grouping of similar categories.
- We are accepting text from keyboard and voice
- We are processing the text for meaning and pronunciation
Home Screen Layout :
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context="com.technogrill.apps.app2.VcomboHome" tools:showIn="@layout/activity_vcombo_home" android:background="@drawable/backgndimg"> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/selectwordsection" android:layout_margin="10dp" android:background="@drawable/backgndshape"> <TextView android:textColor="#faf3faf5" android:layout_marginTop="10dp" android:text="Enter Word" android:textSize="30dp" android:layout_centerHorizontal="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/enter_word_id" /> <Button android:id="@+id/inputtextvoice" android:layout_margin="10dp" android:layout_width="50dp" android:layout_height="50dp" android:layout_below="@+id/word_entry_id" android:background="@drawable/ic_keyboard_voice_white_24dp" android:layout_centerHorizontal="true" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="15dp" android:layout_below="@+id/inputtextvoice" android:layout_centerHorizontal="true" android:layout_marginBottom="15dp" android:text="Tap mic to speak word." android:id="@+id/speaktap"/> <android.support.design.widget.TextInputLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/enter_word_id" android:id="@+id/word_entry_id" android:layout_margin="10dp"> <EditText android:textColor="#faf3faf5" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="Type The Word Here..." android:focusable="true"/> </android.support.design.widget.TextInputLayout> </RelativeLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/backgndshape" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:layout_marginTop="12dp" android:layout_below="@+id/selectwordsection" android:layout_alignStart="@+id/selectwordsection"> <Button android:id="@+id/btn_pronunce" android:textColor="#faf3faf5" android:textStyle="italic" android:background="@drawable/pronounce" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_alignParentStart="true" android:layout_marginStart="44dp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignStart="@+id/btn_pronunce" android:layout_below="@+id/btn_pronunce" android:text="Tap to pronounce" android:id="@+id/textView2" android:layout_margin="10dp"/> <Button android:textColor="#faf3faf5" android:textStyle="italic" android:background="@drawable/meaning" android:id="@+id/btn_meaning" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginEnd="58dp" android:layout_alignBaseline="@+id/btn_pronunce" android:layout_alignBottom="@+id/btn_pronunce" android:layout_alignParentEnd="true" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignStart="@+id/btn_meaning" android:layout_below="@+id/btn_meaning" android:layout_margin="10dp" android:text="Tap to see meaning" /> </RelativeLayout> </RelativeLayout>
Shape.xml for Relative Layout:
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <corners android:radius="5dp"/> <size android:height="100dp" android:width="100dp"/> <stroke android:color="#000000" android:width="2dp"/> </shape>
Now Let’s move towards the feature implementation..
Part 1: Link Layout view’s with the class objects.
Button btnpronunce=(Button)findViewById(R.id.btn_pronunce); Button btnmeaning=(Button)findViewById(R.id.btn_meaning); EditText editText=((TextInputLayout)findViewById(R.id.word_entry_id)).getEditText(); Button tb=(Button)findViewById(R.id.inputtextvoice);
For Text-to-Speech we are going to use :
tts=new TextToSpeech(VcomboHome.this,this);
Part 2: We are going to use two Listeners :
- Button Click Listeners
btnpronunce.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { } });
btnmeaning.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { } });
tb.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { } });
- Text-To-Speech OnInitListener: Implement the TextToSpeech.OninitListeners and override the onInit() method:
public class VcomboHome extends AppCompatActivity implements TextToSpeech.OnInitListener
@Override public void onInit(int code) { if(code==TextToSpeech.SUCCESS){ tts.setLanguage(Locale.getDefault()); }else{ tts=null; Toast.makeText(this,"Failed to initialize TTS engine.",Toast.LENGTH_SHORT).show(); } }
Part 3.1: Implement functionality voice to text:
tb.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent=new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,"en-US"); try{ startActivityForResult(intent, RESULT_SPEECH); editText.setText(" "); }catch(ActivityNotFoundException a){ Toast.makeText(getApplicationContext(),"Your device doesn't support voice to text",Toast.LENGTH_SHORT).show(); } } });
This button will intent to ACTION_RECOGNIZE_SPEECH and re-back with results which also includes the text.so we need to have onActivityResult()
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); switch(requestCode){ case RESULT_SPEECH:{ if(resultCode==RESULT_OK && null != data){ ArrayList<String> text=data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); editText.setText(text.get(0)); } break; } } }
Part 3.2: Implement functionality text to voice :
btnpronunce.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if(tts!=null){ txt=editText.getText().toString(); if(!txt.contentEquals("")){ if(!tts.isSpeaking()){ tts.speak(txt,TextToSpeech.QUEUE_FLUSH,null); } }else tts.speak("Please type a word to pronounce..",TextToSpeech.QUEUE_FLUSH,null); } } });
when we close the application, According to Activity life cycle, onDestroy method is called. So,we need to stop the tts service and shutdown it.
@Override protected void onDestroy() { if(tts!=null) { tts.stop(); tts.shutdown(); } super.onDestroy(); }
Part 3.3: Implement functionality of getting word meaning:
Using Link to find meaning of word: http://www.thefreedictionary.com/wordtosearch .
Intent.ACTION_VIEW will intent you to application which can hold the uri type /http link.
btnmeaning.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { txt=editText.getText().toString(); if(!txt.contentEquals("")) { Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.thefreedictionary.com/" + txt)); startActivity(i); }else Toast.makeText(VcomboHome.this, "No Word Found...", Toast.LENGTH_SHORT).show(); } });
Do Check Your Activity Class with intent filters is registered at Manifest file as:
<activity android:name=".VcomboHome" android:label="@string/title_activity_vcombo_home" android:theme="@style/AppTheme.NoActionBar"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>
You are now ready to run your application.
Where do you can use these functionality?
- You can create a book reader where application will get user read whole text document. Eg. You can make story book reader for your childrens.
- You can use voice to text and analyze text for performing some actions.
- You can make voice dictionary application.
- You can use voice feature for the blind people benefits.
czRRoY Really enjoyed this post.Much thanks again. Much obliged.