EmojiEditTextHelper

class EmojiEditTextHelper


Utility class to enhance custom EditText widgets with EmojiCompat.

public class MyEmojiEditText extends EditText {
     public MyEmojiEditText(Context context) {
         super(context);
         init();
     }
     // ...
     private void init() {
         super.setKeyListener(getEmojiEditTextHelper().getKeyListener(getKeyListener()));
     }

     @Override
     public void setKeyListener(android.text.method.KeyListener keyListener) {
         super.setKeyListener(getEmojiEditTextHelper().getKeyListener(keyListener));
     }

     @Override
     public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
         InputConnection inputConnection = super.onCreateInputConnection(outAttrs);
         return getEmojiEditTextHelper().onCreateInputConnection(inputConnection, outAttrs);
     }

     private EmojiEditTextHelper getEmojiEditTextHelper() {
         if (mEmojiEditTextHelper == null) {
             mEmojiEditTextHelper = new EmojiEditTextHelper(this);
         }
         return mEmojiEditTextHelper;
     }
}

Summary

Public constructors

Default constructor.

EmojiEditTextHelper(
    editText: EditText,
    expectInitializedEmojiCompat: Boolean
)

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

Public functions

KeyListener?
getKeyListener(keyListener: KeyListener?)

Attaches EmojiCompat KeyListener to the widget.

Int

Returns the maximum number of EmojiSpans to be added to a CharSequence.

Boolean

If this helper should add new EmojiSpans to display emoji using the emoji font when text changes.

InputConnection?
onCreateInputConnection(
    inputConnection: InputConnection?,
    outAttrs: EditorInfo
)

Updates the InputConnection with emoji support.

Unit
setEnabled(isEnabled: Boolean)

When helper is enabled, it will process text changes to add appropriate EmojiSpans for display.

Unit
setMaxEmojiCount(maxEmojiCount: @IntRange(from = 0) Int)

Set the maximum number of EmojiSpans to be added to a CharSequence.

Public constructors

EmojiEditTextHelper

Added in 1.0.0
EmojiEditTextHelper(editText: EditText)

Default constructor.

Parameters
editText: EditText

EditText instance

EmojiEditTextHelper

Added in 1.0.0
EmojiEditTextHelper(
    editText: EditText,
    expectInitializedEmojiCompat: Boolean
)

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
editText: EditText

EditText instance

expectInitializedEmojiCompat: Boolean

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 functions

getKeyListener

Added in 1.0.0
fun getKeyListener(keyListener: KeyListener?): KeyListener?

Attaches EmojiCompat KeyListener to the widget. Should be called from setKeyListener. Existing keyListener is wrapped into EmojiCompat KeyListener. When used on devices running API 18 or below, this method returns keyListener that is given as a parameter.

Parameters
keyListener: KeyListener?

KeyListener passed into setKeyListener

Returns
KeyListener?

a new KeyListener instance that wraps keyListener, or null if passed null.

getMaxEmojiCount

Added in 1.0.0
fun getMaxEmojiCount(): Int

Returns the maximum number of EmojiSpans to be added to a CharSequence.

isEnabled

Added in 1.0.0
fun isEnabled(): Boolean

If this helper should add new EmojiSpans to display emoji using the emoji font when text changes.

Returns
Boolean

true if the helper will process emoji spans.

onCreateInputConnection

Added in 1.0.0
fun onCreateInputConnection(
    inputConnection: InputConnection?,
    outAttrs: EditorInfo
): InputConnection?

Updates the InputConnection with emoji support. Should be called from onCreateInputConnection. When used on devices running API 18 or below, this method returns inputConnection that is given as a parameter. If inputConnection is null, returns null.

Parameters
inputConnection: InputConnection?

InputConnection instance created by TextView

outAttrs: EditorInfo

EditorInfo passed into onCreateInputConnection

Returns
InputConnection?

a new InputConnection instance that wraps inputConnection

setEnabled

Added in 1.0.0
fun setEnabled(isEnabled: Boolean): Unit

When helper is enabled, it will process text changes to add appropriate EmojiSpans for display. When helper is disabled, it will not process text changes, but existing spans will not be removed by disabling.

Parameters
isEnabled: Boolean

if this helper should process spans

setMaxEmojiCount

Added in 1.0.0
fun setMaxEmojiCount(maxEmojiCount: @IntRange(from = 0) Int): Unit

Set the maximum number of EmojiSpans to be added to a CharSequence. The number of spans in a CharSequence affects the performance of the EditText insert/delete operations. Insert/delete operations slow down as the number of spans increases.

Parameters
maxEmojiCount: @IntRange(from = 0) Int

maximum number of EmojiSpans to be added to a single CharSequence, should be equal or greater than 0

See also
process