-1

I need a drawable within the text, just like an emoji, so e.g.

"Please press the button looking like this and then proceed ..."

but with a custom drawable instead of the emoji in my example. (How) is this possible?

AskNilesh
  • 67,701
  • 16
  • 123
  • 163
swalkner
  • 16,679
  • 31
  • 123
  • 210

2 Answers2

3

I need a drawable within the text

You can Use ImageSpan

  • Span that replaces the text it's attached to with a Drawable that can be aligned with the bottom or with the baseline of the surrounding text. The drawable can be constructed from varied sources:

but with a custom drawable instead of the emoji in my example. (How) is this possible?

Try this working example

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView  textView=findViewById(R.id.tv);
        Spannable span = new SpannableString("Please press the button looking like this and then proceed ..");
        Drawable test = getResources().getDrawable(R.drawable.abc);
        test.setBounds(0, 0, 32,32);
        ImageSpan imageSpan = new ImageSpan(test, ImageSpan.ALIGN_BASELINE);
        span.setSpan(imageSpan, 36, 37, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);

        textView.setText(span);
    }


}

RESULT

enter image description here

AskNilesh
  • 67,701
  • 16
  • 123
  • 163
1

You can try unicode:

int unicode = 0x1F60A;

And use it like this:

public String getEmojiByUnicode(int unicode){
    return new String(Character.toChars(unicode));
}