Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Set an ImageView from a different Activity

I have two Activities, in the secondary activity I have some ImageViews, by selecting one I want it to take me back to the main Activity and to show me the ImageView with the image selected in the secondary Activity. How can I do it?

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You can send a callback to your second activity. Pass an interface SetImageViewInterface from your first activity to your second activity. On your first activity you should implement it. In the second activity you should call it and pass the imageView. Then you can call finish() to go back to the first activity.

public SetImageViewInterface interface{

   void setImageView(ImageView imageView);
}

First Activity:

   public MyActivity extends Activity implements SetImageViewInterface {

      public void setImageView(ImageView imageView){
         //set the imageView from second activity to first activity
      }
   }

Second Activity:

imageView.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
         //here you pass the image to your first activity
         setImageViewInterface.setImageView(imageView);
    }
});

You can read more about passing interfaces on this answer:
Android: How to send interface from one activity to another

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading