0

to replace the image in the layout programmatically, in my case, i need to refer to the image from another layout of the other class. There is: the first class, which is extended by the second class, each class has its own layout. I need to refer from the first class to the image in the mockup of the second class.

How do I do this based on the image replacement code below?

ImageView img= (ImageView) findViewById(R.id.image);
img.setImageResource(R.drawable.my_image);

3 Answers3

0

pass some value to 2nd activity before starting using putextra based on the value set image there .

if it is from second to first that use startactivityforresult and setresult

Nadil
  • 56
  • 7
0

You can use the image anywhere you want

public class Activity2 extends Activity1 {

 ImageView changeImage;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 int image1 = R.id.imageView1;
 int image2 = R.id.imageView2;
 changeImage = (ImageView)findViewById(R.id.imageView1);
 changeImage.setImageResource(**you can add your image1/image2 **);

 }
}

Think this will solve your problem

Anbu
  • 671
  • 1
  • 6
  • 18
0

NOTE: Parent to child calling is not recommended as it kind of destroys the reason for inheritance. The best way would be to restructure your application design so that there are NO parent to child dependencies. A parent should not ever need to know its children or their capabilities.

However.. you can achieve this by following method in FirstClass :

void udpate(SecondClass s) {
 if(s instanceof SecondClass) ((SecondClass)s).updateImageView(String url);
}

where updateImageView would be method inside your SecondClass for updating required imageView.

Hope it helps.

Sahil
  • 952
  • 1
  • 7
  • 14