0

i want to touch button and drag into other button position you can get clear idea by this image. Guiding Image

Please help me

I Also Found This very helpful. Touch and drag image in android but i want to drag to other button position not some were else on screen.i don't have any idea how i would detect button is entered in other button dimensions.

So far i am doing is just animation but i don't want just straight forward moving button by click. i want user to move by himself button to other button position.

Code

public class AnimationActivity extends Activity implements OnClickListener {

public Button btn_a1, btn_a2;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mainsec);
    btn_a1 = (Button) findViewById(R.id.btn_a1);
    btn_a2 = (Button) findViewById(R.id.btn_a2);

    btn_a1.setOnClickListener(this);
    btn_a2.setOnClickListener(this);

}

@Override
public void onClick(final View v) {
    // TODO Auto-generated method stub
    switch (v.getId()) {
    case R.id.btn_a1: {
        int direction = -1;
        DisplayMetrics displaymetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
        RelativeLayout.LayoutParams absParams = (RelativeLayout.LayoutParams) btn_a1
                .getLayoutParams();

        final float xDelta = (displaymetrics.widthPixels / 2)
                - absParams.leftMargin - (btn_a1.getWidth() / 2);

        final Animation animation = new TranslateAnimation(0, xDelta
                * direction, 0, 0);
        AnimationListener animationOutListener = new AnimationListener() {
            public void onAnimationEnd(Animation animation) {
                btn_a2.setBackgroundDrawable(R.id.blank);// Unselect
            }

            public void onAnimationRepeat(Animation animation) {
                // TODO Auto-generated method stub
            }

            public void onAnimationStart(Animation animation) {
                // TODO Auto-generated method stub
                btn_a2.setBackgroundDrawable(R.id.red);// Select
            }
        };
        animation.setAnimationListener(animationOutListener);
        animation.setDuration(1000);
        btn_a2.startAnimation(animation);

        break;
    }
    case R.id.btn_a2: {
        int direction = 1;
        DisplayMetrics displaymetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
        RelativeLayout.LayoutParams absParams = (RelativeLayout.LayoutParams) btn_a1
                .getLayoutParams();

        final float xDelta = (displaymetrics.widthPixels / 2)
                - absParams.leftMargin - (btn_a1.getWidth() / 2);

        final Animation animation = new TranslateAnimation(0, xDelta
                * direction, 0, 0);
        AnimationListener animationOutListener = new AnimationListener() {
            public void onAnimationEnd(Animation animation) {
                btn_a1.setBackgroundDrawable(R.id.blank);// Unselect
            }

            public void onAnimationRepeat(Animation animation) {
                // TODO Auto-generated method stub
            }

            public void onAnimationStart(Animation animation) {
                // TODO Auto-generated method stub
                btn_a1.setBackgroundDrawable(R.id.red);// Select
            }
        };
        animation.setDuration(1000);
        btn_a1.startAnimation(animation);
        break;
    }
    }
}
Community
  • 1
  • 1
Fahid Mahmood
  • 57
  • 1
  • 8

1 Answers1

1

How about this:

You would have to evaluate the buttons parentViews onTouchEvents, and if you hit it, you can start the onClick method associated with the button, or when you lift your finger, what ever suits your app.

But while finger down/move you can just change the buttons coordinates and trigger a redraw ont he parent view.

You might have to create a new View that supports moving Buttons. I dont know if you can do that inside of a layout.

Daniel Bo
  • 2,518
  • 1
  • 18
  • 29