How to select button after I am dragging finger and release like this:
https://i.stack.imgur.com/vb9SY.gif
I got my code from Get button coordinates and detect if finger is over them - Android
Start code:
imageButtonAppOptionsViewPage.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
final Vibrator v = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
v.vibrate(VibrationEffect.createOneShot(100, VibrationEffect.DEFAULT_AMPLITUDE));
else
v.vibrate(100);
constraintLayoutAppOptionAddNewPage.setVisibility(View.VISIBLE);
constraintLayoutAppOptionAddNewPage.startAnimation(animationShowButtonRight);
animationShowButtonRight.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
public void onAnimationEnd(Animation animation) {
constraintLayoutAppOptionDeletePage.setVisibility(View.VISIBLE);
constraintLayoutAppOptionDeletePage.startAnimation(animationShowButtonRightUp);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
animationShowButtonRightUp.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
public void onAnimationEnd(Animation animation) {
constraintLayoutAppOptionResetPage.setVisibility(View.VISIBLE);
constraintLayoutAppOptionResetPage.startAnimation(animationShowButtonLeftUP); }
@Override
public void onAnimationRepeat(Animation animation) {
}
});
animationShowButtonLeftUP.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
public void onAnimationEnd(Animation animation) {
constraintLayoutAppOptionChangePage.setVisibility(View.VISIBLE);
constraintLayoutAppOptionChangePage.startAnimation(animationShowButtonLeft); }
@Override
public void onAnimationRepeat(Animation animation) {
}
});
break;
case MotionEvent.ACTION_UP:
float endX = event.getX();
float endY = event.getY();
for(int i = 0; i < constraintLayoutAppOption.getChildCount(); i++){
if(constraintLayoutAppOption.getChildAt(i) instanceof ConstraintLayout){
ConstraintLayout constraintLayout = (ConstraintLayout) constraintLayoutAppOption.getChildAt(i);
for (int y = 0; y<constraintLayout.getChildCount();y++){
if(constraintLayout.getChildAt(y) instanceof Button){
Button b = (Button) constraintLayout.getChildAt(y);
if(isPointWithin((int)endX,(int)endY,b.getLeft(), b.getRight(), b.getTop(), b.getBottom())){
b.setBackgroundColor(Color.BLUE);
}else{
b.setBackgroundColor(Color.WHITE);
}
}
break;
}
}
}
break;
}
return true;
}
});
public static boolean isPointWithin(int x, int y, int x1, int x2, int y1, int y2) {
return (x <= x2 && x >= x1 && y <= y2 && y >= y1);
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
// TODO Auto-generated method stub
super.onWindowFocusChanged(hasFocus);
}
End code.
All my buttons got setBackgroundColor(Color.WHITE)
and the button selected don't setBackgroundColor(Color.BLUE)
Do you have an idea how to fix it or something else?
Thank you.