# EmojiEditTextHelper

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

*** ** * ** ***

Kotlin\|[Java](https://developer.android.com/reference/androidx/emoji2/viewsintegration/EmojiEditTextHelper "View this page in Java")

<br />

```
class EmojiEditTextHelper
```

<br />

*** ** * ** ***

Utility class to enhance custom EditText widgets with[EmojiCompat](https://developer.android.com/reference/kotlin/androidx/emoji2/text/EmojiCompat).  

```kotlin
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                                                                                                                                                                                                                              |
|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [EmojiEditTextHelper](https://developer.android.com/reference/kotlin/androidx/emoji2/viewsintegration/EmojiEditTextHelper#EmojiEditTextHelper(android.widget.EditText))`(editText: `[EditText](https://developer.android.com/reference/android/widget/EditText.html)`)` Default constructor.                                                                                                                                                                                     |
| [EmojiEditTextHelper](https://developer.android.com/reference/kotlin/androidx/emoji2/viewsintegration/EmojiEditTextHelper#EmojiEditTextHelper(android.widget.EditText,boolean))`(` ` editText: `[EditText](https://developer.android.com/reference/android/widget/EditText.html)`,` ` expectInitializedEmojiCompat: `[Boolean](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html) `)` Allows skipping of all processing until EmojiCompat.init is called. |

|                                            ### Public functions                                             |
|-------------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [KeyListener](https://developer.android.com/reference/android/text/method/KeyListener.html)`?`              | [getKeyListener](https://developer.android.com/reference/kotlin/androidx/emoji2/viewsintegration/EmojiEditTextHelper#getKeyListener(android.text.method.KeyListener))`(keyListener: `[KeyListener](https://developer.android.com/reference/android/text/method/KeyListener.html)`?)` Attaches EmojiCompat KeyListener to the widget.                                                                                                                                                                                                     |
| [Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)                                 | [getMaxEmojiCount](https://developer.android.com/reference/kotlin/androidx/emoji2/viewsintegration/EmojiEditTextHelper#getMaxEmojiCount())`()` Returns the maximum number of EmojiSpans to be added to a CharSequence.                                                                                                                                                                                                                                                                                                                   |
| [Boolean](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html)                         | [isEnabled](https://developer.android.com/reference/kotlin/androidx/emoji2/viewsintegration/EmojiEditTextHelper#isEnabled())`()` If this helper should add new EmojiSpans to display emoji using the emoji font when text changes.                                                                                                                                                                                                                                                                                                       |
| [InputConnection](https://developer.android.com/reference/android/view/inputmethod/InputConnection.html)`?` | [onCreateInputConnection](https://developer.android.com/reference/kotlin/androidx/emoji2/viewsintegration/EmojiEditTextHelper#onCreateInputConnection(android.view.inputmethod.InputConnection,android.view.inputmethod.EditorInfo))`(` ` inputConnection: `[InputConnection](https://developer.android.com/reference/android/view/inputmethod/InputConnection.html)`?,` ` outAttrs: `[EditorInfo](https://developer.android.com/reference/android/view/inputmethod/EditorInfo.html) `)` Updates the InputConnection with emoji support. |
| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)                               | [setEnabled](https://developer.android.com/reference/kotlin/androidx/emoji2/viewsintegration/EmojiEditTextHelper#setEnabled(boolean))`(isEnabled: `[Boolean](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html)`)` When helper is enabled, it will process text changes to add appropriate EmojiSpans for display.                                                                                                                                                                                                |
| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)                               | [setMaxEmojiCount](https://developer.android.com/reference/kotlin/androidx/emoji2/viewsintegration/EmojiEditTextHelper#setMaxEmojiCount(int))`(maxEmojiCount: @`[IntRange](https://developer.android.com/reference/kotlin/androidx/annotation/IntRange)`(from = 0) `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)`)` 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)  

```
EmojiEditTextHelper(editText: EditText)
```

Default constructor.  

|                                          Parameters                                          |
|----------------------------------------------------------------------------------------------|-------------------|
| `editText: `[EditText](https://developer.android.com/reference/android/widget/EditText.html) | EditText instance |

### EmojiEditTextHelper

Added in[1.0.0](https://developer.android.com/jetpack/androidx/releases/emoji2#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](https://developer.android.com/reference/kotlin/androidx/emoji2/viewsintegration/EmojiEditTextHelper#setEnabled(boolean)).  

|                                                     Parameters                                                      |
|---------------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `editText: `[EditText](https://developer.android.com/reference/android/widget/EditText.html)                        | EditText instance                                                                                                                                                         |
| `expectInitializedEmojiCompat: `[Boolean](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html) | 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](https://developer.android.com/jetpack/androidx/releases/emoji2#1.0.0)  

```
fun getKeyListener(keyListener: KeyListener?): KeyListener?
```

Attaches EmojiCompat KeyListener to the widget. Should be called from[setKeyListener](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                                                   |
|---------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `keyListener: `[KeyListener](https://developer.android.com/reference/android/text/method/KeyListener.html)`?` | KeyListener passed into[setKeyListener](https://developer.android.com/reference/android/widget/TextView.html#setKeyListener(android.text.method.KeyListener)) |

|                                            Returns                                             |
|------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------|
| [KeyListener](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)  

```
fun getMaxEmojiCount(): Int
```

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

|                                                                   See also                                                                    |
|-----------------------------------------------------------------------------------------------------------------------------------------------|---|
| [setMaxEmojiCount](https://developer.android.com/reference/kotlin/androidx/emoji2/viewsintegration/EmojiEditTextHelper#setMaxEmojiCount(int)) |   |
| [process](https://developer.android.com/reference/kotlin/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)  

```
fun isEnabled(): Boolean
```

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

|                                       Returns                                       |
|-------------------------------------------------------------------------------------|----------------------------------------------|
| [Boolean](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html) | 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)  

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

Updates the InputConnection with emoji support. Should be called from[onCreateInputConnection](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                                                           |
|--------------------------------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `inputConnection: `[InputConnection](https://developer.android.com/reference/android/view/inputmethod/InputConnection.html)`?` | InputConnection instance created by TextView                                                                                                                                       |
| `outAttrs: `[EditorInfo](https://developer.android.com/reference/android/view/inputmethod/EditorInfo.html)                     | EditorInfo passed into[onCreateInputConnection](https://developer.android.com/reference/android/widget/TextView.html#onCreateInputConnection(android.view.inputmethod.EditorInfo)) |

|                                                   Returns                                                   |
|-------------------------------------------------------------------------------------------------------------|------------------------------------------------------------|
| [InputConnection](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)  

```
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](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html) | if this helper should process spans |

### setMaxEmojiCount

Added in[1.0.0](https://developer.android.com/jetpack/androidx/releases/emoji2#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](https://developer.android.com/reference/kotlin/androidx/annotation/IntRange)`(from = 0) `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) | maximum number of EmojiSpans to be added to a single CharSequence, should be equal or greater than 0 |

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