1

May be this is a repeated question, if it is kindly direct me to it.

I have linear layout with a textview inside and on click of that linearlayout i want to change the background of linearlayout and text color of textview

Layout not clicked

Layout when clicked

i tried onTouch's Action.Down and Action.Up, but its not working cleanly. If the user swipe in any direction after ActionDown then its colors remains changed unless clicked again.

  • You can simply use `your_liner_layout.setOnClickListener(new View.OnClickListener() { };` – Kathi Apr 03 '16 at 10:25

1 Answers1

1

First you need a GestureDetector like this

 private GestureDetector gestureDetector;

        gestureDetector = new GestureDetector(this, new SingleTapConfirm());

Add SimpleOnGestureListener to GestureDetector

 private class SingleTapConfirm extends GestureDetector.SimpleOnGestureListener {

        @Override
        public boolean onSingleTapUp(MotionEvent event) {
            return true;
        }
    }

Now write a your OnTouchListener

View.OnTouchListener simpleMethod = new View.OnTouchListener() {

            if (gestureDetector.onTouchEvent(event)) {
                // single tap code goes here, its single tap click
                // its like Onclick for your view 
            } else {
                // your code for move and drag, touch 
                switch (event.getActionMasked()) {

                    case MotionEvent.ACTION_DOWN:
                  // your action down action

                        break;

                    case MotionEvent.ACTION_MOVE:
                    // your action move code here
                        break;
                    default:
                        return false;
                }
                return true;
            }

        }
    };

Finally add touch lister to your view

fab.setOnTouchListener(simpleMethod);
Kathi
  • 1,061
  • 1
  • 16
  • 31