2

I found a beginner-friendly tutorial here: http://androidrox.wordpress.com/2011/05/13/android-sample-app-drag-and-drop-image-using-touch/

here is the xml code in my case:

<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/absLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

    <Button
        android:id="@+id/myButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Countries" />

</AbsoluteLayout>

MainActivity.java:

public class MainActivity extends Activity {

AbsoluteLayout absLayout;
Button myButton = null;
boolean touched = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    absLayout = (AbsoluteLayout) findViewById(R.id.absLayout);
    myButton = (Button) findViewById(R.id.myButton);

    myButton.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            touched = true;
            return false;
        }
    });

    absLayout.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if(touched == true) // any event from down and move
            {
                LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,(int)event.getX()-button_Countries.getWidth()/2,(int)event.getY()-myButton.getHeight()/2);
                myButton.setLayoutParams(lp);
            }
            if(event.getAction()==MotionEvent.ACTION_UP){
                touched = false;
            }
            return true;
        }
    });
}

Basically what happens here I need to touch myButton once first to convert the boolean touched to true. Then I might be able to drag myButton by start touching anywhere on absLayout. How can I simply drag the button freely without the need of touching the button first?

I tried setting the LayoutParams on myButton touch listener. It causes flickering to the dragging progress.

Also, AbsoluteLayout is always tagged as deprecated. What does "deprecated" means? Does it mean obsolete and therefore unusable? Or usable but there might be a better solution?

Hendra Anggrian
  • 5,780
  • 13
  • 57
  • 97
  • Check out the similar question and my answer [HERE][1] [1]: http://stackoverflow.com/questions/20606802/how-to-swipe-a-button-ontouch-event-in-android/20607696#20607696 – GrIsHu Dec 16 '13 at 12:24

1 Answers1

0
myButton.setOnTouchListener(new OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        touched = true;
        return false;
    }
});

The above part is responsible for touch that enables your button to be dragged. Now remove this part of the code and change the declaration to boolean touched = true;

Now you should be able to drag without touching the button first.

Sufian
  • 6,405
  • 16
  • 66
  • 120
Yogamurthy
  • 988
  • 15
  • 22
  • It's a good idea to prevent the button tapping first, but then I still have to tap anywhere on the absLayout and NOT the button itself. – Hendra Anggrian Dec 16 '13 at 20:15
  • can you tell why do you use an absolute layout.you can use linear or relative layout which works much better than absolute layout(its deprecated,dont use it anymore). – Yogamurthy Dec 17 '13 at 09:05