drag and drop - custom dragable icon in android -
package com.example.drag_drop_practice; import android.os.bundle; import android.app.activity; import android.view.motionevent; import android.view.view; import android.view.viewgroup; import android.widget.imageview; import android.widget.relativelayout; public class mainactivity extends activity implements view.ontouchlistener { private imageview mimageview; private viewgroup mrrootlayout; private int _xdelta; private int _ydelta; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); mrrootlayout = (viewgroup) findviewbyid(r.id.root); mimageview = (imageview) mrrootlayout.findviewbyid(r.id.imageview); relativelayout.layoutparams layoutparams = new relativelayout.layoutparams(150, 150); mimageview.setlayoutparams(layoutparams); mimageview.setontouchlistener(this); } public boolean ontouch(view view, motionevent event) { final int x = (int) event.getrawx(); final int y = (int) event.getrawy(); switch (event.getaction() & motionevent.action_mask) { case motionevent.action_down: relativelayout.layoutparams lparams = (relativelayout.layoutparams) view.getlayoutparams(); _xdelta = x - lparams.leftmargin; _ydelta = y - lparams.topmargin; break; case motionevent.action_up: break; case motionevent.action_pointer_down: break; case motionevent.action_pointer_up: break; case motionevent.action_move: relativelayout.layoutparams layoutparams = (relativelayout.layoutparams) view.getlayoutparams(); layoutparams.leftmargin = x - _xdelta; layoutparams.topmargin = y - _ydelta; layoutparams.rightmargin = -250; layoutparams.bottommargin = -250; view.setlayoutparams(layoutparams); break; } mrrootlayout.invalidate(); return true; } }
the icon in app draggable code.but problem not able allign scree in android.i mean icon been dragged out of view when drag icon @ border extreme ends.so should add in code,so icon not go beyond border in android.
Comments
Post a Comment