0

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.

Dhaval Solanki
  • 4,589
  • 1
  • 23
  • 39
davidwww
  • 1
  • 1
  • *Im not quite sure what the problem is, what part of this code is working and what isnt? – Joachim Haglund Oct 10 '19 at 11:44
  • The basic idea would be to get the distance between the buttons and the action up for all buttons and change the background accordingly. – Rahul Oct 10 '19 at 11:49
  • @ Joachim Haglund the code need make one of button blue but all are white, it is because any button did not pressed @ Rahul Kumar can you help me? – davidwww Oct 10 '19 at 12:05

1 Answers1

0

i used this to move a texView over the screen while i press the button but you can also use it on a view

right.setOnTouchListener(new View.OnTouchListener() {

    private Handler mHandler;

    @Override public boolean onTouch(View v, MotionEvent event) {
        switch(event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                if (mHandler != null) return true;
                mHandler = new Handler();
                mHandler.postDelayed(mAction, 100);
                break;
            case MotionEvent.ACTION_UP:
                if (mHandler == null) return true;
                mHandler.removeCallbacks(mAction);
                mHandler = null;
                break;
        }
        return false;
    }

    Runnable mAction = new Runnable() {
        @Override public void run() {
            System.out.println("Performing action...");
            game.getTanks().get(game.getCurrentTank()).goRight();
            mHandler.postDelayed(this, 100);
        }
    };

});
DoFlamingo
  • 232
  • 1
  • 18
  • I don't wont to move view, I want when I touch button I will move my finger on button and when I will release the botton will pressed and do the code action, you can see an example of the action in this link https://i.stack.imgur.com/vb9SY.gif – davidwww Oct 10 '19 at 11:57
  • then make a new Runnable when you touch the button and add the code you want to do in there – DoFlamingo Oct 10 '19 at 13:08
  • I have 4 buttons, how the system will know who is the button that i touched? – davidwww Oct 10 '19 at 15:59