-2

I want set an image to my ImageView created dynamically in my android app. I simply create it :

ImageView image = new ImageView(this);

and I want to set an image from a link like

example image

What have I to do ?

EDIT: my code:

LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                View v = vi.inflate(R.layout.article, null);



                img = (ImageView) v.findViewById(R.id.grid_image);
                url=thumbnail;

                Picasso.with(MainActivity.this).load(url).into(img);

It is in a for cicle

Marco Ghigo
  • 13
  • 1
  • 3
  • 2
    there are tons of tutorial for rendering image to a imageview. You can also use libraries like picasso. – thedarkpassenger Feb 04 '16 at 17:26
  • 2
    Possible duplicate of [How to load an ImageView by URL in Android?](http://stackoverflow.com/questions/2471935/how-to-load-an-imageview-by-url-in-android) – hpopiolkiewicz Feb 04 '16 at 17:27
  • check this out https://github.com/pankajnimgade/Tutorial/blob/master/app/src/main/java/miscellaneous/list/activities/ImageViewDynamicallyActivity.java – Pankaj Nimgade Feb 04 '16 at 17:41

4 Answers4

1

You can use 3rd party http://square.github.io/picasso/

Picasso.with(context).load("imageURL").into(image);

add dependency in Gradle

compile 'com.squareup.picasso:picasso:2.5.2'
Mahesh Giri
  • 1,810
  • 19
  • 27
1

You need to get that image from the net.

I recommend you to use Picasso. Add it to your project putting this line into the dependences in your Gradle file:

compile 'com.squareup.picasso:picasso:2.5.2'

And displayed with:

Picasso.with(this).load("URL").into(imageView);
MiguelCatalan
  • 916
  • 10
  • 26
0

From an online source? Something along these lines: image.setImageUri(Uri.parse("http://..."))

tallpaul
  • 1,220
  • 2
  • 13
  • 35
  • 1
    This is not a good choice, as downloading the image will be done on the main application thread, freezing the UI. – CommonsWare Feb 04 '16 at 17:46
0

As everyone has mentioned how to use Piccaso , I would like to mention the question is more focused on how to add this ImageView dynamically to the Layout, you can certainlly check the code on github, it's been tested.

LinearLayout linearLayout = (LinearLayout) findViewById(R.id.ImageViewDynamicallyActivity_linear_layout);
// this would be the linear layout to which you can add the `ImageView`

ImageView imageView = new ImageView(this);

LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
imageView.setLayoutParams(layoutParams);
// you can make a lot of changes to the `imageview` attributes here
linearLayout.addView(imageView);

Picasso.with(getApplicationContext()).load("http://image.3bmeteo.com/images/newarticles/w_663/immagine-nasa-visible-infrared-imaging-radiometer-suite-viirs-3bmeteo-66721.jpg").into(imageView);

I am assuming you would have added Picasso dependency to your build.gradle file for the app module.

compile 'com.squareup.picasso:picasso:2.5.2'
Pankaj Nimgade
  • 4,529
  • 3
  • 20
  • 30