Introduction :
In the mobile application world, creating a speaking calculator application is a very interesting and educational project. You can build a speaking calculator using python code by chaquopy within Android Studio IDE. Here, you create a speaking calculator application that not only performs mathematical operations but also verbally communicates to take input and give results loudly.
In the program, you have to use speech to text and text to speech technology to make a speaking calculator app. In the article, I shall show you how to create speaking calculator app using chaquopy with python code in Android Studio IDE. Here, you have to use python language with java to build the app using chaquopy.
What is Speaking calculator :
The app, which calculate mathematical calculations using audio or voice input and give the results verbally is called Speaking Calculator. Speaking calculator generally utilizes speech to text technology to convert voice input by the user to the mathematical equation. After calculating the equation, the app converts the numerical results in speech using text to speech technology.
The speaking calculator app can perform various mathematical operations such as addition, subtraction, multiplication, division, exponentiation, square roots etc. This app is useful for visual disability person.
About the application :
This is a simple calculator app which takes voice input and give output verbally. When you open the app on your phone, you can see a microphone (mic) on the screen. You have to tap or click on the mic. After that you have to speak like below sentences for calculating your math problem.
- For addition speak like – five plus two
- For subscription speak like – five minus two
- For divide speak like – five dividedby two
- For multiply speak like – five multiply two
Then, the app calculates your voice input using python code and it speak out your answer loudly. If your input is wrong, that will inform you.
Step for create the application :
You have to follow the below steps to build a speaking calculator using python code in android studio IDE.
Step 1 :
At first, open the Android Studio on your pc and create a new project with empty activity. Give a name of the project such as “Speaking_calculator”. You have to check on legacy android.support libraries. You can see how to create Android app on Android Studio IDE. You can also see how to use python code in android studio app.
Step 2 :
After the app will be built, open manifest file and add the following code before application tag.
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Step 3 :
Now, open build.gradle(Project) file and add the below code in repositories.
maven { url "https://chaquo.com/maven" }
You have to also add the following code in dependencies.
classpath "com.chaquo.python:gradle:13.0.0"
After add the above code the build.gradle(Project) file look like below code.
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
jcenter()
maven { url "https://chaquo.com/maven" }
}
dependencies {
classpath "com.android.tools.build:gradle:4.1.1"
classpath "com.chaquo.python:gradle:13.0.0"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
Step 4 :
Then, open build.gradle(Module) file and add the following code in plugins.
id("com.chaquo.python")
Now goto defaultConfig and add the following code after applicationId “com.example.android_app_with_python” and before “minSdkVersion 22”. The code will create a folder with the name “python”.
sourceSets {
main {
python {
srcDirs=["src/main/python"]
}
}
}
After testInstrumentationRunner “android.support.test.runner.AndroidJUnitRunner” add the code. You have to change the path of python.exe file in your code. The python.exe file of your pc will be different than my code.
ndk {
abiFilters "armeabi-v7a", "x86"
}
python {
pip {
install "word2number"
install "num2words"
}
}
python {
buildPython "C:/Users/puskarjasu/AppData/Local/Programs/Python/Python39/python.exe"
}
Now build.gradle(Module) file look like below code.
plugins {
id 'com.android.application'
id("com.chaquo.python")
}
android {
compileSdkVersion 31
buildToolsVersion "34.0.0"
defaultConfig {
applicationId "com.example.android_app_with_python"
sourceSets {
main {
python {
srcDirs=["src/main/python"]
}
}
}
minSdkVersion 22
targetSdkVersion 31
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
ndk {
abiFilters "armeabi-v7a", "x86"
}
python {
pip {
install "word2number"
install "num2words"
}
}
python {
buildPython "C:/Users/puskarjasu/AppData/Local/Programs/Python/Python39/python.exe"
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
implementation 'com.android.support.constraint:constraint-layout:2.0.4'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
Step 5 :
When all code will be added, you have to “Sync Now” with net on your pc. It takes some time to download necessary files in your project.
Step 6 :
After that, open Project in Project tab and goto app> src>main. Here you can see “python” folder is created otherwise you have to create a folder name as “python”. In python folder, create a file “pac.py” and paste the below code.
from word2number import w2n
from num2words import num2words
def main(x):
c=0
li = list(x.split(" "))
try:
a=w2n.word_to_num(li[0])
b=w2n.word_to_num(li[2])
if li[1]=="+":
c=a+b
if li[1]=="-":
c=a-b
if li[1]=="/":
c=a/b
if li[1]=="x":
c=a*b
except:
return "try again"
c="try again"
return num2words(c)
Step 7 :
After open the MainActivity.java file, paste the below code in it.
package com.example.speaking_calculator;
import android.os.Bundle;
import java.util.ArrayList;
import java.util.Locale;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.speech.RecognizerIntent;
import android.speech.tts.TextToSpeech;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
import com.chaquo.python.PyObject;
import com.chaquo.python.Python;
import com.chaquo.python.android.AndroidPlatform;
public class MainActivity extends Activity {
TextToSpeech textToSpeech;
private ImageView btnSpeak;
private final int REQ_CODE_SPEECH_INPUT = 100;
String s;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnSpeak = (ImageView) findViewById(R.id.iv_mic);
s=null;
textToSpeech=null;
textToSpeech = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
@Override
public void onInit(int i) {
if (i != TextToSpeech.ERROR) {
textToSpeech.setLanguage(Locale.UK);
}
}
});
Python.start(new AndroidPlatform(this));
btnSpeak.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
promptSpeechInput();
}
});
}
private void promptSpeechInput () {
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, "Speak Now");
try {
startActivityForResult(intent, REQ_CODE_SPEECH_INPUT);
} catch (ActivityNotFoundException a) {
Toast.makeText(getApplicationContext(),"KK", Toast.LENGTH_SHORT).show();
}
}
@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<String> result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
s = result.get(0);
Python py = Python.getInstance();
PyObject pyobj = py.getModule("pac");
PyObject obj = null;
obj = pyobj.callAttr("main", s.toString());
textToSpeech.speak(obj.toString(), TextToSpeech.QUEUE_FLUSH, null);
}
break;
}
}
}
}
Step 8 :
Now, open the activity_main.xml file. Then, paste the below code in the activity_main.xml file.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
android:background="#0F33FB"
tools:context=".MainActivity">
<ImageView
android:id="@+id/iv_mic"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#FBF9F9"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="80dp"
android:src="@drawable/ic_mic"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/tv_speech_to_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="44dp"
android:layout_marginBottom="10dp"
android:padding="10dp"
android:text="tap mic and speek"
android:textColor="#F6F5F8"
android:textSize="30sp"
app:layout_constraintBottom_toTopOf="@+id/textView2"
app:layout_constraintEnd_toEndOf="@+id/iv_mic"
app:layout_constraintHorizontal_bias="0.489"
app:layout_constraintStart_toStartOf="@+id/iv_mic"
app:layout_constraintTop_toBottomOf="@+id/iv_mic" />
<TextView
android:id="@+id/textView2"
android:layout_width="262dp"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="8dp"
android:text="HOW TO SPEAK"
android:textColor="#F3C00A"
android:textSize="25sp"
app:layout_constraintBottom_toTopOf="@+id/textView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tv_speech_to_text"
tools:ignore="MissingConstraints"></TextView>
<TextView
android:id="@+id/textView"
android:layout_width="267dp"
android:layout_height="27dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="24dp"
android:text="five plus two for addition"
android:textColor="#ECDEB0"
android:textSize="20dp"
app:layout_constraintBottom_toTopOf="@+id/textView3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView2" />
<TextView
android:id="@+id/textView3"
android:layout_width="267dp"
android:layout_height="27dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="24dp"
android:text="five minus two for subscription"
android:textColor="#ECDEB0"
android:textSize="20dp"
app:layout_constraintBottom_toTopOf="@+id/textView4"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />
<TextView
android:id="@+id/textView4"
android:layout_width="267dp"
android:layout_height="27dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="24dp"
android:text="five dividedby two for divide"
android:textColor="#ECDEB0"
android:textSize="20dp"
app:layout_constraintBottom_toTopOf="@+id/textView5"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView3" />
<TextView
android:id="@+id/textView5"
android:layout_width="267dp"
android:layout_height="27dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="24dp"
android:text="five multiply two for multiply"
android:textColor="#ECDEB0"
android:textSize="20dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.555"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView4"
app:layout_constraintVertical_bias="0.0" />
<TextView
android:id="@+id/textView6"
android:layout_width="331dp"
android:layout_height="46dp"
android:textSize="20dp"
android:layout_marginStart="12dp"
android:layout_marginTop="22dp"
android:layout_marginEnd="14dp"
android:layout_marginBottom="38dp"
android:text="DEVELOPMENT BY Puskar Jasu"
android:textColor="#F31313"
app:layout_constraintBottom_toTopOf="@+id/iv_mic"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
</android.support.constraint.ConstraintLayout>
Step 9 :
At last, create an image file name as “ic_mic” in drawable folder for microphone (mic).

Run the application :
It is time to run the speaking calculator app. For this you have to connect your Android phone to computer via USB cable (data cable). After that, click the green “Run” button to run the app on your Android phone. Then, the app will be installed and launch on your Android device. You can see your app on a phone like this image.
Conclusion :
After completing the above article, you have learned how to build a speaking calculator using Python code within the Android Studio IDE. You can customize the app to add more mathematical calculations. Thank you for visiting my site.