Site icon C1CTech

Android highlight the paragraph in TextToSpeech

&NewLine;<p>This post is about how to change paragraph color which is current speaking in <span style&equals;"color&colon; &num;0000ff&semi;"><strong><a style&equals;"color&colon; &num;0000ff&semi;" href&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;android-text-to-speech-example&sol;">TextToSpeech<&sol;a><&sol;strong><&sol;span>&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<div class&equals;"wp-block-buttons aligncenter is-layout-flex wp-block-buttons-is-layout-flex">&NewLine;<div class&equals;"wp-block-button"><a class&equals;"wp-block-button&lowbar;&lowbar;link has-white-color has-text-color has-background" href&equals;"https&colon;&sol;&sol;github&period;com&sol;arunk7839&sol;AndroidHightlightTextDemo" style&equals;"background-color&colon;&num;520599" target&equals;"&lowbar;blank" rel&equals;"noreferrer noopener"><strong>DOWNLOAD CODE<&sol;strong><&sol;a><&sol;div>&NewLine;<&sol;div>&NewLine;&NewLine;&NewLine;&NewLine;<figure class&equals;"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class&equals;"wp-block-embed&lowbar;&lowbar;wrapper">&NewLine;<span class&equals;"embed-youtube" style&equals;"text-align&colon;center&semi; display&colon; block&semi;"><amp-youtube data-videoid&equals;"ldF3tV0e&lowbar;cA" data-param-rel&equals;"1" data-param-showsearch&equals;"0" data-param-showinfo&equals;"1" data-param-iv&lowbar;load&lowbar;policy&equals;"1" data-param-fs&equals;"1" data-param-hl&equals;"en-US" data-param-autohide&equals;"2" data-param-wmode&equals;"transparent" width&equals;"1200" height&equals;"675" layout&equals;"responsive"><a href&equals;"https&colon;&sol;&sol;www&period;youtube&period;com&sol;watch&quest;v&equals;ldF3tV0e&lowbar;cA" placeholder><amp-img src&equals;"https&colon;&sol;&sol;i&period;ytimg&period;com&sol;vi&sol;ldF3tV0e&lowbar;cA&sol;hqdefault&period;jpg" alt&equals;"YouTube Poster" layout&equals;"fill" object-fit&equals;"cover"><noscript><img src&equals;"https&colon;&sol;&sol;i&period;ytimg&period;com&sol;vi&sol;ldF3tV0e&lowbar;cA&sol;hqdefault&period;jpg" loading&equals;"lazy" decoding&equals;"async" alt&equals;"YouTube Poster"><&sol;noscript><&sol;amp-img><&sol;a><&sol;amp-youtube><&sol;span>&NewLine;<&sol;div><&sol;figure>&NewLine;&NewLine;&NewLine;&NewLine;<h3 class&equals;"wp-block-heading"><span style&equals;"color&colon; &num;000080&semi;"><strong>Creating new project<&sol;strong><&sol;span><&sol;h3>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"p1"><b>1<&sol;b>&period; Create a new project by going to&nbsp&semi;<span style&equals;"color&colon; &num;008000&semi;"><b>File&nbsp&semi;<&sol;b><span class&equals;"s1"><b>&Implies;<&sol;b><&sol;span><&sol;span><b><span style&equals;"color&colon; &num;008000&semi;">&nbsp&semi;New Android Project<&sol;span>&comma;<&sol;b>&nbsp&semi;fill the required details and then click on&nbsp&semi;<span style&equals;"color&colon; &num;008000&semi;"><strong>finish<&sol;strong><&sol;span>&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"p1"><b>2<&sol;b>&period; The below layout file consist of two textView and a button to trigger an event that will highlight the paragraph while speech is running in <strong><span style&equals;"color&colon; &num;0000ff&semi;">TextToSpeech<&sol;span>&period;<&sol;strong><&sol;p>&NewLine;&NewLine;&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong data-rich-text-format-boundary&equals;"true">activity&lowbar;main&period;xml<&sol;strong><&sol;span><&sol;p>&NewLine;<pre>&lt&semi;&quest;xml version&equals;"1&period;0" encoding&equals;"utf-8"&quest;&gt&semi;&NewLine;&lt&semi;androidx&period;constraintlayout&period;widget&period;ConstraintLayout xmlns&colon;android&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res&sol;android"&NewLine; xmlns&colon;app&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res-auto"&NewLine; xmlns&colon;tools&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;tools"&NewLine; android&colon;layout&lowbar;width&equals;"match&lowbar;parent"&NewLine; android&colon;layout&lowbar;height&equals;"match&lowbar;parent"&NewLine; tools&colon;context&equals;"&period;MainActivity"&gt&semi;&NewLine;&NewLine; &lt&semi;TextView&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;tv&lowbar;text&lowbar;to&lowbar;speech"&NewLine; android&colon;layout&lowbar;width&equals;"match&lowbar;parent"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;marginTop&equals;"&commat;dimen&sol;margin&lowbar;50dp"&NewLine; android&colon;gravity&equals;"center"&NewLine; android&colon;text&equals;"&commat;string&sol;text&lowbar;to&lowbar;speech"&NewLine; android&colon;textColor&equals;"&commat;color&sol;colorPrimaryDark"&NewLine; android&colon;textSize&equals;"&commat;dimen&sol;textSize&lowbar;30sp"&NewLine; android&colon;textStyle&equals;"bold"&NewLine; app&colon;layout&lowbar;constraintEnd&lowbar;toEndOf&equals;"parent"&NewLine; app&colon;layout&lowbar;constraintStart&lowbar;toStartOf&equals;"parent"&NewLine; app&colon;layout&lowbar;constraintTop&lowbar;toTopOf&equals;"parent" &sol;&gt&semi;&NewLine;&NewLine;&NewLine; &lt&semi;TextView&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;tv&lowbar;text"&NewLine; android&colon;layout&lowbar;width&equals;"match&lowbar;parent"&NewLine; android&colon;layout&lowbar;height&equals;"250dp"&NewLine; android&colon;layout&lowbar;marginLeft&equals;"&commat;dimen&sol;margin&lowbar;10dp"&NewLine; android&colon;layout&lowbar;marginRight&equals;"&commat;dimen&sol;margin&lowbar;10dp"&NewLine; android&colon;layout&lowbar;marginTop&equals;"&commat;dimen&sol;margin&lowbar;30dp"&NewLine; android&colon;background&equals;"&commat;drawable&sol;background"&NewLine; android&colon;textColor&equals;"&commat;color&sol;colorPrimaryDark"&NewLine; android&colon;padding&equals;"&commat;dimen&sol;padding&lowbar;8dp"&NewLine; app&colon;layout&lowbar;constraintEnd&lowbar;toEndOf&equals;"parent"&NewLine; android&colon;textSize&equals;"&commat;dimen&sol;textSize&lowbar;25sp"&NewLine; app&colon;layout&lowbar;constraintStart&lowbar;toStartOf&equals;"&commat;&plus;id&sol;tv&lowbar;text&lowbar;to&lowbar;speech"&NewLine; app&colon;layout&lowbar;constraintTop&lowbar;toBottomOf&equals;"&commat;&plus;id&sol;tv&lowbar;text&lowbar;to&lowbar;speech" &sol;&gt&semi;&NewLine;&NewLine; &lt&semi;Button&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;btn&lowbar;speak&lowbar;text"&NewLine; android&colon;layout&lowbar;width&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;marginTop&equals;"&commat;dimen&sol;margin&lowbar;30dp"&NewLine; android&colon;background&equals;"&commat;color&sol;colorPrimary"&NewLine; android&colon;padding&equals;"&commat;dimen&sol;padding&lowbar;12dp"&NewLine; android&colon;onClick&equals;"speakClicked"&NewLine; android&colon;text&equals;"&commat;string&sol;speak&lowbar;text"&NewLine; android&colon;textColor&equals;"&commat;android&colon;color&sol;white"&NewLine; android&colon;textSize&equals;"&commat;dimen&sol;textSize&lowbar;18sp"&NewLine; android&colon;textStyle&equals;"bold"&NewLine; app&colon;layout&lowbar;constraintEnd&lowbar;toEndOf&equals;"&commat;&plus;id&sol;tv&lowbar;text"&NewLine; app&colon;layout&lowbar;constraintStart&lowbar;toStartOf&equals;"&commat;&plus;id&sol;tv&lowbar;text"&NewLine; app&colon;layout&lowbar;constraintTop&lowbar;toBottomOf&equals;"&commat;&plus;id&sol;tv&lowbar;text" &sol;&gt&semi;&NewLine;&NewLine;&lt&semi;&sol;androidx&period;constraintlayout&period;widget&period;ConstraintLayout&gt&semi;<&sol;pre>&NewLine;<p><strong data-rich-text-format-boundary&equals;"true">3<&sol;strong>&period; Open <strong><span style&equals;"color&colon; &num;008000&semi;">MainActivity&period;java<&sol;span>&comma;<&sol;strong> implement it from <strong><span style&equals;"color&colon; &num;008000&semi;">TextToSpeech&period;OnInitListener<&sol;span> <&sol;strong>and add the below code<strong>&period;<&sol;strong><&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>MainActivity&period;java<&sol;strong><&sol;span><&sol;p>&NewLine;<&excl;-- WP QUADS Content Ad Plugin v&period; 2&period;0&period;98&period;1 -->&NewLine;<div class&equals;"quads-location quads-ad2" id&equals;"quads-ad2" style&equals;"float&colon;none&semi;margin&colon;0px&semi;">&NewLine;&NewLine;<&sol;div>&NewLine;&NewLine;<pre>package com&period;c1ctech&period;androidhightlighttextdemo&semi;&NewLine;&NewLine;import android&period;os&period;Bundle&semi;&NewLine;import android&period;speech&period;tts&period;TextToSpeech&semi;&NewLine;import android&period;speech&period;tts&period;UtteranceProgressListener&semi;&NewLine;import android&period;text&period;SpannableString&semi;&NewLine;import android&period;text&period;Spanned&semi;&NewLine;import android&period;text&period;style&period;ForegroundColorSpan&semi;&NewLine;import android&period;util&period;Log&semi;&NewLine;import android&period;view&period;View&semi;&NewLine;import android&period;widget&period;TextView&semi;&NewLine;&NewLine;import java&period;util&period;ArrayList&semi;&NewLine;import java&period;util&period;Arrays&semi;&NewLine;&NewLine;import androidx&period;appcompat&period;app&period;AppCompatActivity&semi;&NewLine;&NewLine;public class MainActivity extends AppCompatActivity implements TextToSpeech&period;OnInitListener &lbrace;&NewLine;&NewLine; private int paragraphCount &equals; 0&semi;&NewLine; private ArrayList&lt&semi;String&gt&semi; stringArrayList &equals; new ArrayList&lt&semi;&gt&semi;&lpar;&rpar;&semi;&NewLine; TextView textView&semi;&NewLine; TextToSpeech tts&semi;&NewLine;&NewLine; &commat;Override&NewLine; protected void onCreate&lpar;Bundle savedInstanceState&rpar; &lbrace;&NewLine; super&period;onCreate&lpar;savedInstanceState&rpar;&semi;&NewLine; setContentView&lpar;R&period;layout&period;activity&lowbar;main&rpar;&semi;&NewLine; textView &equals; findViewById&lpar;R&period;id&period;tv&lowbar;text&rpar;&semi;&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;creating TTS instance<&sol;span><&sol;strong>&NewLine; tts &equals; new TextToSpeech&lpar;this&comma; this&rpar;&semi;&NewLine; textView&period;setText&lpar;getString&lpar;R&period;string&period;text&rpar;&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; private void speakText&lpar;&rpar; &lbrace;&NewLine; if &lpar;paragraphCount &equals;&equals; 0&rpar; &lbrace;&NewLine; stringArrayList &equals; new ArrayList&lt&semi;&gt&semi;&lpar;Arrays&period;asList&lpar;textView&period;getText&lpar;&rpar;&period;toString&lpar;&rpar;&period;split&lpar;"&bsol;n"&rpar;&rpar;&rpar;&semi;&NewLine; &rcub;&NewLine; try &lbrace;&NewLine; SpannableString spannableString &equals; new SpannableString&lpar;textView&period;getText&lpar;&rpar;&period;toString&lpar;&rpar;&rpar;&semi;&NewLine; spannableString&period;setSpan&lpar;new ForegroundColorSpan&lpar;getResources&lpar;&rpar;&period;getColor&lpar;R&period;color&period;colorPrimaryDark&rpar;&rpar;&comma;&NewLine; 0&comma; textView&period;getText&lpar;&rpar;&period;toString&lpar;&rpar;&period;length&lpar;&rpar;&comma; Spanned&period;SPAN&lowbar;INCLUSIVE&lowbar;INCLUSIVE&rpar;&semi;&NewLine;&NewLine;&NewLine; spannableString&period;setSpan&lpar;new ForegroundColorSpan&lpar;getResources&lpar;&rpar;&period;getColor&lpar;R&period;color&period;colorAccent&rpar;&rpar;&comma;&NewLine; textView&period;getText&lpar;&rpar;&period;toString&lpar;&rpar;&period;indexOf&lpar;stringArrayList&period;get&lpar;paragraphCount&rpar;&rpar;&comma;&NewLine; textView&period;getText&lpar;&rpar;&period;toString&lpar;&rpar;&period;indexOf&lpar;stringArrayList&period;get&lpar;paragraphCount&rpar;&rpar; &plus;&NewLine; stringArrayList&period;get&lpar;paragraphCount&rpar;&period;length&lpar;&rpar;&comma;&NewLine; Spanned&period;SPAN&lowbar;INCLUSIVE&lowbar;INCLUSIVE&rpar;&semi;&NewLine;&NewLine; tts&period;speak&lpar;stringArrayList&period;get&lpar;paragraphCount&rpar;&comma; TextToSpeech&period;QUEUE&lowbar;FLUSH&comma; null&comma; "UniqueID"&rpar;&semi;&NewLine;&NewLine; textView&period;setText&lpar;spannableString&rpar;&semi;&NewLine;&NewLine; &rcub; catch &lpar;Exception e&rpar; &lbrace;&NewLine; e&period;printStackTrace&lpar;&rpar;&semi;&NewLine; &rcub;&NewLine; &rcub;&NewLine;&NewLine;<span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;&sol;Called to signal the completion of the TextToSpeech engine initialization&period;&NewLine;<&sol;strong><&sol;span> &commat;Override&NewLine; public void onInit&lpar;int i&rpar; &lbrace;&NewLine;&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;Listener for events relating to the progress of an utterance&NewLine;<&sol;span><&sol;strong> tts&period;setOnUtteranceProgressListener&lpar;new UtteranceProgressListener&lpar;&rpar; &lbrace;&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;called when speaking starts<&sol;span><&sol;strong>&NewLine; &commat;Override&NewLine; public void onStart&lpar;String utteranceId&rpar; &lbrace;&NewLine; Log&period;i&lpar;"TTS"&comma; "utterance started"&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;called when speaking is finished&period;<&sol;span><&sol;strong>&NewLine; &commat;Override&NewLine; public void onDone&lpar;String utteranceId&rpar; &lbrace;&NewLine; if &lpar;stringArrayList&period;size&lpar;&rpar; - 1 &excl;&equals; paragraphCount&rpar; &lbrace;&NewLine; paragraphCount&plus;&plus;&semi;&NewLine; speakText&lpar;&rpar;&semi;&NewLine; &rcub; else &lbrace;&NewLine; paragraphCount &equals; 0&semi;&NewLine; &rcub;&NewLine; Log&period;i&lpar;"TTS"&comma; "utterance done"&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;called when an error has occurred during processing&period;&NewLine;<&sol;span><&sol;strong> &commat;Override&NewLine; public void onError&lpar;String utteranceId&rpar; &lbrace;&NewLine; Log&period;i&lpar;"TTS"&comma; "utterance error"&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; &rcub;&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; public void speakClicked&lpar;View ignored&rpar; &lbrace;&NewLine; speakText&lpar;&rpar;&semi;&NewLine; &rcub;&NewLine;&rcub;<&sol;pre>&NewLine;&NewLine;&NewLine;<p><span style&equals;"color&colon; &num;000080&semi;"><strong>Note&colon;<&sol;strong><&sol;span> The <span style&equals;"color&colon; &num;0000ff&semi;"><strong>setOnUtteranceProgressListener&lpar;&rpar;<&sol;strong><&sol;span> method must be called before <span style&equals;"color&colon; &num;0000ff&semi;"><strong>speak&lpar;&rpar;<&sol;strong><&sol;span> is called&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p>In the above code&comma;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<ul class&equals;"wp-block-list"><li><span style&equals;"color&colon; &num;0000ff&semi;"><strong>onInit&lpar;&rpar;<&sol;strong>&colon;<&sol;span> Called to signal the completion of the TextToSpeech engine initialization&period;<&sol;li><li class&equals;"p1"><span style&equals;"color&colon; &num;0000ff&semi;"><strong>speak&lpar;&rpar;<&sol;strong>&colon;<&sol;span> Speaks the text using the specified queuing strategy and speech parameters&period;<&sol;li><li><strong><span style&equals;"color&colon; &num;0000ff&semi;">onStart&lpar;&rpar;&colon;<&sol;span> <&sol;strong>called when speaking starts &lpar;soon after calling speak&lpar;&rpar;&rpar;&period;<&sol;li><li><span style&equals;"color&colon; &num;0000ff&semi;"><strong>onDone&lpar;&rpar;&colon; <&sol;strong><&sol;span>called when speaking is finished&period;<&sol;li><li><span style&equals;"color&colon; &num;0000ff&semi;"><strong>onError&lpar;&rpar;&colon; <&sol;strong><&sol;span>called when an error has occurred during processing&period;<&sol;li><li><span style&equals;"color&colon; &num;0000ff&semi;"><strong>stop&lpar;&rpar;<&sol;strong>&colon;<&sol;span> stop the speak&period;<&sol;li><li><span style&equals;"color&colon; &num;0000ff&semi;"><strong>shutdown&lpar;&rpar;<&sol;strong>&colon;<&sol;span> releases the resources used by the TextToSpeech engine&period;<&sol;li><&sol;ul>&NewLine;&NewLine;&NewLine;&NewLine;<p><strong data-rich-text-format-boundary&equals;"true">4<&sol;strong>&period; Now run your project and test your app by clicking the button&period; &nbsp&semi;<&sol;p>&NewLine;&NewLine;&NewLine;<p><img class&equals;"alignnone wp-image-2367" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2021&sol;02&sol;Screenshot&lowbar;1612184216&period;png" alt&equals;"" width&equals;"436" height&equals;"775" &sol;>&NewLine;

Exit mobile version