EmojiEditTextHelper
public final 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 |
|---|
EmojiEditTextHelper(@NonNull EditText editText)Default constructor. |
EmojiEditTextHelper(Allows skipping of all processing until EmojiCompat.init is called. |
Public methods |
|
|---|---|
@Nullable KeyListener |
getKeyListener(@Nullable 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. |
@Nullable InputConnection |
onCreateInputConnection(Updates the InputConnection with emoji support. |
void |
setEnabled(boolean isEnabled)When helper is enabled, it will process text changes to add appropriate EmojiSpans for display. |
void |
setMaxEmojiCount(@IntRange(from = 0) int maxEmojiCount)Set the maximum number of EmojiSpans to be added to a CharSequence. |
Public constructors
EmojiEditTextHelper
public EmojiEditTextHelper(@NonNull EditText editText)
Default constructor.
EmojiEditTextHelper
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 setEnabled.
Public methods
getKeyListener
public @Nullable KeyListener getKeyListener(@Nullable 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 | |
|---|---|
@Nullable KeyListener keyListener |
KeyListener passed into |
| Returns | |
|---|---|
@Nullable KeyListener |
a new KeyListener instance that wraps |
getMaxEmojiCount
public int getMaxEmojiCount()
Returns the maximum number of EmojiSpans to be added to a CharSequence.
| See also | |
|---|---|
setMaxEmojiCount |
|
process |
isEnabled
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
public @Nullable InputConnection onCreateInputConnection(
@Nullable InputConnection inputConnection,
@NonNull EditorInfo outAttrs
)
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 | |
|---|---|
@Nullable InputConnection inputConnection |
InputConnection instance created by TextView |
@NonNull EditorInfo outAttrs |
EditorInfo passed into |
| Returns | |
|---|---|
@Nullable InputConnection |
a new InputConnection instance that wraps |
setEnabled
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
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 | |
|---|---|
@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 | |
|---|---|
process |