I'm fairly new to Android development but I have the following problem. I am trying to create several ImageViews programmatically and use Picasso to load the image and I stumbled on the following problem.
EDIT: What I am trying to achieve is I want the ImageView to be created with width=FILL_PARENT and height=WRAP_CONTENT
See the code extracts below:
This example works but is not what I want (loads the images)
LinearLayout articleBodyLL = (LinearLayout) findViewById(R.id.articleBody);
ImageView articleTitleImageView = (ImageView) findViewById(R.id.articleTitleImageView);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(300,300);
params.gravity= Gravity.CENTER_HORIZONTAL;
[...]
ImageView iv = new ImageView(this);
setImage(iv, imageURL);
articleBodyLL.addView(iv, params);
[...]
private void setImage(final ImageView imageView, final String imgURL) {
Picasso.with(ArticleProvider.getContext())
.load(imgURL)
.fit().centerCrop()
.error(R.drawable.default_image)
.into(imageView);
}
This example does not work (images are not shown)
LinearLayout articleBodyLL = (LinearLayout) findViewById(R.id.articleBody);
ImageView articleTitleImageView = (ImageView) findViewById(R.id.articleTitleImageView);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
params.gravity= Gravity.CENTER_HORIZONTAL;
[...]
ImageView iv = new ImageView(this);
setImage(iv, imageURL);
articleBodyLL.addView(iv, params);
[...]
private void setImage(final ImageView imageView, final String imgURL) {
Picasso.with(ArticleProvider.getContext())
.load(imgURL)
.fit().centerCrop()
.error(R.drawable.default_image)
.into(imageView);
}