2

I know that this question have been asked before and i have gone through them but they didnt solve my problem.

I have ScrollView with one LinearLayout with imageview. And i have used ontouchListner to imageview to give them click effect like this

switch (motionEvent.getAction()) {
        case MotionEvent.ACTION_DOWN:
            view.getBackground().setColorFilter(0xCCCDDC39, PorterDuff.Mode.SRC_ATOP);
            view.invalidate();
            break;
        case MotionEvent.ACTION_CANCEL:
        case MotionEvent.ACTION_UP:
            view.getBackground().clearColorFilter();
            view.invalidate();
            startActivity(view.getId());
            break;
}

But the problem is whenever i scroll ontouchlistner is called of imageview and image gets clicked .I dont want this whenever i am scrolling , Because this does not allow me to scroll. Please help

FaisalAhmed
  • 2,603
  • 3
  • 33
  • 61
  • Try using `onClickListener` instead of touch listener to handle click events & `selector` to change the color of the view on click. – Abhishek V Sep 01 '16 at 05:22
  • i tried that , It does not give the effect i want . for example when the user tap and hold on icon it should be green until user leave the icon . This effect cannot be achieved from onClickListner – FaisalAhmed Sep 01 '16 at 05:24
  • Did you use `selector` also? You can dynamically set two images for the pressed and normal state programmatically. Refer this: http://stackoverflow.com/questions/12754067/dynamically-defining-and-using-selectors – Abhishek V Sep 01 '16 at 05:27
  • I know selector . Reason i didnt used it that because i have around 40 images if i will use selector then i have to used double images 80 and it can increase app size i think – FaisalAhmed Sep 01 '16 at 05:30
  • and when i used selector to draw some color on it . it draw green color all over image making unvisible all can i see is green color – FaisalAhmed Sep 01 '16 at 05:31
  • No need to use another set of 40 images for selected image. You can create the selector dynamically & apply color filter. Please check my answer. – Abhishek V Sep 01 '16 at 05:40

2 Answers2

3

If I understand your question correctly, You can use onClickListener and selector to achieve this. Also you don't have to keep duplicate set of images for the selected state. You can create the selector dynamically like this

        StateListDrawable states = new StateListDrawable();
        Drawable drawable = ContextCompat.getDrawable(this, R.drawable.your image);
        drawable.setColorFilter(0xCCCDDC39, PorterDuff.Mode.SRC_ATOP);
        states.addState(new int[] {android.R.attr.state_pressed}, drawable);
        yourView.setBackground(states);
Abhishek V
  • 11,918
  • 6
  • 45
  • 60
0

a sure short asnwer

scroll.setOnScrollChangeListener(new View.OnScrollChangeListener() {
        @Override
        public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {


                    int x = scrollY - oldScrollY;
                    if (x > 0) {
                        //scroll up
                    } else if (x < 0) {
                        //scroll down
                    } else {

                    }

        }
    });