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
Asked
Active
Viewed 1,201 times
1
-
[Check](https://androidician.wordpress.com/2014/09/24/android-custom-toggle-button-example-ios-like-toggle-buttons/) – Skynet Feb 22 '16 at 12:42
-
@Skynet the reference you gave is for a togglebutton only,he wants something different ! – Animesh Mangla Feb 22 '16 at 12:44
-
He can modify this. However there might exist multiple solutions. – Skynet Feb 22 '16 at 13:09
1 Answers
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