0

I am not good at using API so I am having difficulty and learning with practice, for a current project I am using a Watson API that shows the result in JSON format like below. The API generates result based on users' text input, the result generated might have one or more "tones" based on the sentences. I put it in a textview to show the result.

Could anyone show me how to set different font color for the "tone_name" and "score"? I am not sure how to split it into 2 textviews as the JSON result is generated in a structure. How to separate the tones to set different color or other design but still remain the same result structure?

This is how I get the API result, it shows a list of tones and the scores of each tones generated:

       ToneAnalysis toneAnalysis = toneAnalyzer.tone(toneOptions).execute().getResult();

        List<ToneScore> documentToneScores = toneAnalysis.getDocumentTone().getTones();

        String detectedTones = "";
        for (ToneScore score : documentToneScores) {
            if (score.getScore() > 0.5f) {
                detectedTones += score.getToneName() + " \n" + score.getScore() + "\n\n";
            }
        }

Now the result looks like this because it is shown in a single textview:

enter image description here

I want to change it to something like this? To differentiate the tones from the scores?

enter image description here

JSON result format:

{
  "document_tone": {
    "tones": [
      {
        "score": 0.6165,
        "tone_id": "sadness",
        "tone_name": "Sadness"
      },
      {
        "score": 0.829888,
        "tone_id": "analytical",
        "tone_name": "Analytical"
      }
    ]
  }

xml:

<LinearLayout 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:orientation="vertical"
    tools:context=".Main2Activity">

    <TextView
        android:id="@+id/textmain2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="30dp"
        android:fontFamily="@font/droid_sans"
        android:text="result"
        android:textColor="#000"
        android:textSize="18sp" />

</LinearLayout>

Thank you in advanced.

Added a POJO class but I am still not sure how to connect it to the result :(

public class Tones {

    private String tones;
    private Double score;

    public String getTones() {
        return tones;
    }

    public void setTones(String tones) {
        this.tones = tones;
    }

    public Double getScore() {
        return score;
    }

    public void setScore(Double score) {
        this.score = score;
    }

    public Tones(String tones, Double score) {
        this.tones = tones;
        this.score = score;
    }

    public Tones() {
    }
}
Lionlollipop
  • 113
  • 3
  • 12

1 Answers1

0

You should use an object to store the values you want to display on the screen. Currently you put all in String detectedTones, but you can create a POJO or Kotlin data class to represent it, for example:

data class DetectedTones(val name: String, val score: Double)

Then in the XML layout you create two TextViews - one for name and another for score and set the values there. You could also use the ToneScore class, but typically the apps separate fetching and displaying logic. If you want to separate the logic (I do not think you need it in your case) then you use mapper (new class with static DetectedTones map(input: ToneScore) method) to map one class to another.

In your setup if you just put TextViews into an XML layout, then you need to know in advance how many values in the list you would have. Typically you do not know it in advance, then in Android we use RecyclerView to display list data. Please refer to RecyclerView tutorials for more inforation. For example: Simple Android RecyclerView example

In each row of the RecyclerView you would have an XML which defines the row. There you would have 2 TextViews. In the RecyclerView adapter you would write a code which take each item in the list and sets the name and the score to these TextViews.

vrgrg
  • 566
  • 4
  • 17