Skip to content

Most visited

Recently visited

navigation

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) {
           // Add the "primary" action.
           if (thisAppHasPermissionToInvokeIntent(classification.getIntent())) {
              menu.add(Menu.NONE, 0, 20, classification.getLabel())
                 .setIcon(classification.getIcon())
                 .setIntent(classification.getIntent());
           }
           // Add the "secondary" actions.
           for (int i = 0; i < classification.getSecondaryActionsCount(); i++) {
               if (thisAppHasPermissionToInvokeIntent(classification.getSecondaryIntent(i))) {
                   menu.add(Menu.NONE, i + 1, 20, classification.getSecondaryLabel(i))
                      .setIcon(classification.getSecondaryIcon(i))
                      .setIntent(classification.getSecondaryIntent(i));
               }
           }
           return true;
       }

       public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
           context.startActivity(item.getIntent());
           return true;
       }

       ...
   });
 

Summary

Nested classes

class TextClassification.Builder

Builder for building TextClassification objects. 

class TextClassification.Options

Optional input parameters 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.

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()

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

Intent getIntent()

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

CharSequence getLabel()

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

View.OnClickListener getOnClickListener()

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

int getSecondaryActionsCount()

Returns the number of secondary actions that are available to act on the classified text.

Drawable getSecondaryIcon(int index)

Returns one of the secondary icons that maybe rendered on a widget used to act on the classified text.

Intent getSecondaryIntent(int index)

Returns one of the secondary intents that may be fired to act on the classified text.

CharSequence getSecondaryLabel(int index)

Returns one of the secondary labels that may be rendered on a widget used to act on the classified text.

String getSignature()

Returns the signature for this object.

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.

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 ()

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

Returns
Drawable

This value may be null.

getIntent

added in API level 26
Intent getIntent ()

Returns the primary 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 ()

Returns a label for the primary intent 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 ()

Returns the primary 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.

getSecondaryActionsCount

int getSecondaryActionsCount ()

Returns the number of secondary actions that are available to act on the classified text.

Note: that there may or may not be a primary action.

Returns
int

getSecondaryIcon

Drawable getSecondaryIcon (int index)

Returns one of the secondary icons that maybe rendered on a widget used to act on the classified text.

Parameters
index int: Index of the action to get the icon for.

Returns
Drawable

This value may be null.

Throws
IndexOutOfBoundsException if the specified index is out of range.

getSecondaryIntent

Intent getSecondaryIntent (int index)

Returns one of the secondary intents that may be fired to act on the classified text.

Parameters
index int: Index of the action to get the intent for.

Returns
Intent

This value may be null.

Throws
IndexOutOfBoundsException if the specified index is out of range.

getSecondaryLabel

CharSequence getSecondaryLabel (int index)

Returns one of the secondary labels that may be rendered on a widget used to act on the classified text.

Parameters
index int: Index of the action to get the label for.

Returns
CharSequence

This value may be null.

Throws
IndexOutOfBoundsException if the specified index is out of range.

getSignature

String getSignature ()

Returns the signature for this object. The TextClassifier that generates this object may use it as a way to internally identify this object.

Returns
String

This value will never 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.

This site uses cookies to store your preferences for site-specific language and display options.

Get the latest Android developer news and tips that will help you find success on Google Play.

* Required Fields

Hooray!

Follow Google Developers on WeChat

Browse this site in ?

You requested a page in , but your language preference for this site is .

Would you like to change your language preference and browse this site in ? If you want to change your language preference later, use the language menu at the bottom of each page.

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

Take a short survey?
Help us improve the Android developer experience. (April 2018 — Developer Survey)