4

I want to move my buttons to another palce with touch. I mean if user touch one button and go to another place via touch, the button moves. I write this code for one of buttons, but when i touch one button, three button moves as well as and one button cannot move to right or left and when i move to top of screen, a part of image button deleted!

What is the problem? and How can i solve this? I want run this 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....

mmBs
  • 8,421
  • 6
  • 38
  • 46
SensorS
  • 383
  • 3
  • 8
  • 20

1 Answers1

9

Create a new Class that 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 your Main activity, set OnTouchListener on your view... that is your imageButton or imageView;

MultiTouchListener touchListener=new MultiTouchListener(this);
onButton.setOnTouchListener(touchListener);
Bhavna
  • 836
  • 2
  • 6
  • 19
  • Thanks for your code but again my program has problem :( When i touch and try to move to another place, btnFour move with btnOne(both together!! ) and when i touch btnTwo, btnThree move with this(but this happend first move before i touch btnThree and btnFour!) and i want when user click on the buttons, shows another activity, i implemented this with onClick() method but now with this code, onClick() method not work. What code i add to MultiTouchListener class for solve onClick()?? Thanks – SensorS Oct 07 '13 at 10:50
  • Add the following in button tag in your XML , the button you want to click android:focusable="true" android:focusableInTouchMode="true" – Bhavna Oct 07 '13 at 10:54
  • and another problem is when i move my buttons to bottom of screen or right of scrren, a part of my buttons deleted like part of buttons is under ImageView. Why? Have you any suggestion? – SensorS Oct 07 '13 at 10:57
  • I add this code but my problems not solved. Again i touch my buttons not shows another activity and when i touch btnOne and btnTow first, btnFour and btnThree move with this ! I think my problem is in xml file but i dont know how can i solve this . – SensorS Oct 07 '13 at 11:02
  • For the problem of ur button getting out of screen.. You can put some condition in MotionEvent.ACTION_MOVE: to check if your view is not going out of the screen. For this you can use if(currX< screenWidth) { view.setLayoutParams(layoutParams); } , where screenWidth is the width of the device. You have to get it through some code.. Similarly for the Y axis of the device.. – Bhavna Oct 07 '13 at 11:07
  • for move two buttons together, i check if i delete layout_alignBelow, my problem solved, but my buttons shows in bottom of screen and i dont want this :( Why this happened? – SensorS Oct 07 '13 at 11:22
  • Try using two relative Layouts,, one containing the above two buttons and another containing the below buttons and the aligning the IInd relative Layout below the second one.. – Bhavna Oct 07 '13 at 11:34
  • I will check this about 10-12 hours and if i have any question, i ask of you or i submit your answer :) Thanks – SensorS Oct 07 '13 at 13:00
  • @SensorS Yes.. please do inform about any queries – Bhavna Oct 09 '13 at 10:23
  • Can you tell what `MarginLayoutParams marginParams = new MarginLayoutParams( arg0.getLayoutParams()); marginParams.setMargins((int)( currentX - mPrevX), (int)( currentY - mPrevY),0, 0); LayoutParams layoutParams = new LinearLayout.LayoutParams(marginParams); arg0.setLayoutParams(layoutParams); ` is actually doing ? – utkarsh dubey Sep 08 '16 at 12:14