Coding an Android App With IBM Watson Machine Learning

In early 2011, a natural language understanding computer system called IBM Watson was able to beat all human contestants in a quiz-like game show called Jeopardy! It was an historic event and was perhaps the point when the general public started to realize that artificial intelligence was no longer the stuff of science fiction.

Back then, Watson ran on a supercomputer and could do little more than answer trivia questions. Today, however, it has been transformed into a far more versatile system, and a lot of its capabilities are available over the cloud.

In this series of tutorials, I’ll show you how to use various services offered by the IBM Watson Developer Cloud in Android apps. In this introductory tutorial, you’ll learn how to work with IBM Bluemix, the Watson Java SDK, and the Watson tone analyzer service to create a simple Android app that can identify the emotions present in any piece of text.


To be able to follow along, you’ll need:

  • a recent version of Android Studio
  • a free IBM Bluemix account
  • and a device or emulator running Android 4.4 or higher

1. Creating a Watson Service

Watson’s services can be accessed only through the IBM Bluemix cloud platform. That means you can use them in your application only after you’ve enabled them in the Bluemix console and acquired the required credentials.

Start by logging in to the console and navigating to Services > Watson.

Watson services home screen

Next, press the Create Watson service button to see all the Watson services that are available to you.

List of Watson services

For now, because we’re interested only in analyzing the emotional tone in written text, select the Tone Analyzer service.

On the next screen, change the name of the service to something meaningful and press the Create button.

Tone analyzer creation screen

The tone analyzer service will now be activated for your account, and a set of login credentials will be generated. To see what those credentials are, open the Service credentials tab and select the View credentials action.

Tone analyzer credentials

Save the contents of the JSON document to a file on your computer. I suggest you name the file credentials.json.

2. Project Setup

The Watson Java SDK makes it very easy for you to communicate with any Watson service. To be able to use it in your project, add it as a compile dependency in the app module’s build.gradle file.

For this tutorial, you’ll be needing two more libraries: Commons IO, to simplify file IO operations, and Android Design Support, to be able to use a few Material Design widgets. Add them too as compile dependencies.

You will also need the INTERNET permission to communicate with IBM’s servers, so add the following line to the AndroidManifest.xml file:

Finally, press the Sync Now button to complete the project configuration.

3. Initializing the Tone Analyzer

To communicate with Watson’s tone analyzer service, you must use the ToneAnalyzer class. Its constructor expects a date string as its argument, which it will use to automatically determine the version of the tone analyzer service to use.

Note that the date should always be a static string. This helps make sure that your app doesn’t stop working when a new version of the service introduces breaking changes.

The ToneAnalyzer instance must be configured to use your credentials, so move the credentials.json file, the one you created earlier in this tutorial, to your project’s res/raw folder.

Next, you must extract the values of the username and password keys from the JSON file. The following code shows you how to do so quickly using the IOUtils and JSONObject classes:

You can now complete the configuration of the ToneAnalyzer instance by calling the setUsernameAndPassword() method.

4. Creating a User Interface

Our app needs an interface that will allow the user to type in a few sentences and press a button to begin the tone analysis. In other words, you must now create a simple layout containing an EditText widget and a Button widget.

You can, optionally, also throw in a TextInputLayout container to make sure that the EditText widget conforms to the guidelines of Material Design.

Accordingly, add the following code to your activity’s layout XML file:

5. Using the Tone Analyzer

The tone analysis must start as soon as the user presses the button. Therefore, you need to add an on-click event handler to the Button widget using its setOnClickListener() method. Before you do so, however, don’t forget to get a reference to the widget using the findViewById() method.

Inside the on-click event handler, the first thing you need to do is determine what the user has typed into the EditText widget using its getText() method.

Like all Watson services, the tone analyzer service offers a lot of different features. For instance, it can identify emotions, language style, and social tendencies in its input. It can also work with JSON documents, plain text, and HTML. Therefore, to use it efficiently, you must explicitly specify the features you need using a ToneOptions object, which can be easily created using the ToneOptions.Builder class.

For now, we’ll be using the service only to list all the emotions that are present in a plain text string. Here’s how you create a ToneOptions object saying so:

You can now compose a tone analysis request by calling the getTone() method, which expects a string containing the text to analyze and a ToneOptions object as its only arguments. To actually execute the request asynchronously, you must also call the enqueue() method, which takes a ServiceCallback instance as its argument.

Inside the onResponse() method of the ServiceCallback class, you have access to a ToneAnalysis object. By calling its getDocumentTone() method, you can determine the overall tone of the text you passed to the service.

The return value of the getDocumentTone() method is an ElementTone object containing a list of tone categories. Because we explicitly stated earlier that we’re interested only in the emotions category, the list will contain only one element. Accordingly, here’s how you extract the scores for all the emotions the service detected:

As you might expect, a ToneScore object represents a single emotion. It has a Double containing the actual score and a String telling you the name of the emotion. If the score is greater than 0.5, it means that there’s a good chance the emotion is being expressed in the text.

Let us now pick only those emotions whose scores are greater than 0.5 and use them to create a message that can be displayed to the user using a Toast.

Because the onResponse() method runs on different thread, you must make sure you create and display the Toast only after calling the runOnUiThread() method.

The app is now ready. If you run it, type in a few sentences, and start the analysis, you’ll be able to see Watson accurately identify the emotions present in your text.

Tone analyzer app running


In this tutorial, you created an Android app that can use IBM Watson’s tone analyzer service to perform the complex task of guessing the emotions expressed in a piece of text. You also learned the basics of using the Watson Java SDK and the IBM Bluemix console.

To learn more about the service, you can refer to its official documentation.

In the meantime, you can check out some of our other posts on using machine learning for your Android apps right here on Envato Tuts+!

  • Android Things
    Android Things and Machine Learning
    Paul Trebilcox-Ruiz
  • Android Things
    Android Things: Creating a Cloud-Connected Doorman
    Paul Trebilcox-Ruiz
  • Android SDK
    How to Use Google Cloud Machine Learning Services for Android
    Ashraff Hathibelagal
  • Android SDK
    Create an Intelligent App With Google Cloud Speech and Natural Language APIs
    Ashraff Hathibelagal
  • Android SDK
    How to Use the Google Cloud Vision API in Android Apps
    Ashraff Hathibelagal

Leave a Comment

Scroll to Top