Click here to Skip to main content
15,884,838 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi dears
I want to create dragable button in android using Api7 or Api8.
I search for it but I couln't find my answer.
How could I create this button?
Posted

Java
myOnTouchListener = new OnTouchListener() {
        public boolean onTouch(View v, MotionEvent me){
            if (me.getAction() == MotionEvent.ACTION_DOWN){
                oldXvalue = me.getX();
                oldYvalue = me.getY();
                Log.i(myTag, "Action Down " + oldXvalue + "," + oldYvalue);
            }else if (me.getAction() == MotionEvent.ACTION_MOVE  ){
               LayoutParams params = new LayoutParams(v.getWidth(), v.getHeight(),(int)(me.getRawX() - (v.getWidth() / 2)), (int)(me.getRawY() - (v.getHeight())));
               v.setLayoutParams(params);
            }
            return true;
        }
    };


v is the view that you are wanting to move, in your case it you'd replace v with your button.
Also note that in order to get this to work you had to use an AbsoluteLayout as the parent view in my xml file.
I know that it is deprecated but it seemed more logical to use that then a RelativeLayout and trying to set the margins dynamically to move the view around.

original
 
Share this answer
 
Comments
omid.nazifi 4-Sep-13 22:17pm    
What kind of Layout implementd 'LayoutParams' that you used here?
_Sahar 5-Sep-13 5:51am    
I used 'AbsoluteLayout'
omid.nazifi 7-Sep-13 1:17am    
Thanks Sahar, it worked very well. but you know AbsoluteLayout was deprecated in new versions. Do you know any replacement for it?
_Sahar 7-Sep-13 3:11am    
No,I don't know.
I'm beginner in android.
Would you help me what I should do?
omid.nazifi 7-Sep-13 7:37am    
Yes, you should change it with RelativeLayout either in xml and code. follow below link ;-)

http://stackoverflow.com/a/3295056/967548
First of all, use a RelativeLayout in your xml file:

XML
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android">
                android:id="@+id/my_relative_layout"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

    <imageview>
            android:id="@+id/image1"
            android:layout_width="150dp"
            android:layout_height="150dp"
            android:src="@drawable/splash0"/>


    <imageview>
            android:id="@+id/image2"
            android:layout_width="150dp"
            android:layout_height="150dp"
            android:src="@drawable/splash0" 
            android:layout_x="110dp" 
             android:layout_y="152dp"/>
</imageview></imageview></relativelayout>



Then change your activity, see my example:

Java
public class MyActivity extends Activity implements View.OnTouchListener {
 
    private float oldXvalue;
    private float oldYvalue;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ImageView image1 = (ImageView) findViewById(R.id.image1);
        ImageView image2 = (ImageView) findViewById(R.id.image2);
        image1.setOnTouchListener(this);
        image2.setOnTouchListener(this);
    }

    @Override
    public boolean onTouch(View v, MotionEvent me) {
        if (me.getAction() == MotionEvent.ACTION_DOWN) {
            oldXvalue = me.getX();
            oldYvalue = me.getY();
            Log.i("Omid", "Action Down " + oldXvalue + "," + oldYvalue);
        } else if (me.getAction() == MotionEvent.ACTION_MOVE) {
            RelativeLayout rl = (RelativeLayout) findViewById(R.id.my_relative_layout);
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(v.getWidth(), v.getHeight());
            params.leftMargin = (int) (me.getRawX() - (v.getWidth() / 2));
            params.topMargin = (int) (me.getRawY() - (v.getHeight()));
v.getHeight(), (int) (me.getRawX() - (v.getWidth() / 2)), (int) (me.getRawY() - (v.getHeight())));
            v.setLayoutParams(params);
        }
        return true;
    }
}


I tested it, it worked well. I hope it'll be fine for your :-)
 
Share this answer
 
Comments
_Sahar 8-Sep-13 5:51am    
Thanks for your help,It works so good.
omid.nazifi 8-Sep-13 5:54am    
You're welcome

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900