I wanna display some texts and images but when apps is downloading the images (i run activity for first time) , this happen (It isn't good)
but when the app downloaded the images this happen (i run activity for second time) (It's good)
This is My Code :
LinearLayout layoutRoot = (LinearLayout) findViewById(R.id.layoutRoots);
String text = "This is An Image File [LoadImage:addlike.png] this is Another Image [LoadImage:adddislike.png] this is Another Image ";
processText(text, layoutRoot, R.layout.image_style, R.layout.text_style);
}
private void processText(String text, LinearLayout layoutRoot,int imageLayout, int textLayout) {
Pattern pattern = Pattern.compile("\\[LoadImage:(.*?)\\]");
Matcher matcher = pattern.matcher(text);
int offset = 0;
while (matcher.find()) {
try {
String textBeforImage = text.substring(offset, matcher.start());
offset += textBeforImage.length();
textBeforImage = textBeforImage.trim();
if (textBeforImage.length() != 0) {
addTextView(textBeforImage);
}
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 {
if (offset != text.length()) {
String content = text.substring(offset).trim();
if (content.length() != 0) {
addTextView(content);
}
}
}
catch (Exception e) {
e.printStackTrace();
}
}
private void addTextView(String text){
View custom = G.layoutInflater.inflate(R.layout.text_style, null);
TextView tv = (TextView) custom.findViewById(R.id.myTxt);
tv.setText(text);
parent.addView(custom);
setContentView(parent);
}
private void addImageView(String imageName){
View custom = G.layoutInflater.inflate(R.layout.image_style, null);
parent.addView(custom);
setContentView(parent);
ImageView img = (ImageView) custom.findViewById(R.id.MyImg);
final File imageFile = new File(G.DIR_FINAL + "/" + imageName);
if ( !imageFile.exists()) {
img.setImageBitmap(null);
String link = "http://example.com/Images/" + imageName;
DownloadImage.addToDownloadList(link, img);
}
BitmapFactory.Options options = new BitmapFactory.Options();
//options.inSampleSize = 8;
Bitmap bitmap = BitmapFactory.decodeFile(imageFile.getAbsolutePath(), options);
img.setImageBitmap(bitmap);
}
if you need image_style or text_style or any thing else , tell me
sorry about my English