かめのはこ

とあるエンジニアのメモ帳です

【android】アプリで絵文字を表示してみる

Androidアプリで絵文字を使う要件があったので
試してみた
SO-01B端末でしか試してないのであしからず
 
■まずは入力
 
通常の入力フォームでは絵文字入力はでてこなかった
 
EditTextにallowEmoji属性trueを付加する
 
// ソース

EditText et = new EditText();
Bunble bunble = et.getInputExtra(true);
if ( bunble != null ) bunble.putBoolean("allowEmoji",true);

 
これでソフトウェアキーボード上の記号ボタンを押すと
絵文字一覧が登場した
 
 
■続いて表示
 
バイトコードで記述してやれば表示されました
 
晴れ
 ユニコード E63E
 バイトコード -13,-66,-128,-128
 
曇り
 ユニコード E63F
 バイトコード -13,-66,-128,-127
 
// ソース

package test.textview;

import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TextView;

public class EmojiTest extends Activity {
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  LinearLayout layout = new LinearLayout(this);
  layout.setOrientation(LinearLayout.VERTICAL);
  TextView tv1 = new TextView(this);
  TextView tv2 = new TextView(this);
  // set byte code
  byte[] byte_emoji_hare = new byte[]{-13,-66,-128,-128};
  byte[] byte_emoji_kumori = new byte[]{-13,-66,-128,-127};
  // to string from byte code
  String string_emoji_hare = new String(byte_emoji_hare, 0, 4);
  String string_emoji_kumori = new String(byte_emoji_kumori, 0, 4);
  tv1.setText("晴れ : " + string_emoji_hare);
  tv2.setText("曇り : " + string_emoji_kumori);
  layout.addView(tv1);
  layout.addView(tv2);
  setContentView(layout);
 }
}

 
バイトコードを調べる
 
絵文字をEditTextに入力し、そのデータからgetBytes()で取得する
※地道だ
 
// ソース

String msg = et.getText().toString();
String hoge="";
for( byte ans : msg.getBytes() ) hoge = hoge + ans + ":";
System.out.println(hoge);