'ImageButton to show image of ImageView onClick
I want an ImageButton to change its image when clicked on it. But the image that it changes to has to be the image from an ImageView. Is this possible?
EDIT:
in the first activity where you chose your character to play with, there´s an ImageView
. The Image of it is being changed to the Image of the chosen character via onClickListener
that is set for an ImageButton
:
ImageView imgV3 = (ImageView) findViewById(R.id.imageView3);
View.OnClickListener onClickButton1 = view -> {
imgV3.setImageDrawable(
((ImageButton) view).getDrawable()
);
};
ImageButton imageButton1 = (ImageButton) findViewById(R.id.plOneChar1);
imageButton1.setOnClickListener(onClickButton);
ImageButton imageButton2 = (ImageButton) findViewById(R.id.plOneChar2);
imageButton2.setOnClickListener(onClickButton);
...
Until here everything's working just fine.
In the 2nd activity, where the two players play against each other, I need the current Image of ImageView3
(and imageView4
for the 2nd character, didn't post it above because it's the same code). this current image is what i want to be shown on several ImageButtons
when clicked on them.
This is how i tried to solve it:
public class PlayActivity extends AppCompatActivity {
String random;
String pl1;
String pl2;
List<String> list;
Random rand;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play);
String pl1 = "Player One";
String pl2 = "Player Two";
List<String> list = new ArrayList<String>();
list.add(pl1);
list.add(pl2);
Random rand = new Random();
String random = list.get(rand.nextInt(list.size()));
Toast.makeText(this, "It´s your turn: " + random, Toast.LENGTH_SHORT).show();
}
public void onClickImgBtn (View view) {
ImageButton imgBtn = (ImageButton) view;
ImageView imgV3 = (ImageView) findViewById(R.id.imageView3);
ImageView imgV4 = (ImageView) findViewById(R.id.imageView4);
if((random == pl1) && (imgBtn.getDrawable() == null)){
imgBtn.setImageDrawable(imgV3.getDrawable());
random = list.get(rand.nextInt());
} else if((random == pl2) && (imgBtn.getDrawable() == null)) {
imgBtn.setImageDrawable(imgV4.getDrawable());
random = list.get(rand.nextInt());
} else {
Toast.makeText(this,"Chose other section!", Toast.LENGTH_SHORT).show();
}
}
}
I hope this will help to find what I'm doing wrong. Let me know if more info needed.
EDIT SOLUTION:
I found a solution. I used getTag()
& setTag()
and global variables.
At first i created a new class MyApplication
which extends Application
to declare 3 global variables:
public class MyApplication extends Application {
Integer defaultTag = R.drawable.white;
Integer resImgV3;
public Integer getResImgV3() {
return resImgV3;
}
public void setResImgV3(int resImgV3) {
this.resImgV3 = resImgV3;
}
Integer resImgV4;
public Integer getResImgV4() {
return resImgV4;
}
public void setResImgV4(int resImgV4) {
this.resImgV4 = resImgV4;
}
}
Then i set an onClickListener
to all ImageButtons
that changes the Image of the ImageViews
(imgV3 and imgV4
) by getting the Tags
of the ImageButtons
which are set to the id of the drawable
in each ImageButton
and it also sets the value
of the global variables resImgV3
and resImgV4
to the id of the now set drawables
of imgV3
and imgV4
:
ImageView imgV3 = (ImageView) findViewById(R.id.imageView3);
imgV3.setTag(R.drawable.kid1);
imgV3.setImageResource(R.drawable.kid1);
((MyApplication)this.getApplication()).setResImgV3((Integer) imgV3.getTag());
View.OnClickListener onClickButton = view -> {
imgV3.setTag((Integer) view.getTag());
imgV3.setImageResource((Integer) view.getTag());
((MyApplication) this.getApplication()).setResImgV3((Integer) imgV3.getTag());
};
ImageView imgV4 = (ImageView) findViewById(R.id.imageView4);
imgV4.setTag(R.drawable.kid2);
imgV4.setImageResource(R.drawable.kid2);
((MyApplication)this.getApplication()).setResImgV4((Integer) imgV4.getTag());
View.OnClickListener onClickButton1 = view -> {
imgV4.setTag((Integer) view.getTag());
imgV4.setImageResource((Integer) view.getTag());
((MyApplication) this.getApplication()).setResImgV4((Integer) imgV4.getTag());
};
ImageButton imageButton1 = (ImageButton) findViewById(R.id.plOneChar1);
imageButton1.setImageResource(R.drawable.kid1);
imageButton1.setTag(R.drawable.kid1);
imageButton1.setOnClickListener(onClickButton);
ImageButton imageButton2 = (ImageButton) findViewById(R.id.plOneChar2);
imageButton2.setImageResource(R.drawable.kid2);
imageButton2.setTag(R.drawable.kid2);
imageButton2.setOnClickListener(onClickButton); //and 6 more ImageButtons
And finally in the 2nd activity, where i needed the images of imageView3
and imageView4
, i can easily get the resId
of each drawable
by just accessing the MyApplication
-class like following:
ImageView imgV5 = (ImageView) findViewById(R.id.imageView5);
imgV5.setImageResource(((MyApplication) this.getApplication()).getResImgV3());
imgV5.setTag(((MyApplication) this.getApplication()).getResImgV3());
ImageView imgV6 = (ImageView) findViewById(R.id.imageView6);
imgV6.setImageResource(((MyApplication) this.getApplication()).getResImgV4());
imgV6.setTag(((MyApplication) this.getApplication()).getResImgV4());
I hope this will help other ones with the same problem in the future.
Solution 1:[1]
Try this:
ImageButton Demo_button = (ImageButton)findViewById(R.id.firstimage);
// when you click this demo button
Demo_button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Demo_button.setImageResource(R.drawable.secondimage);
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | V_Ivascu |