android - Moving buttons via Touch -


i want move buttons palce touch. mean if user touch 1 button , go place via touch, button moves. write code 1 of buttons, when touch 1 button, 3 button moves , 1 button cannot move right or left , when move top of screen, part of image button deleted!

what problem? , how can solve this? want run program in android +2.2

onbutton.setontouchlistener(new ontouchlistener(){          @override         public boolean ontouch(view v, motionevent event) {             // todo auto-generated method stub             switch(event.getaction()){                 case motionevent.action_move:                     relativelayout.layoutparams relativelayout=(relativelayout.layoutparams) onebutton.getlayoutparams();                     int x=(int)event.getrawx();                     int y=(int)event.getrawy();                      relativelayout.leftmargin=x-50;                     relativelayout.rightmargin=x-50;                     relativelayout.topmargin=y-50;                     relativelayout.bottommargin=y-50;                     onebutton.setlayoutparams(relativelayout);                     break;                     default:                         break;             }             return true;         }      }); 

xml:

<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent">  <imageview     android:id="@+id/img"     android:src="@drawable/root"     android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:layout_alignparenttop="true"     android:adjustviewbounds="true"     android:scaletype="fitxy"/> <imagebutton      android:id="@+id/btnone"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:layout_alignparentright="true"     android:layout_marginright="35dip"     android:layout_alignparenttop="true"     android:layout_margintop="70dip"     android:src="@drawable/onebutton"     android:background="@null"     android:visibility="invisible"/> <imagebutton      android:id="@+id/btntwo"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:layout_alignparentleft="true"     android:layout_marginleft="35dip"     android:layout_alignparenttop="true"     android:layout_margintop="70dip"     android:src="@drawable/twobutton"     android:background="@null"     android:visibility="invisible"/>    <imagebutton      android:id="@+id/btnthree"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:layout_below="@id/btnone"     android:layout_alignparentleft="true"     android:layout_marginleft="15dip"     android:layout_alignparentbottom="true"     android:layout_marginbottom="10dip"     android:src="@drawable/threebutton"     android:background="@null"     android:visibility="invisible"/>    <imagebutton      android:id="@+id/btnfour"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:layout_below="@id/btntwo"     android:layout_alignparentright="true"     android:layout_marginright="15dip"     android:layout_alignparentbottom="true"     android:layout_marginbottom="10dip"     android:src="@drawable/fourbutton"     android:background="@null"     android:visibility="invisible"/>   </relativelayout> 

thanks....

create new class implements ontouchlistener

import android.annotation.suppresslint; import android.util.log; import android.view.motionevent; import android.view.view; import android.view.view.ontouchlistener; import android.view.viewgroup.marginlayoutparams; import android.widget.relativelayout;  public class multitouchlistener implements ontouchlistener {  private float mprevx; private float mprevy;  public mainactivity mainactivity; public multitouchlistener(mainactivity mainactivity1) {     mainactivity = mainactivity1; }  @override public boolean ontouch(view view, motionevent event) {     float currx,curry;     int action = event.getaction();     switch (action ) {         case motionevent.action_down: {              mprevx = event.getx();             mprevy = event.gety();             break;         }          case motionevent.action_move:         {                  currx = event.getrawx();                 curry = event.getrawy();                   marginlayoutparams marginparams = new marginlayoutparams(view.getlayoutparams());                    marginparams.setmargins((int)(currx - mprevx), (int)(curry - mprevy),0, 0);                 relativelayout.layoutparams layoutparams = new relativelayout.layoutparams(marginparams);                 view.setlayoutparams(layoutparams);                break;         }            case motionevent.action_cancel:             break;          case motionevent.action_up:              break;     }      return true; }  } 

now in main activity, set ontouchlistener on view... imagebutton or imageview;

multitouchlistener touchlistener=new multitouchlistener(this); onbutton.setontouchlistener(touchlistener); 

Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -