abhishek kumar gupta
abhishek kumar gupta

Reputation: 2289

How to differentiate between two images(drawable) placed on same imageView

While writing Robotium testcase, I want to differentiate between two images(drawable) placed on same imageView. Drawables are placed dynamically. I have tried to get drawable with the help of getDrawable() but every time different drawable object is coming.

Is there any way to get the drawable id? Any help or guidance will be well appreciated.

Upvotes: 0

Views: 164

Answers (1)

itsben
itsben

Reputation: 1027

You can't get the drawable id from the drawable itself, but you can store and retrieve the drawable id of the imageView using the setTag() and getTag() methods.

TestActivity.java

public class TestActivity extends Activity {

  private static String TAG = "TestActivity";
  private Activity mActivity;
  private static int ID_TAG = 100;


  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test);
    Log.d(TAG, "onCreate");

    mActivity = this;

    // set drawable
    ImageView imgView = new ImageView(mActivity);
    imageView.setImageResource(R.drawable.photo);
    String value = String.format("%d", R.drawable.photo);
    imageView.setTag(ID_TAG, value);

    // get drawable
    value = imageView.getTag(ID_TAG);
    drawableId = Integer.parseInt(value);
    if(drawableId == R.drawable.photo){
      Log.d(TAG, "You found the photo");
    }

  }

}

Upvotes: 1

Related Questions