Android: Touch to Change Image

Credit: Wikimedia
Credit: Wikimedia

On iOS I do this using a button (with different pressed state), and I figured it would be the same on Android, but turns out, no.

Step 1: Add a second image to the XML, and set the visibility to “gone”.

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/processed_image_background">
<ImageView
android:id="@+id/processed_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitCenter" />
<ImageView
android:id="@+id/processed_image_original"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitCenter"
android:visibility="gone"/>
</FrameLayout>

view raw
images.xml
hosted with ❤ by GitHub

Step 2: Add a tap target that changes visibility.

imageView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent event) {
originalImageView.setVisibility(event.getAction() == MotionEvent.ACTION_DOWN ?
View.VISIBLE : View.GONE);
return true;
}
});

view raw
TapTarget.java
hosted with ❤ by GitHub

Step 3: There is no step 3. Done!

One thought on “Android: Touch to Change Image

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.