Hey,
I’m going to explain how to detect and change a TextView’s color when touches and drags out its region.
This little code segment will save your day!
next_button.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
next_button.setTextColor(getResources().getColor(R.color.myGreyMaterial));
rect = new Rect(v.getLeft(), v.getTop(), v.getRight(), v.getBottom());
return true;
case MotionEvent.ACTION_UP:
if (rect != null
&& !rect.contains(v.getLeft() + (int) event.getX(),
v.getTop() + (int) event.getY())) {
// The motion event was outside of the view, handle this as a non-click event
next_button.setTextColor(getResources().getColor(R.color.colorPrimary));
return true;
}
// The view was clicked.
next_button.setTextColor(getResources().getColor(R.color.colorPrimary));
startActivity(new Intent(getApplicationContext(),RegisterActivity.class));
return true;
}
return false;
}
});
First you need to initialize a Recy object;
private Rect rect;
In the MotionEvent.ACTION_DOWN part, you need to calculate rect
rect = new Rect(v.getLeft(), v.getTop(), v.getRight(), v.getBottom());
It will get the region’s where the region is.
Then, in the MotionEvent.ACTION_UP part, you need to check that touches is in region or not;
if (rect != null
&& !rect.contains(v.getLeft() + (int) event.getX(),
v.getTop() + (int) event.getY()))
Then, ta-daa :) You can use that touch detection :)
If you have any question, ask me :)