Reputation: 24500
I need to compare the image loaded as background into my imageView. I do so like this:
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN) {
switch (v.getId()) {
case R.id.hero1:
if(v.getBackground() == R.drawable.hero1){
//do something
}
break;
case R.id.hero2:
//other stuff
}
But v.getBackground() does not compare to what I have in drawable folder. Why?
Note that v.getBackgroundResource() is not available.
Upvotes: 0
Views: 1338
Reputation: 1641
You can do like this-
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN) {
switch (v.getId()) {
case R.id.hero1:
if (v.getBackground().getConstantState().equals(getResources().getDrawable(R.drawable.hero1).getConstantState())) {
// Do something here
}
break;
case R.id.hero2:
//other stuff
}
Upvotes: 2