Skip to content Skip to sidebar Skip to footer

Using Imagebuttons To Switch Activities. How Can I Switch Image In Second Activity Based On Button Pressed?

I am just starting to dabble with Android development. Right now, I have a program in Android Studio that has two columns of images about 6 images each. I want to make it so if I c

Solution 1:

You could add to the intent the int resource of your pressed image if you give it file name to Tag attribute in xml:

ImageView
android:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/azir"android:tag="theImageFileName.png"android:id="@+id/imageView" />

Then you could retreive it when pressed and add it to the Intent you are using:

@OverridepublicvoidonClick(View v) {
     String fileName = (String) v.getTag();
     Intent intent = newIntent(this, SecondActivity.class);
     intent.putExtra("fileName", fileName);
      startActivity(intent);
 }

In second activity after you initialize the secondImageView:

StringfileName= getIntent().getStringExtra("fileName");
intresId= getResources().getIdentifier(fileName,"drawable", getPackageName());
secondImage.setImageDrawable(getResources().getDrawable(resId));

Post a Comment for "Using Imagebuttons To Switch Activities. How Can I Switch Image In Second Activity Based On Button Pressed?"