0

I receive Data from Server text and in the text there is something like this '[LoadImage:ImageLink]' and I want this text to be replaced with the ImageLink I know how to download the image from the link but I don't know how to display it in the textview I have heard somethings about regular expression but I don't know how does it work
( i don't wanna use ImageSpan , i want to replace that text ([LoadImage:ImageLink]) to image ) i found this in a forum but it wasn't complete
(I want something like this)

String text = "This is An Image File [LoadImage:'image1.png']  this is Another Image [LoadImage:'image2.png']";
Pattern pattern = Pattern.compile("\\[LoadImage:(.*?)\\]")
ProccessText( Text, layoutRoot, R.layout.image_style, R.layout.text_style);
private void processText(String text, LinearLayout layoutRoot,int imageLayout, int textLayout) {

         // proccess input Text Here !
         // i don't know what should i write here
}
Milad M
  • 101
  • 4
  • possible duplicate of [How to display image in Android's TextView?](http://stackoverflow.com/questions/5561981/how-to-display-image-in-androids-textview) – Jen R Jun 18 '15 at 16:48

2 Answers2

1

this is a snippet code from one of my old projects so please check it again !

while (matcher.find()) {
    try {
        // Maybe it's Text Before Image
        String textBeforImage = text.substring(offset, matcher.start());
        offset += textBeforImage.length();
        textBeforImage = textBeforImage.trim();
        if (textBeforImage.length() != 0) {
            addTextView(textBeforImage);
        }
        // now , if it's Text before founded image it's Handled ! ,so add Image !
        String ImageName = matcher.group(1).toString();
        if (ImageName.length() != 0) {
            addImageView(ImageName);
            offset += text.substring(matcher.start(), matcher.end()).length();
        }
    }
    catch (Exception e) {
        e.printStackTrace();
    }
}

try {
    //Text After Last Image
    if (offset != text.length()) {
        String content = text.substring(offset).trim();
        if (content.length() != 0) {
            addTextView(content);
        }
    }
}
catch (Exception e) {
    e.printStackTrace();
}
Mojtaba Yeganeh
  • 2,788
  • 1
  • 30
  • 49
  • thank you a lot , but there isn't any addTextView void or addImageView – Milad M Jun 18 '15 at 19:10
  • i remember that i inflate a xml template , setting font size , color and other options from settings , same process for an image , inflating layout and setting image resource and what ever else you need ! – Mojtaba Yeganeh Jun 18 '15 at 19:42
0

I you are already getting the image, this answer is probably what you are looking for.

Essentially you create a SpannableString and assign this to the setSpan property.

Community
  • 1
  • 1
Sivakanesh
  • 817
  • 4
  • 13
  • 36