1

Hi I'm using this code for checking current image:

public void onToggle(View view){
    ImageView imageFav = (ImageView) view;
    if (imageFav.getResources().equals(R.drawable.fave_icon_1)) {
        dataSource.addToFavorite(idiom);
        Toast.makeText(this, "Saved", Toast.LENGTH_SHORT).show();
        imageFav.setImageResource(R.drawable.fave_icon_2);

    }else if(imageFav.getResources().equals(R.drawable.fave_icon_2)){
        dataSource.deleteFromFavorite(idiom);
        Toast.makeText(this, "Deleted", Toast.LENGTH_SHORT).show();
        imageFav.setImageResource(R.drawable.fave_icon_1);
    }
}

XML:

<ImageView
      android:id="@+id/img_fav_icon"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:onClick="onToggle"
      android:src="@drawable/fave_icon_1" />

But it doesn't work. Is there a generic way for checking current drawable.
Any idea?

Sirwan Afifi
  • 10,654
  • 14
  • 63
  • 110

2 Answers2

1

you are comparing the ImageView with a drawable id and, of course, the comparison will give never true as result. What you could is to have two different images view and toggle the visibility of the two, checking the id of the one you click on

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
1
getResources().getDrawable(R.id.icon).toString().equals(R.drawable.icon)

it have worked for me i hope it may solve your problem

Abhishek Chaubey
  • 2,960
  • 1
  • 17
  • 24