0

I have an imageview as a button that has as default image(app:srcCompat="@drawable/image_one") that I want to validate, this button has an event, if it performs the event correctly, I change the image from the code setImageResource(R.drawable.image_two).

How can I validate if my imageview has the default image(image_one) or if I change image(image_two)?

XML:

<ImageView
    android:id="@+id/image_view"
    android:layout_width="75dp"
    android:layout_height="75dp"
    android:layout_margin="5dp"
    app:srcCompat="@android:drawable/image_one" />

Code:

ImageView btnImage;
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    btnImage = findViewById(R.id.image_view);

    btnImage.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
       
        }
    });

}
//image validation
public void validateImage() {
  
}
Rick V
  • 25
  • 6

3 Answers3

0

Use ImageView.getDrawable to get the drawable in the view. Check that its equal (using .equals, not ==) to the drawable you want it to have.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
0

very simple you use Glide

    String image_two_expected = "https://akcdn.detik.net.id/visual/2021/03/03/ilustrasi-virus-covid-19-photo-created-by-wirestock-via-freepik_169.jpeg?w=715&q=90"

    try {
            if (image_two_expected != null) {
            Glide.with(context)
                    .placeholder(R.drawable.your_default_image)
                    .load(image_two_expected)
                    .into(view);
        }
        } catch (Exception ex) {
            Log.e("ERROR", "LOAD IMAGE: " + ex);
        }
Hendra
  • 26
  • 3
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 01 '21 at 02:26
-1

You can use tag attribute. At initial state set your imageView as

<ImageView
    android:id="@+id/image_view"
    android:layout_width="75dp"
    android:layout_height="75dp"
    android:layout_margin="5dp"
    android:tag="1"
    app:srcCompat="@android:drawable/image_one" />

Then on your click action when you change the image set your imageView's tag as 2. You can check if the tag is 1 or 2 to decide what to do.

Kdaydin
  • 441
  • 5
  • 17
  • That wouldn't actually validate the image changing. And adding code to "test" when it isn't actually testing what you want is pointless and fragile. – Gabe Sechan Nov 01 '21 at 00:51
  • @GabeSechan Image validation is not used for checking if the image is the intended image that's not the point as I understand, just want to check the state if it is one or two. For that, you don't need to deal with resources. This is not a UI test. – Kdaydin Nov 01 '21 at 10:44
  • And that's not the way to do it. You're testing the wrong thing, with code that has no other effect. That isn't actually testing what they want to do. That's not just a useless test, it's a negative impact on your codebase as it makes you think something is tested when it isn't, while adding unnecessary code to it. – Gabe Sechan Nov 01 '21 at 13:30