Introduction
Welcome to the gateway of Android ingenuity! In this tutorial, we’ll embark on a journey to unveil the secrets behind crafting your very own Text-to-Speech app for Android devices. With the power of Android Studio at your fingertips, we’ll navigate through the intricacies of development, breaking down complex concepts into simple, actionable steps.
But that’s not all. What sets this tutorial apart is the promise of empowerment. Not only will you learn how to build a functional Text-to-Speech app from scratch, but you’ll also gain access to the complete source code, ready to be customized and integrated into your projects.
So, whether you’re a seasoned developer looking to expand your skill set or a newcomer eager to dive into the world of app creation, join us as we unlock the potential of Android development and take the first step towards building your own digital empire.
Getting Started…
Step 1:
Hello everyone, welcome to another tutorial. Today, we’re going to create a simple text-to-speech app in Android Studio using Java. Let’s get started by setting up a new project.
Step 2:
In this step, we’ll design the layout of our app. We’ll have a TextView to prompt the user, an EditText for input, and a Button to trigger the text-to-speech functionality.
<TextView android:id="@+id/inputText" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Enter text to speak:" android:textSize="18sp" android:layout_marginBottom="16dp"/> <EditText android:id="@+id/editText" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/inputText" android:hint="Type here" android:inputType="textMultiLine" android:minLines="3" android:gravity="start|top" android:scrollbars="vertical"/> <Button android:id="@+id/speakButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/editText" android:layout_centerHorizontal="true" android:text="Speak"/>
Step 3:
Now, let’s ensure our app has the necessary permissions for text-to-speech functionality. We’ll add permissions for INTERNET, RECORD_AUDIO, and MODIFY_AUDIO_SETTINGS in the AndroidManifest.xml file.
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<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">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Step 4:
In this step, we’ll initialize the TextToSpeech object in our MainActivity.java file. This object allows us to convert text to speech in our app.
import android.os.Bundle; import android.speech.tts.TextToSpeech; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; import java.util.Locale; public class MainActivity extends AppCompatActivity { private TextToSpeech textToSpeech; private EditText editText; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); editText = findViewById(R.id.editText); Button speakButton = findViewById(R.id.speakButton); textToSpeech = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() { @Override public void onInit(int status) { if (status != TextToSpeech.ERROR) { textToSpeech.setLanguage(Locale.getDefault()); } else { Toast.makeText(MainActivity.this, "Initialization failed", Toast.LENGTH_SHORT).show(); } } }); speakButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String text = editText.getText().toString(); textToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH, null, null); } }); } @Override protected void onDestroy() { if (textToSpeech != null) { textToSpeech.stop(); textToSpeech.shutdown(); } super.onDestroy(); } }
Step 5:
Now, let’s implement the functionality to convert the entered text to speech when the user clicks the button.
import android.os.Bundle; import android.speech.tts.TextToSpeech; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; import java.util.Locale; public class MainActivity extends AppCompatActivity { private TextToSpeech textToSpeech; private EditText editText; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); editText = findViewById(R.id.editText); Button speakButton = findViewById(R.id.speakButton); textToSpeech = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() { @Override public void onInit(int status) { if (status != TextToSpeech.ERROR) { textToSpeech.setLanguage(Locale.getDefault()); } else { Toast.makeText(MainActivity.this, "Initialization failed", Toast.LENGTH_SHORT).show(); } } }); speakButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String text = editText.getText().toString(); if (!text.isEmpty()) { textToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH, null, null); } else { Toast.makeText(MainActivity.this, "Enter text to speak", Toast.LENGTH_SHORT).show(); } } }); } @Override protected void onDestroy() { if (textToSpeech != null) { textToSpeech.stop(); textToSpeech.shutdown(); } super.onDestroy(); } }
Step 6:
Lastly, to ensure our app runs smoothly and avoids memory leaks, we release the TextToSpeech resources in the onDestroy() method.
import android.os.Bundle; import android.speech.tts.TextToSpeech; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; import java.util.Locale; public class MainActivity extends AppCompatActivity { private TextToSpeech textToSpeech; private EditText editText; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); editText = findViewById(R.id.editText); Button speakButton = findViewById(R.id.speakButton); textToSpeech = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() { @Override public void onInit(int status) { if (status != TextToSpeech.ERROR) { textToSpeech.setLanguage(Locale.getDefault()); } else { Toast.makeText(MainActivity.this, "Initialization failed", Toast.LENGTH_SHORT).show(); } } }); speakButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String text = editText.getText().toString(); if (!text.isEmpty()) { textToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH, null, null); } else { Toast.makeText(MainActivity.this, "Enter text to speak", Toast.LENGTH_SHORT).show(); } } }); } @Override protected void onDestroy() { if (textToSpeech != null) { textToSpeech.stop(); textToSpeech.shutdown(); } super.onDestroy(); } }
Step 7:
That’s it! You’ve successfully created a simple text-to-speech app in Android Studio using Java. Feel free to run the app on an emulator or a physical device to test it out. Let me know if you have any questions or need further assistance!