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:
I want to change it to something like this? To differentiate the tones from the scores?
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() {
}
}