-1

I want click effect when image is clicked but from my code whenever i click image for the first time nothing happen however when i click for second time it shows orange colour effect that image was clicked . Below is my code ,I know this might not be the correct way to do this . But i want to know why this is happening

image.xml

 <ImageView
                style="@style/icon"
               android:background="@drawable/fear_96"
                android:onClick="see"
                android:id="@+id/abulation"
                />

below is onclick method

  public void see(View view) {

    view.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {

            switch (motionEvent.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    view.getBackground().setColorFilter(0xf0f47521,   
PorterDuff.Mode.SRC_ATOP);
                    view.invalidate();
                    break;

                case MotionEvent.ACTION_CANCEL:
                case MotionEvent.ACTION_UP:

                    view.getBackground().clearColorFilter();
                    view.invalidate();
             startActivity(view.getId());
                    break;
            }

            return true;
        }
    });
  }

  public void startActivity(int id)
  {
    Intent intent=new Intent(this,DuwaListView.class);
    switch (id)
    {
        case R.id.abulation:
            intent.putExtra(Intent.EXTRA_TEXT,"Abulation");
            break;
        case R.id.dressing:
            intent.putExtra(Intent.EXTRA_TEXT,"Dressing");
        break;
        case R.id.restroom:
            intent.putExtra(Intent.EXTRA_TEXT,"Restroom");
            break;
        default:

Toast.makeText(this,"underconstruction",Toast.LENGTH_SHORT).show();
            return;


    }


    startActivity(intent);
}

Please help why this code is showing such behaviour

FaisalAhmed
  • 3,469
  • 7
  • 46
  • 76

2 Answers2

1

When you click on image for the first it just set the Touch Listener on the image. So instead of adding touch listener to image inside the see function. Try it with outside this.

<ImageView
  style="@style/icon"
  android:background="@drawable/fear_96"
  android:id="@+id/abulation" />

ImageView imageView = (ImageView)findViewById(R.id.abulation);
imageView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {

            switch (motionEvent.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    view.getBackground().setColorFilter(0xf0f47521, PorterDuff.Mode.SRC_ATOP);
                    view.invalidate();
                    break;

                case MotionEvent.ACTION_CANCEL:
                case MotionEvent.ACTION_UP:
                    view.getBackground().clearColorFilter();
                    view.invalidate();
                    startActivity(view.getId());
                    break;
            }

            return true;
        }
    });
  }
Deepak Goyal
  • 4,747
  • 2
  • 21
  • 46
  • i have around 45 imageButton and i didnt to want make that much object so i was just trying to do in this way . I was also thinking in that way but it is good to know the real reason behind this . Anyways thanks for your answer. – FaisalAhmed Aug 03 '16 at 07:46
0

Try putting the

startActivity(view.getId());

above the

case MotionEvent.ACTION_CANCEL:

or

case MotionEvent.ACTION_UP:

it worked for me. hope this helps