Make Tone Analyzer Android App



Original Source Here

Make An App….

Now create an Android App. Click on Create New Project then an create an empty Activity and then write your project and project name and click on finish button.

After gradle process running MainActivity.java and activity_main.xml is created with default code. Add the IBM CLOUD dependency in your App:

add dependency in build.gradle and sync now

implementation 'com.ibm.watson:ibm-watson:9.0.2'

After this replace MainActivity.java and activity_main.xml with this code:

MainActivity.java

package com.project.sentimentanalysis;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.ibm.cloud.sdk.core.http.ServiceCallback;
import com.ibm.cloud.sdk.core.security.Authenticator;
import com.ibm.cloud.sdk.core.security.IamAuthenticator;
import com.ibm.watson.tone_analyzer.v3.ToneAnalyzer;
import com.ibm.watson.tone_analyzer.v3.model.ToneAnalysis;
import com.ibm.watson.tone_analyzer.v3.model.ToneOptions;
import com.ibm.watson.tone_analyzer.v3.model.ToneScore;

import org.w3c.dom.Text;

import java.util.List;

public class MainActivity extends AppCompatActivity {

TextView txt1 ;
EditText edtTxt1;
Button btn1;
String sentiment;
boolean check = true;


private class AskWatsonTask extends AsyncTask<String,Void,String>{
@Override
protected String doInBackground(String... TextToAnalyse) {
System.out.println(edtTxt1.getText());

runOnUiThread(new Runnable() {
@Override
public void run() {
txt1.setText("What is happening inside the thread");
}
});
Authenticator authenticator = new IamAuthenticator("Replace with your Api key");
ToneAnalyzer service = new ToneAnalyzer("2017-09-21", authenticator);

ToneOptions toneOptions = new ToneOptions.Builder().text(String.valueOf(edtTxt1.getText())).build();
ToneAnalysis tone = service.tone(toneOptions).execute().getResult();
System.out.println(tone);


sentiment = "Test Sentiment";
System.out.println(tone);


try {


return tone.getDocumentTone().getTones().get(0).getToneId().toString();
} catch (Exception exception) {

check= false;
// Toast.makeText(getApplicationContext(),"Try Again Robot is confuse",Toast.LENGTH_SHORT).show();
return null;
}

}

@Override
protected void onPostExecute(String result) {
// txt1.setText("The message sentiment is "+ result);
if(check ) {
Intent intent = new Intent(MainActivity.this, ShowActivity.class);
intent.putExtra("sentiment", result);

startActivity(intent);
}

else
{
Toast.makeText(getApplicationContext(),"Try Again Robot is confuse",Toast.LENGTH_SHORT).show();
}

}
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


txt1 = (TextView) findViewById(R.id.txt1);
edtTxt1 = (EditText) findViewById(R.id.edttxt1);
btn1 = (Button) findViewById(R.id.btn1);


btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
System.out.println("logging to the console that the button pressed for the test" + edtTxt1.getText());
txt1.setText("Display at UI the sentiment to be checked for" +edtTxt1.getText());

AskWatsonTask task = new AskWatsonTask();
task.execute(new String[]{});
}
}

);
}


}

activity_main.java

<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical"
android:layout_height="match_parent"
tools:context=".MainActivity">

<View
android:layout_width="match_parent"
android:layout_height="50dp"></View>

<TextView
android:id="@+id/txt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textSize="18dp"
android:text="Hey ! I am Robo Analyzer...."
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<EditText
android:id="@+id/edttxt1"
android:layout_margin="40dp"
android:layout_width="match_parent"
android:layout_height="100dp"
></EditText>

<Button
android:id="@+id/btn1"
android:layout_width="match_parent"
android:layout_height="60dp"
android:text="Analyize "></Button>

</LinearLayout>

After this replace ShowActivity.java and activity_show.xml with this code:

ShowActivity.java

package com.project.sentimentanalysis;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class ShowActivity extends AppCompatActivity {

TextView txt2;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show);

txt2 = (TextView) findViewById(R.id.text1);

String id = getIntent().getStringExtra("sentiment");

txt2.setText(id);
}

@Override
public void onBackPressed() {
super.onBackPressed();
finish();
}
}

activity_show.xml

<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical"
android:layout_height="match_parent"
tools:context=".ShowActivity">


<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="The sentiment is :"
android:textSize="20dp"
android:layout_margin="50dp"></TextView>

<TextView
android:id="@+id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="The sentiment is :"
android:textSize="20dp"
android:layout_margin="50dp"></TextView>

</LinearLayout>

Run Application 🚀😊…

After this run your Application.

AI/ML

Trending AI/ML Article Identified & Digested via Granola by Ramsey Elbasheer; a Machine-Driven RSS Bot

%d bloggers like this: