Android Dev Summit, October 23-24: two days of technical content, directly from the Android team. Sign-up for livestream updates.

EmojiEditTextHelper

class EmojiEditTextHelper
kotlin.Any
   ↳ androidx.emoji.widget.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

<init>(@NonNull editText: EditText)

Default constructor.

Public methods

KeyListener
getKeyListener(@NonNull keyListener: KeyListener)

Attaches EmojiCompat KeyListener to the widget.

Int

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

InputConnection?
onCreateInputConnection(@Nullable inputConnection: InputConnection?, @NonNull outAttrs: EditorInfo)

Updates the InputConnection with emoji support.

Unit
setMaxEmojiCount(maxEmojiCount: Int)

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

Public constructors

<init>

EmojiEditTextHelper(@NonNull editText: EditText)

Default constructor.

Parameters
editText EditText: EditText instance

Public methods

getKeyListener

@NonNull fun getKeyListener(@NonNull keyListener: KeyListener): KeyListener

Attaches EmojiCompat KeyListener to the widget. Should be called from . 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 TextView#setKeyListener(KeyListener)
Return
KeyListener: a new KeyListener instance that wraps keyListener.

getMaxEmojiCount

fun getMaxEmojiCount(): Int

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

onCreateInputConnection

@Nullable fun onCreateInputConnection(@Nullable inputConnection: InputConnection?, @NonNull outAttrs: EditorInfo): InputConnection?

Updates the InputConnection with emoji support. Should be called from . 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 InputConnection?: EditorInfo passed into TextView#onCreateInputConnection(EditorInfo)
Return
InputConnection?: a new InputConnection instance that wraps inputConnection

setMaxEmojiCount

fun setMaxEmojiCount(maxEmojiCount: 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 Int: maximum number of EmojiSpans to be added to a single CharSequence, should be equal or greater than 0