Search code examples
androidonclickimageviewviewflipperontouchlistener

Make differents between setOnClickListener and setOnTouchListener


I need some help. In my android app I have a ViewFlipper that contains some LinearLayout. On every LinearLayout I have a ImageView. For switching between screens I use this code :

LinearLayout layMain = (LinearLayout) findViewById(R.id.layout_main);
        layMain.setOnTouchListener((OnTouchListener) this);

//--------------

public boolean onTouch(View arg0, MotionEvent arg1) {

        // Get the action that was done on this touch event
        switch (arg1.getAction()) {
        case MotionEvent.ACTION_DOWN: {
            // store the X value when the user's finger was pressed down
            downXValue = arg1.getX();
            break;
        }

        case MotionEvent.ACTION_UP: {
            // Get the X value when the user released his/her finger
            float currentX = arg1.getX();

            // going backwards: pushing stuff to the right
            if (downXValue < currentX) {
                // Get a reference to the ViewFlipper
                ViewFlipper vf = (ViewFlipper) findViewById(R.id.details);
                // Set the animation
                vf.setOutAnimation(AnimationUtils.loadAnimation(this,
                        R.anim.push_right_out));
                vf.setInAnimation(AnimationUtils.loadAnimation(this,
                        R.anim.push_right_in));

                // Flip!
                vf.showPrevious();
            }

            // going forwards: pushing stuff to the left
            if (downXValue > currentX) {
                // Get a reference to the ViewFlipper
                ViewFlipper vf = (ViewFlipper) findViewById(R.id.details);
                // Set the animation
                // vf.setInAnimation(AnimationUtils.loadAnimation(this,
                // R.anim.push_left_in));
                vf.setOutAnimation(AnimationUtils.loadAnimation(this,
                        R.anim.push_left_out));
                vf.setInAnimation(AnimationUtils.loadAnimation(this,
                        R.anim.push_left_in));
                // Flip!
                vf.showNext();
            }
            break;
        }
        }

        // if you return false, these actions will not be recorded
        return true;
    }

Every screen contains a ImageView.So, when I switch the screens the imageview is changing. Now I want to implement this: When an image is clicked a url to be open. Here is my code :

ViewFlipper vf = (ViewFlipper) findViewById(R.id.details);

        for (i = 0; i < jArray.length(); i++) {
            LinearLayout l = new LinearLayout(this);
            l.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
                    LayoutParams.FILL_PARENT));
            l.setBackgroundColor(0x000000);
            l.setOrientation(LinearLayout.VERTICAL);
            vf.addView(l);

            ImageView img = new ImageView(this);
            img.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
                    LayoutParams.FILL_PARENT));
            Drawable image1 = ImageOperations(getBaseContext(), url[i]);
            img.setImageDrawable(image1);
            System.out.println("target" + target[i]);
            img.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri
                            .parse("http://google.com"));
                    startActivity(browserIntent);
                }
            });

            l.addView(img);
        }

The problem is that everytime I touch the screen the url is open and now I can't switch between screens. How to do this, to make the differents between onClick() and on Touch()?

Please help me.. Thanks in advance


Solution

  • I think you use image View like a background for Linear Layout , it seems Activity handle the touch for ImageView , to achieve your Functionality setTouch listener for imageView and we can handle the click for image view indirectly using following code in OntouchListener,

    if (downXValue == currentX) {
    Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri
                            .parse("http://google.com"));
                    startActivity(browserIntent);}