EmojiTextViewHelper

public final class EmojiTextViewHelper


Utility class to enhance custom TextView widgets with EmojiCompat.

public class MyEmojiTextView extends TextView {
    public MyEmojiTextView(Context context) {
        super(context);
        init();
    }
    // ..
    private void init() {
        getEmojiTextViewHelper().updateTransformationMethod();
    }

    @Override
    public void setFilters(InputFilter[] filters) {
        super.setFilters(getEmojiTextViewHelper().getFilters(filters));
    }

    @Override
    public void setAllCaps(boolean allCaps) {
        super.setAllCaps(allCaps);
        getEmojiTextViewHelper().setAllCaps(allCaps);
    }

    private EmojiTextViewHelper getEmojiTextViewHelper() {
        if (mEmojiTextViewHelper == null) {
            mEmojiTextViewHelper = new EmojiTextViewHelper(this);
        }
        return mEmojiTextViewHelper;
    }
}

Summary

Public constructors

Default constructor.

EmojiTextViewHelper(
    @NonNull TextView textView,
    boolean expectInitializedEmojiCompat
)

Allows skipping of all processing until EmojiCompat.init is called.

Public methods

@NonNull InputFilter[]
getFilters(@NonNull InputFilter[] filters)

Appends EmojiCompat InputFilters to the widget InputFilters.

boolean
void
setAllCaps(boolean allCaps)

Call when allCaps is set on TextView.

void
setEnabled(boolean enabled)

When enabled, methods will have their documented behavior.

void

Updates widget's TransformationMethod so that the transformed text can be processed.

@Nullable TransformationMethod

Returns transformation method that can update the transformed text to display emojis.

Public constructors

EmojiTextViewHelper

Added in 1.0.0
public EmojiTextViewHelper(@NonNull TextView textView)

Default constructor.

Parameters
@NonNull TextView textView

TextView instance

EmojiTextViewHelper

Added in 1.0.0
public EmojiTextViewHelper(
    @NonNull TextView textView,
    boolean expectInitializedEmojiCompat
)

Allows skipping of all processing until EmojiCompat.init is called. This is useful when integrating EmojiTextViewHelper into libraries that subclass TextView that do not have control over EmojiCompat initialization by the app that uses the TextView subclass. If this helper is initialized prior to EmojiCompat.init, the TextView it's configuring will not display emoji using EmojiCompat after init is called until the transformation method and filter are updated. The easiest way to do that is call setEnabled.

Parameters
@NonNull TextView textView

TextView instance

boolean expectInitializedEmojiCompat

if true, this helper will assume init has been called and throw if it has not. If false, the methods on this helper will have no effect until EmojiCompat.init is called.

Public methods

getFilters

Added in 1.0.0
public @NonNull InputFilter[] getFilters(@NonNull InputFilter[] filters)

Appends EmojiCompat InputFilters to the widget InputFilters. Should be called by setFilters to update the InputFilters. When used on devices running API 18 or below, this method returns filters that is given as a parameter.

Parameters
@NonNull InputFilter[] filters

InputFilter array passed to setFilters

Returns
@NonNull InputFilter[]

same copy if the array already contains EmojiCompat InputFilter. A new array copy if not.

isEnabled

Added in 1.0.0
public boolean isEnabled()
Returns
boolean

current enabled state for this helper

setAllCaps

Added in 1.0.0
public void setAllCaps(boolean allCaps)

Call when allCaps is set on TextView. When used on devices running API 18 or below, this method does nothing.

Parameters
boolean allCaps

allCaps parameter passed to setAllCaps

setEnabled

Added in 1.0.0
public void setEnabled(boolean enabled)

When enabled, methods will have their documented behavior. When disabled, all methods will have no effect and emoji will not be processed. Setting this to disable will also have the side effect of setting both the transformation method and filter if enabled has changed since the last call. By default EmojiTextViewHelper is enabled. You do not need to call updateTransformationMethod again after calling setEnabled.

Parameters
boolean enabled

if this helper should process emoji.

updateTransformationMethod

Added in 1.0.0
public void updateTransformationMethod()

Updates widget's TransformationMethod so that the transformed text can be processed. Should be called in the widget constructor. When used on devices running API 18 or below, this method does nothing.

wrapTransformationMethod

Added in 1.0.0
public @Nullable TransformationMethod wrapTransformationMethod(
    @Nullable TransformationMethod transformationMethod
)

Returns transformation method that can update the transformed text to display emojis. When used on devices running API 18 or below, this method returns transformationMethod that is given as a parameter.

Parameters
@Nullable TransformationMethod transformationMethod

instance to be wrapped