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?