0

I am writing an Android application which gets string to parse using regex. Let's say it gets

'Tis an example image of a beaver {[beaver.jpg]} having an afternoon tea.

And what I want to get is TextView with 'Tis an example image of a beaver, ImageView beaver.jpg and an another TextView having an afternoon tea..

How (if possible) is creating objects possible after reading such string and parsing with regex?

adam
  • 395
  • 1
  • 2
  • 14
  • Will it always be [text][image][text] as in one image and two text blocks or will there be multiple images imbedded in the text? – Chris Stillwell Oct 16 '14 at 21:08
  • None of above. I will use custom classes that will extend View and there will be no rule in how they are going to be arranged. – adam Oct 16 '14 at 21:09
  • Will the image path always be enclosed inside a `{[ ]}`, and will it always be a `.jpg`? – drew moore Oct 16 '14 at 21:15
  • Ok, will there be always one image and one or two text blocks? Or could there be n images and m text blocks? Also, have you looked at WebViews and asset loading? It may be easier than what you are attempting. – Chris Stillwell Oct 16 '14 at 21:16
  • @drewmoore No, but there will be some (about 20) specific rules for each object to be created. Image here is an example. – adam Oct 16 '14 at 21:17
  • @ChrisS There will be n images, m textviews, k AnotherTextViews, l AnotherAnotherTextViews and so on. I wanted to avoid using WebView but I will probably by forced to do so. – adam Oct 16 '14 at 21:20

1 Answers1

1

Your custom view class should have a constructor that takes three String parameters, one for the path of the image, one for text to be displayed before the image and one for the text to display afterwards.

class YourView { 
    public YourView(String imgPath, String beforeImg, String afterImg){ 
         //add TextView for text before image 
         //add ImageView for image 
         //add TextView for text after image 
    }
}

If your image will always be enclosed in a {[ ]}, the regex (.+)\\{\\[(.+)\\]\\}(.+) will capture everything before the {[ in capturing group 0, everything between {[ and }] (i.e. the image path) in group 1, and everything after }] in group 2.

If you use a Java Matcher You can then achieve your desired result like:

    String pre, path, post; 
    while (matcher.find()){ 
        pre = matcher.group(0);
        path = matcher.group(1);
        post = matcher.group(2); 
        YourView view = new YourView(path, pre, post); 
        //add view 
    }

Note that, if the image is at the end or beginning of the text, you'll simply be passing empty strings as the third/first parameters here, which will result in the creation of empty TextViews, which should be harmless (or you can do a check and not create one for empty strings).

That said, also note that above code is untested (but should give you the general idea), and that I'm a Java programmer with little experience in Android - so let me know if there's an Android-specific reason this wouldn't work

drew moore
  • 31,565
  • 17
  • 75
  • 112
  • Thanks, but it will not be as simple as text/image/text, but more complex and not always the same. Sometimes just one text, and sometimes heading/boldtext/text/italic/heading/text. I did not want to use WebView but I think this will be the best way. – adam Oct 16 '14 at 21:34