Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android compare imageView with image

I want to compare current imageView with image from R.drawable. I guess I tried everything but I can't solve this one. I tried everything form stack overflow.

XML:

<ImageView
        android:layout_width="match_parent"
        android:src="@drawable/red"
        android:id="@+id/imageview1"
        android:clickable="true"
        android:layout_height="match_parent" />

Android:

final ImageView test = (ImageView) findViewById(R.id.imageview1);

test.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View v) {
          //if(test.getResources().equals(R.drawable.red))
          //if(test.getDrawable().equals(R.drawable.red))
          if(test.getDrawable().getConstantState().equals(getResources().getDrawable(R.drawable.red).getConstantState()))
          {
               Toast.makeText(getApplicationContext(), "work", Toast.LENGTH_SHORT).show();
          }
          else
          {
               Toast.makeText(getApplicationContext(), "not work", Toast.LENGTH_SHORT).show();
          }
     }
});
like image 853
Michael Avatar asked Aug 11 '26 20:08

Michael


1 Answers

If you want to save some information to view, you can use tag.

    <ImageView
    android:layout_width="match_parent"
    android:id="@+id/imageview1"
    android:src="@drawable/red"
    android:tag="work"
    android:clickable="true"
    android:layout_height="match_parent" />

and now you can compare

        ImageView image = (ImageView) findViewById(R.id.imageview1);
    if ("work".equals(image.getTag())){
        Toast.makeText(getApplicationContext(), "work", Toast.LENGTH_SHORT).show();
    }

you can set this tag from code

image.setTag("not work");
like image 183
Vivart Avatar answered Aug 14 '26 11:08

Vivart



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!