Easy fix. For example i need men/woman 🚺🚹.
Lol they show as <?> here, heres the image:
But on lower sdks they show up as a unknown symbol.
The general idea is to get the char codes behind these symbols, and if the string contains them, then put a image from the raw folder in place of the emoji in a spannable string.
You can get emoji images from emojipedia http://emojipedia.org/mens-symbol/
In a string these characters come in a format of two characters:
man symbol - '\uD83D','\uDEB9'
woman symbol - '\uD83D','\uDEBA'
So, i cycle through string with a for loop and check if charAt(i) == '\uD83D' and if next char is '\uDEB9' or '\uDEBA' then continue with the image stuff. Btw on newer java sdk you can use a String/char in a switch:
SpannableString ss = new SpannableString(input);
if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
int ssl = ss.length();
for (int i = 0; i < ssl; i++){
char c = ss.charAt(i);
if (c == '\uD83D' && i < ssl - 1){
char next = ss.charAt(i + 1);
float k = 1f;
switch (next){
case '\uDEB9'://man emoji
Bitmap man = getBitmapFromRaw("man", context);
if (man != null) {
Drawable d = new BitmapDrawable(context.getResources(), man);
float scale = context.getResources().getDisplayMetrics().density * k;
float w = d.getIntrinsicWidth() / scale;
float h = d.getIntrinsicHeight() / scale;
d.setBounds(0, 0, (int) w, (int) h);
ImageSpan is = new ImageSpan(d, ImageSpan.ALIGN_BASELINE);
ss.setSpan(is, i, i + 2, Spannable.SPAN_INCLUSIVE_EXCLUSIVE); }
break;
case '\uDEBA'://woman emoji
Bitmap woman = getBitmapFromRaw("woman", context);
if (woman != null) {
Drawable d = new BitmapDrawable(context.getResources(), woman);
float scale = context.getResources().getDisplayMetrics().density * k;
float w = d.getIntrinsicWidth() / scale;
float h = d.getIntrinsicHeight() / scale;
d.setBounds(0, 0, (int) w, (int) h);
ImageSpan is = new ImageSpan(d, ImageSpan.ALIGN_BASELINE);
ss.setSpan(is, i, i + 2, Spannable.SPAN_INCLUSIVE_EXCLUSIVE); }
break;
default: break; }
}
}
}
The getBitmapFromRaw method:
public static Bitmap getBitmapFromRaw(String filename, Context context) {
return getBitmapFromRaw(filename, context, false);
}
public static Bitmap getBitmapFromRaw(String filename, Context context, boolean reduceQuality) {
int identifier = context.getResources().getIdentifier(filename, "raw", context.getPackageName());
Bitmap bmp = null;
if (identifier != 0) {
InputStream is = context.getResources().openRawResource(identifier);
BufferedInputStream bis = new BufferedInputStream(is);
BitmapFactory.Options options = null;
if (reduceQuality) {
options = new BitmapFactory.Options();
options.inSampleSize = 2;
options.inPreferredConfig = Bitmap.Config.RGB_565;
}
bmp = BitmapFactory.decodeStream(bis, null, options);
}
return bmp;
}