3

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);
}
Ioannis I
  • 298
  • 3
  • 15
  • 1
    what is the problem exactly that you are facing? – Aakash Jul 27 '15 at 21:03
  • Is there a reason you want to use the version that does not work, or is this just curiosity about why it doesn't work? – APH Jul 27 '15 at 21:10
  • The problem is that the image is not shown at all.What I am trying to do is for the image to be loaded and fill the parent and not just be shown as a fixed size. – Ioannis I Jul 27 '15 at 21:17

2 Answers2

1

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

              }
            });
      }
jknair0
  • 1,194
  • 13
  • 24
0
    myLinearLayout = new LinearLayout(this);
    myLinearLayout.setOrientation(LinearLayout.VERTICAL);

      for (int z = 0; z < zCont.length; z++) {
      LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(100, 100);
                params.gravity= Gravity.LEFT;//CENTER_HORIZONTAL
                params.setMargins(-20, 0, 0, -10);
                ImageView iv = new ImageView(this);
                iv.setScaleType(ImageView.ScaleType.FIT_XY);
                setImage(iv, "http://sait.ru/fotota/rua.jpeg");
               myLinearLayout.addView(iv, params);
        }

    private void setImage(final ImageView imageView, final String imgURL) {
                Picasso.with(this)
                        .load(imgURL)
                        .fit().centerCrop()
                        .error(R.mipmap.ic_launcher)
                        .into(imageView);
            }
Gennady Kozlov
  • 1,011
  • 1
  • 11
  • 11