Drag and drop RelativeLayout onto Google Map

I ran into the problem of dragging and dropping the layout on a Google Map. When I tried to drag this layout, it will disappear.

I am using this code: -

imggg.setOnTouchListener(new View.OnTouchListener() {
        int prevX,prevY;
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            final RelativeLayout.LayoutParams par=(RelativeLayout.LayoutParams)v.getLayoutParams();
            switch(event.getAction())
            {
                case MotionEvent.ACTION_MOVE:
                {
                    par.topMargin+=(int)event.getRawY()-prevY;
                    prevY=(int)event.getRawY();
                    par.leftMargin+=(int)event.getRawX()-prevX;
                    prevX=(int)event.getRawX();
                    v.setLayoutParams(par);
                    return true;
                }
                case MotionEvent.ACTION_UP:
                {
                    par.topMargin+=(int)event.getRawY()-prevY;
                    par.leftMargin+=(int)event.getRawX()-prevX;
                    v.setLayoutParams(par);
                    return true;
                }
                case MotionEvent.ACTION_DOWN:
                {
                    prevX=(int)event.getRawX();
                    prevY=(int)event.getRawY();
                    par.bottomMargin=-2*v.getHeight();
                    par.rightMargin=-2*v.getWidth();
                    v.setLayoutParams(par);
                    return true;
                }
            }
            return false;

        }
    });  

here, imggg is the relative layout object and this is the overlay on the google map. Now I want to drag this layout to the touch, as we drag the marker from one position to another.

Please help me with this. Thanks in advance.

+4
source share
1 answer

You can use this code to drag RelativeLayout:

RelativeLayout draggable = (RelativeLayout) findViewById(R.id.draggable);
draggable.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(final View view, final MotionEvent motionEvent) {
        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) view.getLayoutParams();

        switch (motionEvent.getAction()) {
            case MotionEvent.ACTION_MOVE: {
                params.topMargin = (int) motionEvent.getRawY() - (view.getHeight() / 2);
                params.leftMargin = (int) motionEvent.getRawX() - (view.getWidth() / 2);
                view.setLayoutParams(params);
                return true;
            }
            case MotionEvent.ACTION_UP: {
                return true;
            }
            case MotionEvent.ACTION_DOWN: {
                return true;
            }
        }

        return false;
    }
});

And this is the layout so that this example is completed:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">

    <fragment
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MapsActivity"/>

    <RelativeLayout
        android:id="@+id/draggable"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="#FF0000"
        android:clickable="true"/>

</RelativeLayout>
+1
source

All Articles