display smiley in textview and edittext in android
13,543
Solution 1
Yes there is another way for showing smiley within the TextView
or EditText
. Build a Spannable
text using ImageSpan
and then setText the Spannable to TextView
or EditText
. Here is an post for the same.
Solution 2
To set Smiley in edittext
int value=R.id.ic_launcher;
Drawable Smiley = getResources().getDrawable(value);
Smiley.setBounds(0, 0, 15, 15);
SpannableStringBuilder builder = new SpannableStringBuilder();
String imgValue = "["+value+"]";
builder.append(imgValue);
builder.setSpan(new ImageSpan(Smiley), builder.length()-imgValue.length(), builder.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
edit_text.getText().insert(txt.getSelectionStart(), builder);
now to fetch smiley in listview or textview
textview.setText(addSmileySpans(context,edit_text.getText()));
public CharSequence addSmileySpans(Context context, CharSequence msg) {
SpannableStringBuilder builder = new SpannableStringBuilder(your_recieved_message);
Pattern pattern = Pattern.compile("\\[([^\\[\\]]+)\\]");
if( pattern != null )
{
Matcher matcher = pattern.matcher( your_recieved_message );
int matchesSoFar = 0;
while( matcher.find() )
{
CharSequence cs =matcher.group().subSequence(1, matcher.group().length()-1);
int value = Integer.parseInt(cs.toString());
System.out.println("pattern is::"+matcher.group().subSequence(1, matcher.group().length()-1));
int start = matcher.start() - (matchesSoFar * 2);
int end = matcher.end() - (matchesSoFar * 2);
Drawable Smiley = context.getResources().getDrawable(value);
Smiley.setBounds(0, 0,15,15);
builder.setSpan(new ImageSpan(Smiley), start + 1, end - 1, 0 );
builder.delete(start, start + 1);
builder.delete(end - 2, end -1);
matchesSoFar++;
}
}
return builder;
}
Solution 3
I think it is little bit late.
public void addSmily() {
CharSequence text = myEditText.getText();
int resource = R.drawable.ic_menu_emoticons ;
myEditText.setText(getSpannableText(text,resource));
}
private Spannable getSpannableText(CharSequence text, int smilyToAppend) {
Spannable spannable = Factory.getInstance().newSpannable(text+" ");
ImageSpan smilySpan = new ImageSpan(getApplicationContext(),smilyToAppend);
spannable.setSpan(smilySpan, spannable.length()-1, spannable.length(), 0);
return spannable;
}
Author by
Ramesh Solanki
i am senior android developer with 5 years experience, working at ahmedabad
Updated on June 29, 2022Comments
-
Ramesh Solanki almost 2 years
hello i am developing chat application in which i want to insert smiley i have not much idea about it how to integrate and display in it can u give me suggestion for doing the same ?
ImageGetter imageGetter = new ImageGetter() { public Drawable getDrawable(String source) { Drawable d = getResources().getDrawable( R.drawable.happy); d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight()); return d; } }; cs = Html.fromHtml( "<img src='" + getResources() .getDrawable(R.drawable.happy) + "'/>", imageGetter, null); System.out.println("cs is:- " + cs); edttxtemoji.setText(cs);
i found this code, in this it uses images, is this feasible ? or there is another solutions ? please give me better solution for this thanx in advance
-
Bismark Ito over 10 yearsSee solution using ImageSpan with EditText: stackoverflow.com/questions/19534427/…
-