public class

EmojiAppCompatEditText

extends AppCompatEditText

 java.lang.Object

↳EditText

androidx.appcompat.widget.AppCompatEditText

↳androidx.emoji.widget.EmojiAppCompatEditText

Gradle dependencies

compile group: 'androidx.emoji', name: 'emoji-appcompat', version: '1.2.0-alpha03'

  • groupId: androidx.emoji
  • artifactId: emoji-appcompat
  • version: 1.2.0-alpha03

Artifact androidx.emoji:emoji-appcompat:1.2.0-alpha03 it located at Google repository (https://maven.google.com/)

Androidx artifact mapping:

androidx.emoji:emoji-appcompat com.android.support:support-emoji-appcompat

Androidx class mapping:

androidx.emoji.widget.EmojiAppCompatEditText android.support.text.emoji.widget.EmojiAppCompatEditText

Overview

AppCompatEditText widget enhanced with emoji capability by using EmojiEditTextHelper. When used on devices running API 18 or below, this widget acts as a regular AppCompatEditText.

Summary

Constructors
publicEmojiAppCompatEditText(Context context)

publicEmojiAppCompatEditText(Context context, AttributeSet attrs)

publicEmojiAppCompatEditText(Context context, AttributeSet attrs, int defStyleAttr)

Methods
public intgetMaxEmojiCount()

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

public InputConnectiononCreateInputConnection(EditorInfo outAttrs)

If a listener is set, the returned will use it to handle calls to .

public voidsetKeyListener(KeyListener keyListener)

Adds EmojiCompat KeyListener to correctly edit multi-codepoint emoji when they've been converted to spans.

public voidsetMaxEmojiCount(int maxEmojiCount)

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

from AppCompatEditTextdrawableStateChanged, getCustomSelectionActionModeCallback, getSupportBackgroundTintList, getSupportBackgroundTintMode, getSupportCompoundDrawablesTintList, getSupportCompoundDrawablesTintMode, getText, getTextClassifier, isEmojiCompatEnabled, onDragEvent, onReceiveContent, onTextContextMenuItem, setBackgroundDrawable, setBackgroundResource, setCompoundDrawables, setCompoundDrawablesRelative, setCustomSelectionActionModeCallback, setEmojiCompatEnabled, setSupportBackgroundTintList, setSupportBackgroundTintMode, setSupportCompoundDrawablesTintList, setSupportCompoundDrawablesTintMode, setTextAppearance, setTextClassifier
from java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructors

public EmojiAppCompatEditText(Context context)

public EmojiAppCompatEditText(Context context, AttributeSet attrs)

public EmojiAppCompatEditText(Context context, AttributeSet attrs, int defStyleAttr)

Methods

public void setKeyListener(KeyListener keyListener)

Adds EmojiCompat KeyListener to correctly edit multi-codepoint emoji when they've been converted to spans.

public InputConnection onCreateInputConnection(EditorInfo outAttrs)

If a listener is set, the returned will use it to handle calls to .

public void setMaxEmojiCount(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:

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

See also: {@link androidx.emoji.R.attr#maxEmojiCount}

public int getMaxEmojiCount()

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

See also: EmojiAppCompatEditText.setMaxEmojiCount(int), {@link androidx.emoji.R.attr#maxEmojiCount}

Source

/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package androidx.emoji.widget;

import android.content.Context;
import android.text.method.KeyListener;
import android.util.AttributeSet;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputConnection;

import androidx.annotation.IntRange;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatEditText;
import androidx.emoji.text.EmojiCompat;

/**
 * AppCompatEditText widget enhanced with emoji capability by using {@link EmojiEditTextHelper}.
 * When used on devices running API 18 or below, this widget acts as a regular
 * {@link AppCompatEditText}.
 *
 * {@link androidx.emoji.R.attr#maxEmojiCount}
 */
public class EmojiAppCompatEditText extends AppCompatEditText {
    private EmojiEditTextHelper mEmojiEditTextHelper;

    /**
     * Prevent calling {@link #init(AttributeSet, int)} multiple times in case super() constructors
     * call other constructors.
     */
    private boolean mInitialized;

    public EmojiAppCompatEditText(Context context) {
        super(context);
        init(null /*attrs*/, 0 /*defStyleAttr*/);
    }

    public EmojiAppCompatEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(attrs, androidx.appcompat.R.attr.editTextStyle);
    }

    public EmojiAppCompatEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(attrs, defStyleAttr);
    }

    private void init(@Nullable AttributeSet attrs, int defStyleAttr) {
        if (!mInitialized) {
            mInitialized = true;
            final EditTextAttributeHelper attrHelper = new EditTextAttributeHelper(this, attrs,
                    defStyleAttr, 0);
            setMaxEmojiCount(attrHelper.getMaxEmojiCount());
            setKeyListener(super.getKeyListener());
        }
    }

    @Override
    public void setKeyListener(@Nullable KeyListener keyListener) {
        if (keyListener != null) {
            keyListener = getEmojiEditTextHelper().getKeyListener(keyListener);
        }
        super.setKeyListener(keyListener);
    }

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

    /**
     * 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.
     *
     * @param maxEmojiCount maximum number of EmojiSpans to be added to a single CharSequence,
     *                      should be equal or greater than 0
     *
     * @see EmojiCompat#process(CharSequence, int, int, int)
     *
     * {@link androidx.emoji.R.attr#maxEmojiCount}
     */
    public void setMaxEmojiCount(@IntRange(from = 0) int maxEmojiCount) {
        getEmojiEditTextHelper().setMaxEmojiCount(maxEmojiCount);
    }

    /**
     * Returns the maximum number of EmojiSpans to be added to a CharSequence.
     *
     * @see #setMaxEmojiCount(int)
     * @see EmojiCompat#process(CharSequence, int, int, int)
     *
     * {@link androidx.emoji.R.attr#maxEmojiCount}
     */
    public int getMaxEmojiCount() {
        return getEmojiEditTextHelper().getMaxEmojiCount();
    }

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