Question

well I have lots of images as a buttons in my layout and I want to use setOnTouchListener with Switch Case to reduce coding. But I saw that. If I use switch case with images id Images needs double or more clicks to trigger event.

This is working but very long coding: it makes very long coding for each images.

    ImageView imageView1 = (ImageView) findViewById(R.id.image1);

                // set the ontouch listener
                imageView1.setOnTouchListener(new OnTouchListener() {
                            //events
                            }

this is what I want but not working properly:

So that I decided to use Switch case stament.

                           public void btnClicker(View v) {
                            switch (v.getId()) {
                            case R.id.image1:
                            imagetouch(image1);
                            break;

This is Xml

<ImageView
                    android:id="@+id/image1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:clickable="true"
                    android:onClick="btnClicker"
                    android:src="@drawable/kupa2" />

And this is my imagetouch method:

public void imagetouch(ImageView image) {
        image.setOnTouchListener(new OnTouchListener() {

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

                case MotionEvent.ACTION_DOWN: {

                    ImageView view = (ImageView) v;
                    // overlay is black with transparency of 0x77 (119)
                    view.getDrawable().setColorFilter(0x77000000,
                            PorterDuff.Mode.SRC_ATOP);
                    view.invalidate();

                    break;
                }
                case MotionEvent.ACTION_UP: {

                    //my events

                }
                case MotionEvent.ACTION_CANCEL: {
                    ImageView view = (ImageView) v;
                    // clear the overlay
                    view.getDrawable().clearColorFilter();
                    view.invalidate();

                    break;
                }
                }

                return true;
            }
        });
    }
Was it helpful?

Solution

Actually I have fix it with implements OnTouchListener

For example,

public class myclass extends Activity implements OnTouchListener{

And then implements touch listener:

@Override
    public boolean onTouch(View v, MotionEvent event) { 
            switch(e.getAction){
            case (MotionEvent.TOUCH_DOWN)
               // TO DO smthg.
                break;
            }

        }

and than in activity On Create method write this code:

@Override
protected void onCreate(Bundle savedInstanceState) {
.
.
.
  ImageView iv1 = (ImageView) findViewById(R.id.ImageView);
  iv1.setOnTouchListener(this);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top