
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”.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
Step 2: Add a tap target that changes visibility.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
}); |
Step 3: There is no step 3. Done!
One reply on “Android: Touch to Change Image”
[…] Android: Touch to Change Image. Technical post from 2016 on having two images and changing the image in response to touch events. […]