Abhishec Kumar
Abhishec Kumar

Reputation: 105

OnLongCLickListener is not working as I need

I have a TextView, with a onLongClickListener and OnClick event, on holding TextView, its color changes to red, and on releasing, its color is supposed to change to white.

Problem: When I hold the TextView and move my finger outside of it while holding, and then leave my finger, its color does not change to white.

XML

<TextView
    android:layout_width="match_parent"
    android:text="hello"
    android:textColor="#ffff"
    android:id="@+id/timer"
    android:layout_height="wrap_content"
    />

Java

final TextView t1 = (TextView) findViewById(R.id.timer);
    t1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            t1.setTextColor(Color.WHITE);

        }
    });
    t1.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            t1.setTextColor(Color.RED);
            return false;
        }
    });

Upvotes: 2

Views: 954

Answers (3)

Drew Szurko
Drew Szurko

Reputation: 1621

Use OnTouchListener that way you can register touch down and up events. MotionEvent case MotionEvent.ACTION_DOWN: will set the color to red when the user touches down on your TextView, and case MotionEvent.ACTION_UP: will set the color to white when the user lifts their finger off the TextView.

final TextView t1 = (TextView) findViewById(R.id.timer);


t1.setOnTouchListener(new View.OnTouchListener()
    {

        @Override
        public boolean onTouch(View v, MotionEvent event)
        {
        switch (event.getAction()) {

        case MotionEvent.ACTION_DOWN:
            t1.setTextColor(Color.RED);
            break; 
        case MotionEvent.ACTION_UP:
            t1.setTextColor(Color.WHITE);
            break;
    }
return true;
        }
   });

Upvotes: 0

Charuka Silva
Charuka Silva

Reputation: 13153

View.OnClickListener - Interface definition for a callback to be invoked when a view is clicked. View.OnLongClickListener - Interface definition for a callback to be invoked when a view has been clicked AND held.

So what you are saying is 100% true.It should be red because its being been clicked and held as the way you did.

But when i hold the text view and move my finger outside the text view while holding , and then leave my finger , it not changes its color to white

You have given color white to text view when it gets only clicked !! If you want to get that white like you said(when clicked and held), you need to set the white color in OnLongClickListener

To the point if you want to detect your views touch and release and change colors related to that then you need to use OnTouchListener instead of clickListeners

View.OnTouchListener - Interface definition for a callback to be invoked when a touch event is dispatched to this view. The callback will be invoked before the touch event is given to the view

       t1.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch ( event.getAction() ) {
                case MotionEvent.ACTION_DOWN:
                    t1.setTextColor(Color.RED); // pressed state
                 break;

                case MotionEvent.ACTION_UP:
                    t1.setTextColor(Color.WHITE); // Released state
                 break;
            }
            return true;
        }
    });

Upvotes: 2

aneurinc
aneurinc

Reputation: 1238

Assign a onTouch listener and look for MotionEvent.ACTION_DOWN and MotionEvent.ACTION_MOVE:

@Override
public boolean onTouch(View v, MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
       // Construct a rect of the view's bounds
       rect = new Rect(v.getLeft(), v.getTop(), v.getRight(), v.getBottom());
    }

    if (event.getAction() == MotionEvent.ACTION_MOVE) {
        if (!rect.contains(v.getLeft() + (int) event.getX(), v.getTop() + (int) event.getY())) {
            // User moved outside bounds
            t1.setTextColor(Color.WHITE);
        }
    }
    return false;
}

Upvotes: 0

Related Questions