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

6
  • Try using onClickListener instead of touch listener to handle click events & selector to change the color of the view on click. Commented Sep 1, 2016 at 5: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 Commented Sep 1, 2016 at 5:24
  • Did you use selector also? You can dynamically set two images for the pressed and normal state programmatically. Refer this: stackoverflow.com/questions/12754067/… Commented Sep 1, 2016 at 5: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 Commented Sep 1, 2016 at 5: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 Commented Sep 1, 2016 at 5:31

2 Answers 2

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); 
Sign up to request clarification or add additional context in comments.

4 Comments

I have to do this for all images
You just have to write this code at one place right? You are already setting background image to the view. Just add this state_pressed state along with it.
which image is R.drawable.your Image?
The image which you are setting as background to view
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 { } } }); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.