-1

I am trying to add multiple smiles in textview using this code.
This is my TextView.

<TextView
        android:id="@+id/textViewId"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:bufferType="spannable" />

And this is add smiley function.

public void addSmily() {
        int resource  = R.drawable.smily ;

        Spannable spannable = Spannable.Factory.getInstance().newSpannable(" ");

        Drawable d = ContextCompat.getDrawable(this, resource);
        d.setBounds(0, 0, 40, 40);
        ImageSpan smilySpan = new ImageSpan(d, ImageSpan.ALIGN_BASELINE);

        spannable.setSpan(smilySpan, spannable.length()-1, spannable.length(), 0);

        sendText.append(spannable);
}

Smiles are adding perfectly but the problem is when I add lots of smiles did not fit in a single line then the first line of smiles become invisible and they start from the 2nd line.

App Privew This is what happening. Plz, someone help me.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Muhammad Asim
  • 379
  • 1
  • 4
  • 14

2 Answers2

3

Solution:

Try this inside your button:

SpannableString ss = new SpannableString("abc");

Drawable d = ContextCompat.getDrawable(your_activity.this, R.drawable.your_smiley_drawable);
d.setBounds(0, 0, 40, 40);
ImageSpan span = new ImageSpan(d, ImageSpan.ALIGN_BASELINE);
ss.setSpan(span, 0, 3, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);

edittext.append(ss);

Note: Also, EditText's inputtype must be textMultiline.

Try it, Works in my lap, Let's Hope it helps to you too.

Ümañg ßürmån
  • 9,695
  • 4
  • 24
  • 41
  • can you explain whats the difference between using " single space " and "abc" in SpannableString? – Muhammad Asim Sep 28 '18 at 12:29
  • There is no difference, anything inside the double-quotes("") is treated as String, If you give single-space, You are writing a "space character" which will also be treated as a String. @MuhammadAsim Check out more: https://developer.android.com/reference/android/text/SpannableString – Ümañg ßürmån Sep 28 '18 at 12:44
1

You can also set smiley by unicode in textview.

how set emoji by unicode in a textview?

int unicode = 0x1F60A;

Which can be used with

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

So Textview displays without Drawable

Try it with http://apps.timwhitlock.info/emoji/tables/unicode

Hope it may help you.

Jack
  • 223
  • 3
  • 13