android - Restrict dynamically added views from overlapping each other in relative layout -
i have relativelayout consisting button
this button add text views cand dragged , dropped when clicked
my problem how prevent these textvies overlapping each other when dragged , dropped on 1 another
dynamic adding , dragging of textviews said looks this
my main activity
public class mainactivity extends activity { relativelayout droplayoutnew; button addtext; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); droplayoutnew = (relativelayout) findviewbyid(r.id.edtxtlayout); droplayoutnew.setondraglistener(new mydraglistener()); addtext = (button) findviewbyid(r.id.mainbutton1); addtext.setonclicklistener(new mybuttonclicklistener()); } }
my clicklistener
public class mybuttonclicklistener implements onclicklistener { @override public void onclick(view v) { viewgroup relativeparent = (viewgroup) v.getparent(); customedittext edttxt = new customedittext(v.getcontext()); relativeparent.addview(edttxt); edttxt.setonlongclicklistener(new mylongclicklistener()); } }
** mylongclicklistner **
public class mylongclicklistner implements onlongclicklistener { @override public boolean onlongclick(view v) { clipdata dragdata = clipdata.newplaintext("",""); view.dragshadowbuilder shdwbldr = new view.dragshadowbuilder(v); v.startdrag(dragdata, shdwbldr, v, 0); v.setvisibility(view.invisible); return true; } }
my draglistener
public class mydraglistener implements ondraglistener { private layoutparams params; @override public boolean ondrag(view v, dragevent event) { view view = (view) event.getlocalstate(); switch(event.getaction()) { case dragevent.action_drag_started: params = (relativelayout.layoutparams) view.getlayoutparams(); break; case dragevent.action_drag_entered: int x = (int) event.getx(); int y = (int) event.gety(); break; case dragevent.action_drag_exited : break; case dragevent.action_drag_location : x= (int) event.getx(); y = (int) event.gety(); break; case dragevent.action_drag_ended : break; case dragevent.action_drop: x = (int) event.getx(); y = (int) event.gety(); params.leftmargin = x; params.topmargin = y; view.setlayoutparams(params); view.setvisibility(view.visible); break; default: break; } return true; } }
mybuttonclicklistener
public class mybuttonclicklistener implements onclicklistener { @override public void onclick(view v) { viewgroup relativeparent = (viewgroup) v.getparent(); textview txt = new textview(v.getcontext()); relativeparent.addview(txt); txt.settext("drag me"); txt.setonlongclicklistener(new mylongclicklistner()); } }
my layout
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#cdc2c0" android:id="@+id/ondraglayout"> <button android:layout_height="wrap_content" android:text="t" android:layout_width="wrap_content" android:layout_alignparentbottom="true" android:layout_alignparentright="true" android:id="@+id/mainbutton1"/> <relativelayout android:layout_height="match_parent" android:layout_width="match_parent" android:layout_above="@id/xvalue" android:background="#e1a29d" android:id="@+id/edtxtlayout"/>
Comments
Post a Comment