1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.example.android.support.text.emoji; 18 19 import android.content.Context; 20 import android.text.InputFilter; 21 import android.util.AttributeSet; 22 23 import androidx.annotation.Nullable; 24 import androidx.appcompat.widget.AppCompatTextView; 25 import androidx.emoji.widget.EmojiTextViewHelper; 26 27 28 /** 29 * A sample implementation of custom TextView. 30 * 31 * <p>You can use {@link EmojiTextViewHelper} to make your custom TextView compatible with 32 * EmojiCompat.</p> 33 */ 34 public class CustomTextView extends AppCompatTextView { 35 36 private EmojiTextViewHelper mEmojiTextViewHelper; 37 38 public CustomTextView(Context context) { 39 this(context, null); 40 } 41 42 public CustomTextView(Context context, @Nullable AttributeSet attrs) { 43 this(context, attrs, 0); 44 } 45 46 public CustomTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { 47 super(context, attrs, defStyleAttr); 48 getEmojiTextViewHelper().updateTransformationMethod(); 49 } 50 51 @Override 52 public void setFilters(InputFilter[] filters) { 53 super.setFilters(getEmojiTextViewHelper().getFilters(filters)); 54 } 55 56 @Override 57 public void setAllCaps(boolean allCaps) { 58 super.setAllCaps(allCaps); 59 getEmojiTextViewHelper().setAllCaps(allCaps); 60 } 61 62 /** 63 * Returns the {@link EmojiTextViewHelper} for this TextView. 64 * 65 * <p>This method can be called from super constructors through {@link 66 * #setFilters(InputFilter[])} or {@link #setAllCaps(boolean)}.</p> 67 */ 68 private EmojiTextViewHelper getEmojiTextViewHelper() { 69 if (mEmojiTextViewHelper == null) { 70 mEmojiTextViewHelper = new EmojiTextViewHelper(this); 71 } 72 return mEmojiTextViewHelper; 73 } 74 75 } 76