Hello I also use this scenario in my current application.
1) Using CheckBox
Use CheckBox
with android:button="@null"
property,
This property will remove border of CheckBox
and display only your drawable images.
state_checked
property will work with CheckBox
<CheckBox
android:id="@+id/imgDisplayCheckimg"
android:layout_width="wrap_contenrt"
android:layout_height="wrap_contenrt"
android:background="@drawable/display_checkbox"
android:button="@null"
android:checked="false"
android:clickable="true" />
This is Drawable file
display_checkbox.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- When selected, use red-->
<item android:drawable="@drawable/red_color" android:state_checked="true"/>
<!-- When not selected, use green-->
<item android:drawable="@drawable/green_color" android:state_checked="false"/>
</selector>
Replace red_color and green_color with your drawable name.
2) Using ImageView
Declare this variable Globally
boolean switchStatus = false;
Find your ImageView and add below Click Listener.
switchImageView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(switchStatus == true) {
anonymousImage.setImageResource(R.drawable.red);
switchStatus = false;
} else {
anonymousImage.setImageResource(R.drawable.green);
switchStatus = true;
}
}
});
ImageView
in layout file.
<ImageView
android:id="@+id/switchImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/green" />
Change name at green and red with your drawable name.