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);
}
Take a look at this code. this works fine. A suggestion is that if you are trying to form some kind of list take a look at RecyclerView to avoid Exceptions(many exceptions :p).
LinearLayout articleBodyLL = (LinearLayout) findViewById(R.id.articleBody);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
params.gravity= Gravity.CENTER_HORIZONTAL;
ImageView iv = new ImageView(this);
iv.setScaleType(ImageView.ScaleType.FIT_XY);
setImage(iv, "http://square.github.io/picasso/static/sample.png");
articleBodyLL.addView(iv, params);
//////////////////////////////////////////////
private void setImage(final ImageView imageView, final String imgURL) {
Picasso.with(this)
.load(imgURL)
.error(R.mipmap.ic_launcher)
.into(new Target() {
@Override public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
Log.e("loaded", "onBitmapLoaded: loaded" );
imageView.setImageBitmap(bitmap);
imageView.invalidate();
}
@Override public void onBitmapFailed(Drawable errorDrawable) {
Log.e("loaded", "onBitmapFailed: load failed" );
}
@Override public void onPrepareLoad(Drawable placeHolderDrawable) {
}
});
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With