2

I am trying to display some text that can contain emojis that were created on an iOS version of the app. When we render these characters on Android I see [x] next to the emoji.

Is there a way to extend the Android TextView so that skin tone characters are ignored when rendering textviews.

I've tried creating a regex to replace them with empty characters but Android Studio complains that the regex isn't valid because the hex character syntax is not supported and it looks like other escape characters are also stripped out

replaceAll("[\\x{1f3fb}-\\x{1f3ff}]", "")
Barry Irvine
  • 13,858
  • 3
  • 25
  • 36

1 Answers1

2

I think I've finally worked it out.

public class CustomTextView extends AppCompatTextView {

  private static final String FITZPATRICK_PREFIX = "\uD83C";

  public CustomTextView(Context context) {
      this(context, null);
  }

  public CustomTextView(final Context context, final AttributeSet attrs) {
    this(context, attrs, 0);
  }

  public CustomTextView(final Context context, final AttributeSet attrs, final int defStyleAttr) {
    super(context, attrs, defStyleAttr);
  }

  @Override
  public void setText(final CharSequence text, final BufferType type) {
    super.setText(stripFitzpatrickCharacters(text == null ? "" : text.toString()), type);
  }

  public static String stripFitzpatrickCharacters(@NonNull final String string) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N && string.contains(FITZPATRICK_PREFIX)) {
        final String[] parts = string.split(FITZPATRICK_PREFIX);
        final StringBuilder builder = new StringBuilder();
        builder.append(parts[0]);
        for (int i = 1; i < parts.length; i++) {
            builder.append(parts[i].substring(1));
        }
        return builder.toString();
    } else {
        return string;
    }
}

}

Barry Irvine
  • 13,858
  • 3
  • 25
  • 36