1

I wanted to use sliding image button in android just like the alarm clock screen in android please help me through this. Thank you.screenshot of what I want to create

Mithilesh Izardar
  • 2,117
  • 1
  • 13
  • 24

1 Answers1

0

You can make a custom ontouchlistener for it and set your x and y position accordingly.

Here is a sample for it.

https://stackoverflow.com/a/19222290/5193608

MAin code part from it (in case link expires):

public class MultiTouchListener implements OnTouchListener
{

private float mPrevX;
private float mPrevY;

public MainActivity mainActivity;
public MultiTouchListener(MainActivity mainActivity1) {
    mainActivity = mainActivity1;
}

@Override
public boolean onTouch(View view, MotionEvent event) {
    float currX,currY;
    int action = event.getAction();
    switch (action ) {
        case MotionEvent.ACTION_DOWN: {

            mPrevX = event.getX();
            mPrevY = event.getY();
            break;
        }

        case MotionEvent.ACTION_MOVE:
        {

                currX = event.getRawX();
                currY = event.getRawY();


                MarginLayoutParams marginParams = new MarginLayoutParams(view.getLayoutParams());   
                marginParams.setMargins((int)(currX - mPrevX), (int)(currY - mPrevY),0, 0);
                RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(marginParams);
                view.setLayoutParams(layoutParams); 


            break;
        }



        case MotionEvent.ACTION_CANCEL:
            break;

        case MotionEvent.ACTION_UP:

            break;
    }

    return true;
}

}

Now in your Main activity, set OnTouchListener on your view... that is your imageButton or imageView;

MultiTouchListener touchListener=new MultiTouchListener(this);
onButton.setOnTouchListener(touchListener);

Hope,it helps !.

Community
  • 1
  • 1
Animesh Mangla
  • 773
  • 7
  • 31