Example usage for android.view View setOnTouchListener

List of usage examples for android.view View setOnTouchListener

Introduction

In this page you can find the example usage for android.view View setOnTouchListener.

Prototype

public void setOnTouchListener(OnTouchListener l) 

Source Link

Document

Register a callback to be invoked when a touch event is sent to this view.

Usage

From source file:Main.java

public static void setOnTouchBackgroundEffect(View view) {
    view.setOnTouchListener(new OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
            if (!(v.getBackground() instanceof TransitionDrawable))
                return false;

            TransitionDrawable transition = (TransitionDrawable) v.getBackground();

            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                transition.startTransition(500);
                break;
            case MotionEvent.ACTION_HOVER_EXIT:
            case MotionEvent.ACTION_CANCEL:
            case MotionEvent.ACTION_UP:
                transition.reverseTransition(500);
                break;
            }/*  w  w w . j av a  2  s .co m*/

            return false;
        }
    });
}

From source file:Main.java

/**
 * View Scale Animation//from   w  ww  .  j  av a2s.c  o  m
 * <p/>
 * Scale value {@link AnimUtils#SCALE}
 *
 * @param targetView
 */
public static void pushScaleAnimation(View targetView) {
    targetView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            pushScale(v, event.getAction(), SCALE, SCALE);
            return false;
        }
    });
}

From source file:Main.java

public static void setViewTouchHightlighted(final View view) {
    if (view == null) {
        return;/* w w w  . j  a v a2 s. c o  m*/
    }

    view.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                view.setBackgroundColor(Color.rgb(1, 175, 244));
            } else if (event.getAction() == MotionEvent.ACTION_UP) {
                view.setBackgroundColor(Color.rgb(255, 255, 255));
            }
            return false;
        }
    });
}

From source file:Main.java

public static void setOnTouchScaleAnimation(View targetView, final float scaleX, final float scaleY) {
    targetView.setOnTouchListener(new View.OnTouchListener() {
        @Override/*from w  w  w.j  ava  2s  . c om*/
        public boolean onTouch(View v, MotionEvent event) {
            final int action = event.getAction();
            switch (action) {
            case MotionEvent.ACTION_DOWN: {
                ScaleAnimation anim = new ScaleAnimation(1.0f, scaleX, 1.0f, scaleY, v.getWidth() / 2,
                        v.getHeight() / 2);
                anim.setDuration(60);
                anim.setFillEnabled(true);
                anim.setFillAfter(true);
                v.startAnimation(anim);
                break;
            }
            case MotionEvent.ACTION_CANCEL:
            case MotionEvent.ACTION_UP: {
                ScaleAnimation anim = new ScaleAnimation(scaleX, 1.0f, scaleY, 1.0f, v.getWidth() / 2,
                        v.getHeight() / 2);
                anim.setDuration(100);
                v.startAnimation(anim);
                break;
            }
            }
            return false;
        }
    });
}

From source file:Main.java

/**
 * View Scale Animation//from  ww  w.j  av a 2 s .com
 *
 * @param targetView
 * @param scaleX     toScaleX and fromScaleX
 * @param scaleY     toScaleY and fromScaleY
 */
public static void pushScaleAnimation(View targetView, final float scaleX, final float scaleY) {
    targetView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            pushScale(v, event.getAction(), scaleX, scaleY);
            return false;
        }
    });
}

From source file:Main.java

public static void autoHideInput(final View view, final InputMethodManager imm) {

    if (!(view instanceof EditText)) {
        view.setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
                return false;
            }/* w  ww  .java 2  s .c o m*/

        });
    }

    if (view instanceof ViewGroup) {

        for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
            View innerView = ((ViewGroup) view).getChildAt(i);
            autoHideInput(innerView, imm);
        }
    }
}

From source file:Main.java

public static void setupUI(final Context context, View view) {
    if (view == null)
        return;/*ww  w  .  j  a  v  a  2 s .c  om*/

    if (!(view instanceof EditText)) {
        view.setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                hideSoftKeyboard(context);
                return false;
            }
        });
    }

    if (view instanceof ViewGroup) {
        for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
            View innerView = ((ViewGroup) view).getChildAt(i);
            setupUI(context, innerView);
        }
    }
}

From source file:Main.java

public static void setupUI(final View view, final Activity activity) {

    //Set up touch listener for non-text box views to hide keyboard.
    if (!(view instanceof EditText)) {
        view.setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                hideSoftKeyboard(activity);
                return false;
            }//www  .j a va 2  s.  c  o m
        });
    }
    //If a layout container, iterate over children and seed recursion.
    if (view instanceof ViewGroup) {
        for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
            View innerView = ((ViewGroup) view).getChildAt(i);
            setupUI(innerView, activity);
        }
    }
}

From source file:com.adguard.android.commons.BrowserUtils.java

public static void showBrowserInstallDialog(final Context context) {
    // Touch listener for changing colors of CardViews
    View.OnTouchListener touchListener = new View.OnTouchListener() {
        @Override//from w  w w.ja v a  2 s.c om
        public boolean onTouch(View v, MotionEvent event) {
            int action = event.getActionMasked();
            if (action == MotionEvent.ACTION_DOWN) {
                ((CardView) v).setCardBackgroundColor(R.color.card_view_background_pressed);
            } else if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_CANCEL
                    || action == MotionEvent.ACTION_OUTSIDE) {
                ((CardView) v).setCardBackgroundColor(R.color.white);
            }
            return false;
        }
    };

    final LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    final View dialogLayout = inflater.inflate(R.layout.select_browser_dialog, null);
    AlertDialog.Builder builder = new AlertDialog.Builder(context, R.style.AlertDialog);
    builder.setNegativeButton(android.R.string.cancel, null);
    builder.setView(dialogLayout);
    final AlertDialog dialog = builder.create();

    View cardView = dialogLayout.findViewById(R.id.browser_yandex);
    cardView.setOnTouchListener(touchListener);
    cardView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ActivityUtils.startMarket(context, YANDEX_BROWSER_PACKAGE, REFERRER);
            dialog.dismiss();
        }
    });

    cardView = dialogLayout.findViewById(R.id.browser_samsung);
    cardView.setOnTouchListener(touchListener);
    cardView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ActivityUtils.startMarket(context, SAMSUNG_BROWSER_PACKAGE, null);
            dialog.dismiss();
        }
    });

    dialog.show();
}

From source file:org.vqeg.viqet.activities.MainActivity.java

public static void setupUI(View view, final Activity activity) {

    view.setOnTouchListener(new View.OnTouchListener() {

        public boolean onTouch(View v, MotionEvent event) {
            InputMethodManager inputMethodManager = (InputMethodManager) activity
                    .getSystemService(Activity.INPUT_METHOD_SERVICE);
            inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
            return false;
        }/*w  w  w . j  av  a 2 s .  c  o  m*/

    });
}