1

I have a MainActivity with 4 imageviews placed in 4 different card views.
What I would like to is, when clicking on one of these cardviews/imageviews it shows the image in fullscreen in a new activity.

I guess I could create 4 new activities and place a fullscreen imageview in each one of those, referencing to the selected image in MainActivity. But this approach does not seem so smooth.

I would prefer one "imageActivity" and then pass the selected imageview. Could this be done with passing the parameter for the resource id?

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
CodeKiller
  • 27
  • 3
  • 9
  • You can use `Bundle options = ActivityOptionsCompat.makeSceneTransitionAnimation(this, view, Contract.SHARED_IMAGE_ELEMENT_NAME).toBundle(); ActivityCompat.startActivity(this,intent, options);` – KeLiuyue Sep 16 '17 at 14:58
  • And you can look at [https://github.com/lgvalle/Material-Animations/blob/master/README.md](https://github.com/lgvalle/Material-Animations/blob/master/README.md) – KeLiuyue Sep 16 '17 at 15:01

1 Answers1

0

There can be a good solution than what I am going to offer but as even I'm learning and if it's just about 4 cardviews, I would've done the following.

Create a public static variable in MainActivity
public static Drawable resId;

set onClickListener for every Imageview like this.

ImageView image1 = (ImageView) findViewById(R.id.image1);
image1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        resId = R.drawable.image1;
        Intent intent = new Intent(MainActivity.this,your_fullscreen_activity_name.class);
        startActivity(intent);
        
    }
});
ImageView image2 = (ImageView) findViewById(R.id.image2);
image2.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        resId = R.drawable.image2;
        Intent intent = new Intent(MainActivity.this,your_fullscreen_activity_name.class);
        startActivity(intent);
        
    }
});
ImageView image3 = (ImageView) findViewById(R.id.image3);
image3.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        resId = R.drawable.image3;
        Intent intent = new Intent(MainActivity.this,your_fullscreen_activity_name.class);
        startActivity(intent);
        
    }
});
ImageView image4 = (ImageView) findViewById(R.id.image4);
image4.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        resId = R.drawable.image4;
        Intent intent = new Intent(MainActivity.this,your_fullscreen_activity_name.class);
        startActivity(intent);
        
    }
});

or on the cardviews, see here or here.
Create a fullscreenactivity and put its name in those intents.
Then in the java file of this fullscreenactivity, get the reference of layout/imageview and do this.

    ViewGroup viewGroup = findViewById(R.id.fullscreenLayout);
    viewGroup.setBackgroundDrawable(MainActivity.resId);

OR

    ImageView imageView = findViewById(R.id.fullscreenImageview); 
    imageView .setBackgroundDrawable(MainActivity.resId);

You can use either the imageView or the viewGroup.

And yes, Don't forget to add id of layout in XML file of fullscreenactivity or create a imagview with size same as parent layout.
If anything is confusing/not helpful, I'll help.

Community
  • 1
  • 1
Lalit Fauzdar
  • 5,953
  • 2
  • 26
  • 50
  • Why not just set `Home.flag` to the drawable resource? – OneCricketeer Sep 16 '17 at 17:15
  • Also, the static variable really isn't needed. Pass the integer via that Intent – OneCricketeer Sep 16 '17 at 17:16
  • @cricket_007 yes, one can use putExtra with intent and also one can create a Drawable variable in Home/Mainactivity and pass it but nothing wrong here too. More I'll get experience, better I'll try to answer. Thanks for your suggestion, I'll try to improve. – Lalit Fauzdar Sep 16 '17 at 17:18