TextClassification

class TextClassification : Parcelable
kotlin.Any
   ↳ 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 -> classification.getActions().get(0).getActionIntent().send()); 

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; } ... }); 

Requires API level 26 (Android 8.0, Oreo)

Summary

Nested classes

Builder for building TextClassification objects.

A request object for generating TextClassification.

Inherited constants
Public methods
Int

Returns the number of entities found in the classified text.

Float

Returns the confidence score for the specified entity.

Drawable?

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

CharSequence?

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

View.OnClickListener?

Returns the OnClickListener that may be triggered to act on the classified text.

String

Returns a string representation of the object.

String?

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

String
getEntity(index: Int)

Returns the entity at the specified index.

Intent?

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

String?

Gets the classified text.

Unit
writeToParcel(dest: Parcel!, flags: Int)

Flatten this object in to a Parcel.

MutableList<RemoteAction!>!

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

Int

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

Properties
static Parcelable.Creator<TextClassification!>!

Requires API level 28 (Android 8.+, P)

Public methods

getEntityCount

added in API level 26
fun getEntityCount(): Int

Returns the number of entities found in the classified text.
Requires API level 26 (Android 8.0, Oreo)

Return
Int: Value is 0 or greater

getConfidenceScore

added in API level 26
fun getConfidenceScore(entity: String!): Float

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.
Requires API level 26 (Android 8.0, Oreo)

Parameters
entity String!: Value is android.view.textclassifier.TextClassifier#TYPE_UNKNOWN, android.view.textclassifier.TextClassifier#TYPE_OTHER, android.view.textclassifier.TextClassifier#TYPE_EMAIL, android.view.textclassifier.TextClassifier#TYPE_PHONE, android.view.textclassifier.TextClassifier#TYPE_ADDRESS, android.view.textclassifier.TextClassifier#TYPE_URL, android.view.textclassifier.TextClassifier#TYPE_DATE, android.view.textclassifier.TextClassifier#TYPE_DATE_TIME, or android.view.textclassifier.TextClassifier#TYPE_FLIGHT_NUMBER
Return
Float: Value is between 0.0 and 1.0 inclusive

getIcon

added in API level 26
fun getIcon(): Drawable?

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

NOTE: This field is not parcelable and only represents the icon of the first RemoteAction (if one exists) when this object is read from a parcel.
Requires API level 26 (Android 8.0, Oreo)

Return
Drawable?: This value may be null.

getLabel

added in API level 26
fun getLabel(): CharSequence?

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

NOTE: This field is not parcelable and only represents the label of the first RemoteAction (if one exists) when this object is read from a parcel.
Requires API level 26 (Android 8.0, Oreo)

Return
CharSequence?: This value may be null.

getOnClickListener

added in API level 26
fun getOnClickListener(): View.OnClickListener?

Returns the OnClickListener that may be triggered to act on the classified text.

NOTE: This field is not parcelable and only represents the first RemoteAction (if one exists) when this object is read from a parcel.
Requires API level 26 (Android 8.0, Oreo)

Return
View.OnClickListener?: This value may be null.

toString

added in API level 26
fun toString(): String

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()) 
Requires API level 26 (Android 8.0, Oreo)
Return
String: a string representation of the object.

getId

added in API level 28
fun getId(): String?

Returns the id, if one exists, for this object.
Requires API level 28 (Android 8.+, P)

Return
String?: This value may be null.

getIntent

added in API level 26
fun getIntent(): Intent?

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

NOTE: This field is not parcelled and will always return null when this object is read from a parcel.
Requires API level 26 (Android 8.0, Oreo)

getText

added in API level 26
fun getText(): String?

Gets the classified text.
Requires API level 26 (Android 8.0, Oreo)

Return
String?: This value may be null.

writeToParcel

added in API level 28
fun writeToParcel(dest: Parcel!, flags: Int): Unit

Flatten this object in to a Parcel. Requires API level 28 (Android 8.+, P)

Parameters
dest Parcel!: The Parcel in which the object should be written.
flags Parcel!: Additional flags about how the object should be written. May be 0 or #PARCELABLE_WRITE_RETURN_VALUE. Value is either 0 or a combination of android.os.Parcelable#PARCELABLE_WRITE_RETURN_VALUE, and android.os.Parcelable.PARCELABLE_ELIDE_DUPLICATES

getActions

added in API level 28
fun getActions(): MutableList<RemoteAction!>!

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.
Requires API level 28 (Android 8.+, P)

describeContents

added in API level 28
fun describeContents(): Int

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. Requires API level 28 (Android 8.+, P)

Return
Int: a bitmask indicating the set of special object types marshaled by this Parcelable object instance. Value is either 0 or android.os.Parcelable#CONTENTS_FILE_DESCRIPTOR

Properties

CREATOR

added in API level 28
static val CREATOR: Parcelable.Creator<TextClassification!>!

Requires API level 28 (Android 8.+, P)