Home | History | Annotate | Download | only in emojicompat
      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.emojicompat;
     18 
     19 import android.content.Context;
     20 import android.os.Bundle;
     21 import android.support.text.emoji.EmojiCompat;
     22 import android.support.v7.app.AppCompatActivity;
     23 import android.widget.TextView;
     24 
     25 import java.lang.ref.WeakReference;
     26 
     27 
     28 public class MainActivity extends AppCompatActivity {
     29 
     30     // [U+1F469] (WOMAN) + [U+200D] (ZERO WIDTH JOINER) + [U+1F4BB] (PERSONAL COMPUTER)
     31     private static final String WOMAN_TECHNOLOGIST = "\uD83D\uDC69\u200D\uD83D\uDCBB";
     32 
     33     // [U+1F469] (WOMAN) + [U+200D] (ZERO WIDTH JOINER) + [U+1F3A4] (MICROPHONE)
     34     private static final String WOMAN_SINGER = "\uD83D\uDC69\u200D\uD83C\uDFA4";
     35 
     36     static final String EMOJI = WOMAN_TECHNOLOGIST + " " + WOMAN_SINGER;
     37 
     38     @Override
     39     protected void onCreate(Bundle savedInstanceState) {
     40         super.onCreate(savedInstanceState);
     41         setContentView(R.layout.activity_main);
     42 
     43         // TextView variant provided by EmojiCompat library
     44         final TextView emojiTextView = findViewById(R.id.emoji_text_view);
     45         emojiTextView.setText(getString(R.string.emoji_text_view, EMOJI));
     46 
     47         // EditText variant provided by EmojiCompat library
     48         final TextView emojiEditText = findViewById(R.id.emoji_edit_text);
     49         emojiEditText.setText(getString(R.string.emoji_edit_text, EMOJI));
     50 
     51         // Button variant provided by EmojiCompat library
     52         final TextView emojiButton = findViewById(R.id.emoji_button);
     53         emojiButton.setText(getString(R.string.emoji_button, EMOJI));
     54 
     55         // Regular TextView without EmojiCompat support; you have to manually process the text
     56         final TextView regularTextView = findViewById(R.id.regular_text_view);
     57         EmojiCompat.get().registerInitCallback(new InitCallback(regularTextView));
     58 
     59         // Custom TextView
     60         final TextView customTextView = findViewById(R.id.emoji_custom_text_view);
     61         customTextView.setText(getString(R.string.custom_text_view, EMOJI));
     62     }
     63 
     64     private static class InitCallback extends EmojiCompat.InitCallback {
     65 
     66         private final WeakReference<TextView> mRegularTextViewRef;
     67 
     68         InitCallback(TextView regularTextView) {
     69             mRegularTextViewRef = new WeakReference<>(regularTextView);
     70         }
     71 
     72         @Override
     73         public void onInitialized() {
     74             final TextView regularTextView = mRegularTextViewRef.get();
     75             if (regularTextView != null) {
     76                 final EmojiCompat compat = EmojiCompat.get();
     77                 final Context context = regularTextView.getContext();
     78                 regularTextView.setText(
     79                         compat.process(context.getString(R.string.regular_text_view, EMOJI)));
     80             }
     81         }
     82 
     83     }
     84 
     85 }
     86