# EmojiEditTextHelper

Artifact: [androidx.emoji2:emoji2-views-helper](https://developer.android.com/jetpack/androidx/releases/emoji2) [View Source](https://cs.android.com/search?q=file:androidx/emoji2/viewsintegration/EmojiEditTextHelper.java+class:androidx.emoji2.viewsintegration.EmojiEditTextHelper) Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/emoji2#1.0.0)

*** ** * ** ***


```
public final class EmojiEditTextHelper
```

<br />

*** ** * ** ***

Utility class to enhance custom EditText widgets with `https://developer.android.com/reference/androidx/emoji2/text/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 |
|---|
| `https://developer.android.com/reference/androidx/emoji2/viewsintegration/EmojiEditTextHelper#EmojiEditTextHelper(android.widget.EditText)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/widget/EditText.html editText)` Default constructor. |
| `https://developer.android.com/reference/androidx/emoji2/viewsintegration/EmojiEditTextHelper#EmojiEditTextHelper(android.widget.EditText,boolean)( @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/widget/EditText.html editText, boolean expectInitializedEmojiCompat )` Allows skipping of all processing until EmojiCompat.init is called. |

| ### Public methods |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/android/text/method/KeyListener.html` | `https://developer.android.com/reference/androidx/emoji2/viewsintegration/EmojiEditTextHelper#getKeyListener(android.text.method.KeyListener)(@https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/android/text/method/KeyListener.html keyListener)` Attaches EmojiCompat KeyListener to the widget. |
| `int` | `https://developer.android.com/reference/androidx/emoji2/viewsintegration/EmojiEditTextHelper#getMaxEmojiCount()()` Returns the maximum number of EmojiSpans to be added to a CharSequence. |
| `boolean` | `https://developer.android.com/reference/androidx/emoji2/viewsintegration/EmojiEditTextHelper#isEnabled()()` If this helper should add new EmojiSpans to display emoji using the emoji font when text changes. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/android/view/inputmethod/InputConnection.html` | `https://developer.android.com/reference/androidx/emoji2/viewsintegration/EmojiEditTextHelper#onCreateInputConnection(android.view.inputmethod.InputConnection,android.view.inputmethod.EditorInfo)( @https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/android/view/inputmethod/InputConnection.html inputConnection, @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/view/inputmethod/EditorInfo.html outAttrs )` Updates the InputConnection with emoji support. |
| `void` | `https://developer.android.com/reference/androidx/emoji2/viewsintegration/EmojiEditTextHelper#setEnabled(boolean)(boolean isEnabled)` When helper is enabled, it will process text changes to add appropriate EmojiSpans for display. |
| `void` | `https://developer.android.com/reference/androidx/emoji2/viewsintegration/EmojiEditTextHelper#setMaxEmojiCount(int)(@https://developer.android.com/reference/androidx/annotation/IntRange(from = 0) int maxEmojiCount)` Set the maximum number of EmojiSpans to be added to a CharSequence. |

## Public constructors

### EmojiEditTextHelper

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/emoji2#1.0.0)

```
public EmojiEditTextHelper(@NonNull EditText editText)
```

Default constructor.

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/widget/EditText.html editText` | EditText instance |

### EmojiEditTextHelper

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/emoji2#1.0.0)

```
public EmojiEditTextHelper(
    @NonNull EditText editText,
    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 `https://developer.android.com/reference/androidx/emoji2/viewsintegration/EmojiEditTextHelper#setEnabled(boolean)`.

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/widget/EditText.html editText` | EditText 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

### getKeyListener

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/emoji2#1.0.0)

```
public @Nullable KeyListener getKeyListener(@Nullable KeyListener keyListener)
```

Attaches EmojiCompat KeyListener to the widget. Should be called from `https://developer.android.com/reference/android/widget/TextView.html#setKeyListener(android.text.method.KeyListener)`. 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 |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/android/text/method/KeyListener.html keyListener` | KeyListener passed into `https://developer.android.com/reference/android/widget/TextView.html#setKeyListener(android.text.method.KeyListener)` |

| Returns |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/android/text/method/KeyListener.html` | a new KeyListener instance that wraps `keyListener`, or null if passed null. |

### getMaxEmojiCount

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/emoji2#1.0.0)

```
public int getMaxEmojiCount()
```

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

| See also |
|---|---|
| `https://developer.android.com/reference/androidx/emoji2/viewsintegration/EmojiEditTextHelper#setMaxEmojiCount(int)` |   |
| `https://developer.android.com/reference/androidx/emoji2/text/EmojiCompat#process(java.lang.CharSequence,int,int,int)` |   |

### isEnabled

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/emoji2#1.0.0)

```
public boolean isEnabled()
```

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](https://developer.android.com/jetpack/androidx/releases/emoji2#1.0.0)

```
public @Nullable InputConnection onCreateInputConnection(
    @Nullable InputConnection inputConnection,
    @NonNull EditorInfo outAttrs
)
```

Updates the InputConnection with emoji support. Should be called from `https://developer.android.com/reference/android/widget/TextView.html#onCreateInputConnection(android.view.inputmethod.EditorInfo)`. 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 |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/android/view/inputmethod/InputConnection.html inputConnection` | InputConnection instance created by TextView |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/view/inputmethod/EditorInfo.html outAttrs` | EditorInfo passed into `https://developer.android.com/reference/android/widget/TextView.html#onCreateInputConnection(android.view.inputmethod.EditorInfo)` |

| Returns |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/android/view/inputmethod/InputConnection.html` | a new InputConnection instance that wraps `inputConnection` |

### setEnabled

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/emoji2#1.0.0)

```
public void setEnabled(boolean isEnabled)
```

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 |
|---|---|
| `boolean isEnabled` | if this helper should process spans |

### setMaxEmojiCount

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/emoji2#1.0.0)

```
public void setMaxEmojiCount(@IntRange(from = 0) int maxEmojiCount)
```

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 |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/IntRange(from = 0) int maxEmojiCount` | maximum number of EmojiSpans to be added to a single CharSequence, should be equal or greater than 0 |

| See also |
|---|---|
| `https://developer.android.com/reference/androidx/emoji2/text/EmojiCompat#process(java.lang.CharSequence,int,int,int)` |   |