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.graphics.Color; 20 import android.os.Bundle; 21 import android.text.Spannable; 22 import android.text.SpannableString; 23 import android.text.SpannableStringBuilder; 24 import android.text.Spanned; 25 import android.text.style.BackgroundColorSpan; 26 import android.text.style.RelativeSizeSpan; 27 import android.text.style.StrikethroughSpan; 28 import android.text.style.UnderlineSpan; 29 import android.view.LayoutInflater; 30 import android.view.View; 31 import android.view.ViewGroup; 32 import android.widget.ArrayAdapter; 33 import android.widget.MultiAutoCompleteTextView; 34 import android.widget.TextView; 35 36 import androidx.appcompat.widget.AppCompatAutoCompleteTextView; 37 import androidx.appcompat.widget.AppCompatCheckBox; 38 import androidx.appcompat.widget.AppCompatCheckedTextView; 39 import androidx.appcompat.widget.AppCompatMultiAutoCompleteTextView; 40 import androidx.appcompat.widget.AppCompatRadioButton; 41 import androidx.appcompat.widget.AppCompatToggleButton; 42 import androidx.appcompat.widget.SwitchCompat; 43 import androidx.emoji2.text.EmojiCompat; 44 import androidx.fragment.app.Fragment; 45 46 /** 47 * Main fragment. 48 */ 49 public class MainFragment extends Fragment { 50 51 // [U+1F469] (WOMAN) + [U+200D] (ZERO WIDTH JOINER) + [U+1F4BB] (PERSONAL COMPUTER) 52 private static final String WOMAN_TECHNOLOGIST = "\uD83D\uDC69\u200D\uD83D\uDCBB"; 53 54 // [U+1F469] (WOMAN) + [U+200D] (ZERO WIDTH JOINER) + [U+1F3A4] (MICROPHONE) 55 private static final String WOMAN_SINGER = "\uD83D\uDC69\u200D\uD83C\uDFA4"; 56 57 static final String EMOJI = WOMAN_TECHNOLOGIST + " " + WOMAN_SINGER; 58 private static final String[] AUTOCOMPLETE_ITEMS = new String[3]; 59 static { 60 AUTOCOMPLETE_ITEMS[0] = "Woman technologist " + WOMAN_TECHNOLOGIST; 61 AUTOCOMPLETE_ITEMS[1] = "Woman singer " + WOMAN_SINGER; 62 AUTOCOMPLETE_ITEMS[2] = "Woman"; 63 }; 64 65 private TextView mEmojiTextView; 66 private TextView mAppcompatTextView; 67 private TextView mEmojiEditText; 68 TextView mAppcompatEditText; 69 private TextView mEmojiButton; 70 private TextView mAppcompatButton; 71 TextView mRegularTextView; 72 private TextView mCustomTextView; 73 private AppCompatToggleButton mAppCompatToggleButton; 74 private SwitchCompat mSwitchCompat; 75 private AppCompatCheckedTextView mAppCompatCheckedTextView; 76 private AppCompatCheckBox mAppCompatCheckBox; 77 private AppCompatRadioButton mAppCompatRadioButton; 78 AppCompatAutoCompleteTextView mAppCompatAutoCompleteTextView; 79 AppCompatMultiAutoCompleteTextView mAppCompatMultiAutoCompleteTextView; 80 private TextView mRelative; 81 82 final Config.Listener mConfigListener = new Config.Listener() { 83 @Override 84 public void onEmojiCompatUpdated() { 85 init(); 86 } 87 }; 88 newInstance()89 static MainFragment newInstance() { 90 MainFragment fragment = new MainFragment(); 91 return fragment; 92 } 93 94 @Override onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)95 public View onCreateView(LayoutInflater inflater, ViewGroup container, 96 Bundle savedInstanceState) { 97 final View view = inflater.inflate(R.layout.fragment_main, container, false); 98 99 // TextView variant provided by EmojiCompat library 100 mEmojiTextView = view.findViewById(R.id.emoji_text_view); 101 // TextView from AppCompat 102 mAppcompatTextView = view.findViewById(R.id.appcompat_text_view); 103 // EditText variant provided by EmojiCompat library 104 mEmojiEditText = view.findViewById(R.id.emoji_edit_text); 105 // EditText from AppCompat 106 mAppcompatEditText = view.findViewById(R.id.appcompat_edit_text); 107 // Button variant provided by EmojiCompat library 108 mEmojiButton = view.findViewById(R.id.emoji_button); 109 // Button from AppCompat 110 mAppcompatButton = view.findViewById(R.id.appcompat_button); 111 // Regular TextView without EmojiCompat support; you have to manually process the text 112 mRegularTextView = view.findViewById(R.id.regular_text_view); 113 // Custom TextView 114 mCustomTextView = view.findViewById(R.id.emoji_custom_text_view); 115 116 mAppCompatToggleButton = view.findViewById(R.id.appcompat_toggle_button); 117 mSwitchCompat = view.findViewById(R.id.switch_compat); 118 mAppCompatCheckedTextView = view.findViewById(R.id.appcompat_checked_text_view); 119 mAppCompatCheckBox = view.findViewById(R.id.appcompat_checkbox); 120 mAppCompatRadioButton = view.findViewById(R.id.appcompat_radiobutton); 121 mAppCompatAutoCompleteTextView = view.findViewById(R.id.appcompat_autocomplete_textview); 122 mAppCompatMultiAutoCompleteTextView = 123 view.findViewById(R.id.appcompat_multiautocomplete_textview); 124 125 mRelative = view.findViewById(R.id.relative); 126 127 final TextView emojiListButton = view.findViewById(R.id.emoji_list_button); 128 emojiListButton.setOnClickListener(new View.OnClickListener() { 129 @Override 130 public void onClick(View v) { 131 ((MainActivity) getActivity()).showAllEmojis(); 132 } 133 }); 134 135 init(); 136 return view; 137 } 138 139 @Override onStart()140 public void onStart() { 141 super.onStart(); 142 Config.get().registerListener(mConfigListener); 143 } 144 145 @Override onStop()146 public void onStop() { 147 Config.get().unregisterListener(mConfigListener); 148 super.onStop(); 149 } 150 init()151 private void init() { 152 mEmojiTextView.setText(getString(R.string.emoji_text_view, EMOJI)); 153 Spannable spannableString = new SpannableString(getString(R.string.appcompat_text_view, 154 EMOJI)); 155 spannableString.setSpan(new BackgroundColorSpan(Color.parseColor("#aaFFaa")), 156 0, spannableString.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE); 157 spannableString.setSpan(new UnderlineSpan(), 0, spannableString.length(), 158 Spanned.SPAN_INCLUSIVE_EXCLUSIVE); 159 spannableString.setSpan(new StrikethroughSpan(), 0, spannableString.length(), 160 Spanned.SPAN_INCLUSIVE_EXCLUSIVE); 161 mAppcompatTextView.setText(spannableString); 162 mEmojiEditText.setText(getString(R.string.emoji_edit_text, EMOJI)); 163 mAppcompatEditText.setText(getString(R.string.appcompat_edit_text, EMOJI)); 164 165 mEmojiButton.setText(getString(R.string.emoji_button, EMOJI)); 166 mAppcompatButton.setText(getString(R.string.appcompat_button, EMOJI)); 167 mRegularTextView.setText(getString(R.string.regular_text_view, EMOJI)); 168 mAppCompatToggleButton.setTextOn(getString(R.string.toggle_on, EMOJI)); 169 mAppCompatToggleButton.setTextOff(getString(R.string.toggle_off, EMOJI)); 170 mSwitchCompat.setText(getString(R.string.switch_compat, EMOJI)); 171 mSwitchCompat.setTextOn("\uD83C\uDF89"); 172 mSwitchCompat.setTextOff("⛔"); 173 mSwitchCompat.setShowText(true); 174 mSwitchCompat.setEmojiCompatEnabled(true); 175 mAppCompatCheckedTextView.setText(getString(R.string.checked_text_view, EMOJI)); 176 mAppCompatCheckBox.setText(getString(R.string.check_box, EMOJI)); 177 mAppCompatRadioButton.setText(getString(R.string.radio_button, EMOJI)); 178 179 ArrayAdapter<String> adapter = new ArrayAdapter<String>(getContext(), 180 android.R.layout.simple_dropdown_item_1line, AUTOCOMPLETE_ITEMS); 181 mAppCompatAutoCompleteTextView.setAdapter(adapter); 182 mAppCompatMultiAutoCompleteTextView.setAdapter(adapter); 183 mAppCompatMultiAutoCompleteTextView.setTokenizer( 184 new MultiAutoCompleteTextView.CommaTokenizer()); 185 186 EmojiCompat.get().registerInitCallback(new EmojiCompat.InitCallback() { 187 @Override 188 public void onInitialized() { 189 final EmojiCompat compat = EmojiCompat.get(); 190 if (compat.getLoadState() == EmojiCompat.LOAD_STATE_SUCCEEDED) { 191 mRegularTextView.setText( 192 compat.process(getString(R.string.regular_text_view, EMOJI))); 193 194 195 mAppcompatEditText.setHint( 196 compat.process(getString(R.string.appcompat_edit_text, EMOJI))); 197 mAppCompatAutoCompleteTextView.setHint( 198 compat.process(getString(R.string.autocomplete_hint, EMOJI))); 199 mAppCompatMultiAutoCompleteTextView.setHint( 200 compat.process(getString(R.string.multiautocomplete_hint, EMOJI))); 201 } 202 } 203 }); 204 mCustomTextView.setText(getString(R.string.custom_text_view, EMOJI)); 205 mRelative.setText(relativeText()); 206 mRelative.setTextSize(10); 207 } 208 relativeText()209 private Spannable relativeText() { 210 SpannableStringBuilder builder = new SpannableStringBuilder(); 211 builder.append(EMOJI); 212 RelativeSizeSpan sizeSpan = new RelativeSizeSpan(4F); 213 builder.setSpan(sizeSpan, 0, EMOJI.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 214 builder.append("<-- Relative Size 4.0 |||| regular size ->"); 215 builder.append(EMOJI); 216 return builder; 217 } 218 } 219