0% found this document useful (0 votes)
78 views

Android Spell Checker Api: Andriodmanifest - XML

The document describes an Android spell checker API application that allows a user to enter text, click a button to fetch spelling suggestions, and displays the suggestions. It includes code for the AndroidManifest file, MainActivity class, layout XML file, and strings resource file. The MainActivity handles getting suggestions from the SpellCheckerSession API and displays them on the screen.

Uploaded by

Sandy Richards
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
78 views

Android Spell Checker Api: Andriodmanifest - XML

The document describes an Android spell checker API application that allows a user to enter text, click a button to fetch spelling suggestions, and displays the suggestions. It includes code for the AndroidManifest file, MainActivity class, layout XML file, and strings resource file. The MainActivity handles getting suggestions from the SpellCheckerSession API and displays them on the screen.

Uploaded by

Sandy Richards
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Android Spell Checker API

AndriodManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.elcot.spellcheckerv2">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />


</intent-filter>
</activity>
</application>

</manifest>

MainActivity.java
package com.example.elcot.spellcheckerv2;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.textservice.SentenceSuggestionsInfo;
import android.view.textservice.SpellCheckerSession;
import android.view.textservice.SuggestionsInfo;
import android.view.textservice.TextInfo;
import android.view.textservice.TextServicesManager;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Locale;

public class MainActivity extends AppCompatActivity implements


SpellCheckerSession.SpellCheckerSessionListener {

private TextView suggestions;


EditText ed;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);
suggestions = findViewById(R.id.textView2);

public void fetch(View view) {

ed = findViewById(R.id.editText);
Android Spell Checker API
String input = ed.getText().toString();
fetchSuggestionsFor(input);
Toast.makeText(getApplicationContext(), "Fetching Suggestions...",
Toast.LENGTH_SHORT).show();
}

@Override
public void onGetSuggestions(SuggestionsInfo[] results) {
}

@Override
public void onGetSentenceSuggestions(SentenceSuggestionsInfo[] results) {
final StringBuffer sb = new StringBuffer("");
for (SentenceSuggestionsInfo result : results) {
int n = result.getSuggestionsCount();
for (int i = 0; i < n; i++) {
int m = result.getSuggestionsInfoAt(i).getSuggestionsCount();

for (int k = 0; k < m; k++) {


sb.append(result.getSuggestionsInfoAt(i).getSuggestionAt(k))
.append("\n");
}
sb.append("\n");
}
}

runOnUiThread(new Runnable() {
@Override
public void run() {
suggestions.setText(sb.toString());
}
});
}

private void fetchSuggestionsFor(String input) {


TextServicesManager tsm = (TextServicesManager)
getSystemService(TEXT_SERVICES_MANAGER_SERVICE);

SpellCheckerSession session = tsm.newSpellCheckerSession(null, Locale.ENGLISH,


this, true);

session.getSentenceSuggestions(new TextInfo[]{new TextInfo(input)}, 5);


}
}

activity_main.xml
<?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"
tools:context=".MainActivity">

<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/header"
Android Spell Checker API
android:textSize="18sp"
android:typeface="serif"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.05" />

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="36dp"
android:layout_marginEnd="8dp"
android:text="@string/label1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.278"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tv1" />

<EditText
android:id="@+id/editText"
android:layout_width="322dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="12dp"
android:layout_marginEnd="8dp"
android:ems="10"
android:inputType="text"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.653"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />

<Button
android:id="@+id/button"
android:layout_width="161dp"
android:layout_height="36dp"
android:layout_marginStart="8dp"
android:layout_marginTop="12dp"
android:layout_marginEnd="8dp"
android:onClick="fetch"
android:text="@string/buttonText"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.502"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText" />

<TextView
android:id="@+id/textView2"
android:layout_width="337dp"
android:layout_height="267dp"
android:layout_marginStart="8dp"
android:layout_marginTop="12dp"
android:layout_marginEnd="8dp"
android:text="@string/suggestionText"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.944"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button" />
Android Spell Checker API

</android.support.constraint.ConstraintLayout>

Layout

strings.xml
<resources>
<string name="app_name">Spell Checker API</string>
<string name="header">Andriod Spell Checker API</string>
<string name="label1">Enter Text and click suggestions button</string>
<string name="buttonText">Get Suggestions</string>
<string name="suggestionText">Suggestions will be shown here...</string>
</resources>

Output:
Android Spell Checker API

Type something in the text box.


Android Spell Checker API

Hit “Get Suggestions” Button...


Android Spell Checker API

You might also like