Tell me please. Before me is the task of dynamically changing the image when you click on the ImageView. This is essentially a like button. Clicked once, the picture changed to like, clicked another time, the picture changed to unlike and so on to infinity. Initially, the image from resources resides in imageView during initialization. Therefore, I decided that it was necessary to compare the image in ImageView and the image from resources. I decide this way:

if (imageViewLike.getDrawable().getConstantState().equals(ContextCompat.getDrawable(getApplicationContext(), R.drawable.ic_favorite_border_black_18dp).getConstantState())){ imageViewLike.setImageResource(R.drawable.ic_favorite_black_18dp); } else {imageViewLike.setImageResource(R.drawable.ic_favorite_border_black_18dp); } 

in version 4.1.1 it works in 5.1 no.

    1 answer 1

    Take any standard Android widget with two states ( Checkbox , ToggleButton , Switch , RadioButton - in your case it is more convenient first) and customize it so that the state is "on" (checked) - the image is like, the state is "off" (not checked) - dizlike. Everything, it will switch when clicked (the text needs to be made an empty line if you do not need a signature).

    You can find out the current state using the isCheced() method, force the state to be set using the setChecked() method. In both cases, the true value will correspond to the like, and the false value will correspond to the message.