Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatic imageview and picasso

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);
}
like image 532
Ioannis I Avatar asked Mar 01 '26 01:03

Ioannis I


1 Answers

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) {

              }
            });
      }
like image 157
jknair Avatar answered Mar 02 '26 16:03

jknair