11
This is :) and want to :) replace with :D new image.

I have this type of string that is i have got from EditTextbox.NOw i want to replace all ":)" with image1 and ":D" with image2.I want to do like string.replaceall(":)",image1) and string.replaceall(":D",image2).So can anybody suggest me how to do this with small code and also better performance.I have write the code and it is working also fine but it takes much time.

textview.setText(getSmiledText(ctx, stringvalue));
private static final HashMap<String, Integer> emoticons = new HashMap<String, Integer>();
    static {
        emoticons.put(":)", R.drawable.j1);
        emoticons.put(":D", R.drawable.j2);}

public static Spannable getSmiledText(Context context, String s) {
        int index;
        SpannableStringBuilder builder = new SpannableStringBuilder();
        builder.append(s);

        for (index = 0; index < builder.length(); index++) {
            for (Entry<String, Integer> entry : emoticons.entrySet()) {
                int length = entry.getKey().length();
                if (index + length > builder.length())
                    continue;
                if (builder.subSequence(index, index + length).toString()
                        .equals(entry.getKey())) {
                    builder.setSpan(new ImageSpan(context, entry.getValue()),
                            index, index + length,
                            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                    index += length - 1;
                    break;
                }
            }
        }
        return builder;
    }
Nency
  • 482
  • 1
  • 8
  • 21
  • `it is working also fine` so what was the question again ? – njzk2 Oct 09 '12 at 07:52
  • i need the better solution which will increase the performance.This is working but take much time bcz it will check character by character.So i need better solution – Nency Oct 09 '12 at 07:56
  • it seems that `setSpan` is the slowest part... I have the similar issue with replacing background colour of the text. – bancer Oct 31 '12 at 12:24

2 Answers2

2

Check this:

public static Spannable getSmiledText(Context context, String s) 
    {
    int index;
    SpannableStringBuilder builder = new SpannableStringBuilder();
    builder.append(s);

    for (Entry<String, Integer> entry : EmoticonsCode.emoticons_code.entrySet())
    {
        try {
           int length = entry.getKey().length();
           for ( index = s.indexOf(entry.getKey()); index >= 0; index = s.indexOf(entry.getKey(), index + 1))
           {
                System.out.println(index);
                builder.setSpan(new ImageSpan(context, entry.getValue()), index, index + length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
           }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
   }
   return builder;
}
arghtype
  • 4,376
  • 11
  • 45
  • 60
Sachin Arora
  • 426
  • 4
  • 6
0

What you need to do is just load the images earlier not at run time, load images and save in variables and just assign the images at run time. FYI characters is not the problem, i came through same issue earlier i understood it as touch points issue, but problem was loading of images.

jay
  • 292
  • 1
  • 11
  • can you explain more how to load image previosly? I have set the images like this emoticons.put(":)", R.drawable.j1); emoticons.put(":D", R.drawable.j2); so now how can i do? – Nency Oct 09 '12 at 08:03
  • Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.j1); Bitmap bitmap1 = BitmapFactory.decodeResource(getResources(), R.drawable.j2); builder.setSpan(bitmap,index, index + length,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); I hope this helps. – jay Oct 09 '12 at 08:07
  • YOu can also put string and bitmap into your hashmap. – jay Oct 09 '12 at 08:09
  • But if i have set the image in hashmap like R.draweable.j1 then what is the different between that and this bitmap – Nency Oct 09 '12 at 08:13
  • difference is you have already loaded the resource like this emoticons.put(":)", BitmapFactory.decodeResource(getResources(), R.drawable.j1)); – jay Oct 09 '12 at 08:15
  • okay so if just i write the R.drawable.j1 then it will not load the image but if i write BitmapFactory.decodeResource(getResources(), R.drawable.j1 then it will load the image right? – Nency Oct 09 '12 at 08:19
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/17738/discussion-between-nency-and-jay) – Nency Oct 09 '12 at 08:22