ERJAN
ERJAN

Reputation: 24500

I can not compare view.getBackground() == R.drawable.image ?[android]

I need to compare the image loaded as background into my imageView. I do so like this:

 @Override
    public boolean onTouch(View v, MotionEvent event) {

        if(event.getAction() == MotionEvent.ACTION_DOWN) {
            switch (v.getId()) {
                case R.id.hero1:
                    if(v.getBackground() == R.drawable.hero1){
                    //do something
              }
                    break;
                case R.id.hero2:
                 //other stuff
          } 

But v.getBackground() does not compare to what I have in drawable folder. Why?

Note that v.getBackgroundResource() is not available.

Upvotes: 0

Views: 1338

Answers (1)

Rahul Chaurasia
Rahul Chaurasia

Reputation: 1641

You can do like this-

  @Override
        public boolean onTouch(View v, MotionEvent event) {

            if(event.getAction() == MotionEvent.ACTION_DOWN) {
                switch (v.getId()) {
                    case R.id.hero1:

                      if (v.getBackground().getConstantState().equals(getResources().getDrawable(R.drawable.hero1).getConstantState())) {
                         // Do something here
                      }

                        break;
                    case R.id.hero2:
                     //other stuff
              } 

Upvotes: 2

Related Questions