How to make Drag & Drop Button in Android -


i want make drag , drop button. drag want , stays there. below code scales button, doesn't change position.

package com.dynamic; import android.app.activity; import android.os.bundle; import android.view.motionevent; import android.view.view; import android.view.viewgroup.layoutparams; import android.widget.button; import android.widget.framelayout; import android.widget.imageview; import android.widget.textview;  import com.dynamic.r; import com.dynamic.r.layout;   public class dy extends activity {      int status;      private framelayout layout;      imageview image;      button b;      layoutparams params;      public void oncreate(bundle savedinstancestate) {             super.oncreate(savedinstancestate);             setcontentview(r.layout.frm);             final button tv=(button)findviewbyid(r.id.txt_birth);             tv.setdrawingcacheenabled(true);              layout = (framelayout) findviewbyid(r.id.frm);             params = new layoutparams(100,100);               params = new layoutparams(layoutparams.wrap_content,layoutparams.wrap_content);             tv.setontouchlistener(new view.ontouchlistener(){          @override         public boolean ontouch(view v, motionevent me) {             // todo auto-generated method stub              if (me.getaction() == motionevent.action_down) {                 system.out.println("up");                 status = 0;              }             if (me.getaction() == motionevent.action_up) {                 status = 1;                 //log.i("drag", "stopped dragging");             } else if (me.getaction() == motionevent.action_move) {                 if (status == 0) {                     system.out.println("dragging");                      tv.setpadding((int) me.getrawx(), (int) me.getrawy(), 0, 0);                 //  b.setpadding(0,50,0,0);                     tv.invalidate();                  }             }             return false;         }      }); } } 

i working on similar this. here ontouchlistener using:

         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 view wanting move, in case you'd replace v button. note in order work had use absolutelayout parent view in xml file. know deprecated seemed more logical use relativelayout , trying set margins dynamically move view around. formula used new x , y positions tries make view centered on finger while moving it. not quite perfect, depending on size of view still little off center finger.


Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -