TextClassification

public final class TextClassification
extends Object implements Parcelable

java.lang.Object
   ↳ android.view.textclassifier.TextClassification


Information for generating a widget to handle classified text.

A TextClassification object contains icons, labels, onClickListeners and intents that may be used to build a widget that can be used to act on classified text. There is the concept of a primary action and other secondary actions.

e.g. building a view that, when clicked, shares the classified text with the preferred app:

// Called preferably outside the UiThread.
   TextClassification classification = textClassifier.classifyText(allText, 10, 25);

   // Called on the UiThread.
   Button button = new Button(context);
   button.setCompoundDrawablesWithIntrinsicBounds(classification.getIcon(), null, null, null);
   button.setText(classification.getLabel());
   button.setOnClickListener(v -> context.startActivity(classification.getIntent()));
 

e.g. starting an action mode with menu items that can handle the classified text:

// Called preferably outside the UiThread.
   final TextClassification classification = textClassifier.classifyText(allText, 10, 25);

   // Called on the UiThread.
   view.startActionMode(new ActionMode.Callback() {

       public boolean onCreateActionMode(ActionMode mode, Menu menu) {
           for (int i = 0; i < classification.getActions().size(); ++i) {
              RemoteAction action = classification.getActions().get(i);
              menu.add(Menu.NONE, i, 20, action.getTitle())
                 .setIcon(action.getIcon());
           }
           return true;
       }

       public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
           classification.getActions().get(item.getItemId()).getActionIntent().send();
           return true;
       }

       ...
   });
 

Summary

Nested classes

class TextClassification.Builder

Builder for building TextClassification objects. 

class TextClassification.Request

A request object for generating TextClassification. 

Inherited constants

Fields

public static final Creator<TextClassification> CREATOR

Public methods

int describeContents()

Describe the kinds of special objects contained in this Parcelable instance's marshaled representation.

List<RemoteAction> getActions()

Returns a list of actions that may be performed on the text.

float getConfidenceScore(String entity)

Returns the confidence score for the specified entity.

String getEntity(int index)

Returns the entity at the specified index.

int getEntityCount()

Returns the number of entities found in the classified text.

Drawable getIcon()

This method was deprecated in API level P. Use getActions() instead.

String getId()

Returns the id, if one exists, for this object.

Intent getIntent()

This method was deprecated in API level P. Use getActions() instead.

CharSequence getLabel()

This method was deprecated in API level P. Use getActions() instead.

View.OnClickListener getOnClickListener()

This method was deprecated in API level P. Use getActions() instead.

String getText()

Gets the classified text.

String toString()

Returns a string representation of the object.

void writeToParcel(Parcel dest, int flags)

Flatten this object in to a Parcel.

Inherited methods

Fields

Public methods

describeContents

int describeContents ()

Describe the kinds of special objects contained in this Parcelable instance's marshaled representation. For example, if the object will include a file descriptor in the output of writeToParcel(Parcel, int), the return value of this method must include the CONTENTS_FILE_DESCRIPTOR bit.

Returns
int a bitmask indicating the set of special object types marshaled by this Parcelable object instance.

getActions

List<RemoteAction> getActions ()

Returns a list of actions that may be performed on the text. The list is ordered based on the likelihood that a user will use the action, with the most likely action appearing first.

Returns
List<RemoteAction>

getConfidenceScore

added in API level 26
float getConfidenceScore (String entity)

Returns the confidence score for the specified entity. The value ranges from 0 (low confidence) to 1 (high confidence). 0 indicates that the entity was not found for the classified text.

Parameters
entity String

Value is TYPE_UNKNOWN, TYPE_OTHER, TYPE_EMAIL, TYPE_PHONE, TYPE_ADDRESS, TYPE_URL, TYPE_DATE, TYPE_DATE_TIME or TYPE_FLIGHT_NUMBER.

Returns
float

Value is between 0.0 and 1.0 inclusive.

getEntity

added in API level 26
String getEntity (int index)

Returns the entity at the specified index. Entities are ordered from high confidence to low confidence.

Parameters
index int

Returns
String

This value will never be null.

Value is TYPE_UNKNOWN, TYPE_OTHER, TYPE_EMAIL, TYPE_PHONE, TYPE_ADDRESS, TYPE_URL, TYPE_DATE, TYPE_DATE_TIME or TYPE_FLIGHT_NUMBER.

Throws
IndexOutOfBoundsException if the specified index is out of range.

getEntityCount

added in API level 26
int getEntityCount ()

Returns the number of entities found in the classified text.

Returns
int

getIcon

added in API level 26
Drawable getIcon ()

This method was deprecated in API level P.
Use getActions() instead.

Returns an icon that may be rendered on a widget used to act on the classified text.

Returns
Drawable

This value may be null.

getId

String getId ()

Returns the id, if one exists, for this object.

Returns
String

This value may be null.

getIntent

added in API level 26
Intent getIntent ()

This method was deprecated in API level P.
Use getActions() instead.

Returns an intent that may be fired to act on the classified text.

Returns
Intent

This value may be null.

getLabel

added in API level 26
CharSequence getLabel ()

This method was deprecated in API level P.
Use getActions() instead.

Returns a label that may be rendered on a widget used to act on the classified text.

Returns
CharSequence

This value may be null.

getOnClickListener

added in API level 26
View.OnClickListener getOnClickListener ()

This method was deprecated in API level P.
Use getActions() instead.

Returns the OnClickListener that may be triggered to act on the classified text. This field is not parcelable and will be null for all objects read from a parcel. Instead, call Context#startActivity(Intent) with the result of #getSecondaryIntent(int). Note that this may fail if the activity doesn't have permission to send the intent.

Returns
View.OnClickListener

This value may be null.

getText

added in API level 26
String getText ()

Gets the classified text.

Returns
String

This value may be null.

toString

added in API level 26
String toString ()

Returns a string representation of the object. In general, the toString method returns a string that "textually represents" this object. The result should be a concise but informative representation that is easy for a person to read. It is recommended that all subclasses override this method.

The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of:

 getClass().getName() + '@' + Integer.toHexString(hashCode())
 

Returns
String a string representation of the object.

writeToParcel

void writeToParcel (Parcel dest, 
                int flags)

Flatten this object in to a Parcel.

Parameters
dest Parcel: The Parcel in which the object should be written.

flags int: Additional flags about how the object should be written. May be 0 or PARCELABLE_WRITE_RETURN_VALUE.