ImageView myImageView = findViewById(R.id.my_image_view); myImageView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { myImageView.setSelected(!myImageView.isSelected()); } });
ImageView myImageView = findViewById(R.id.my_image_view); myImageView.setSelected(true); // set the ImageView as selected if (myImageView.isSelected()) { myImageView.setImageResource(R.drawable.selected_image); } else { myImageView.setImageResource(R.drawable.unselected_image); }This example uses the AttributeSet android:selectedImage to specify a different image for the ImageView when it is selected. The code then changes the image source of the ImageView when its selection state is changed. Package library: android.widget.