Android Drag and drop images on the Screen?

20,633

Solution 1

Write Below Code into your Activity File.

windowwidth = getWindowManager().getDefaultDisplay().getWidth();
windowheight = getWindowManager().getDefaultDisplay().getHeight();


tv1 = (ImageView)findViewById(R.id.image);
tv1.setOnTouchListener(new View.OnTouchListener() {         

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        layoutParams1 = (RelativeLayout.LayoutParams) tv1.getLayoutParams();
        switch(event.getActionMasked())
        {
            case MotionEvent.ACTION_DOWN:
                break;
            case MotionEvent.ACTION_MOVE:
                int x_cord = (int) event.getRawX();
                int y_cord = (int) event.getRawY();
                if (x_cord > windowwidth) {
                    x_cord = windowwidth;
                }
                if (y_cord > windowheight) {
                    y_cord = windowheight;
                }
                layoutParams1.leftMargin = x_cord - 25;
                layoutParams1.topMargin = y_cord - 75;
                tv1.setLayoutParams(layoutParams1);
                break;
            default:
                break;
        }
        return true;
    }
});

tv2 = (ImageView)findViewById(R.id.image1);
tv2.setOnTouchListener(new View.OnTouchListener() {         

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        layoutParams2 = (RelativeLayout.LayoutParams) tv2.getLayoutParams();
        switch(event.getActionMasked())
        {
            case MotionEvent.ACTION_DOWN:
                break;
            case MotionEvent.ACTION_MOVE:
                int x_cord = (int) event.getRawX();
                int y_cord = (int) event.getRawY();
                if (x_cord > windowwidth) {
                    x_cord = windowwidth;
                }
                if (y_cord > windowheight) {
                    y_cord = windowheight;
                }
                layoutParams2.leftMargin = x_cord - 25;
                layoutParams2.topMargin = y_cord - 75;
                tv2.setLayoutParams(layoutParams2);
                break;
            default:
                break;
        }
        return true;
    }
});

XML File:-

<?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:layout_width="50sp" android:layout_height="50sp"
        android:id="@+id/image" android:src="@drawable/image">
    </ImageView>
    <ImageView android:layout_y="30dip" android:layout_x="118dip"
        android:layout_width="50sp" android:layout_height="50sp" android:id="@+id/image1"
        android:src="@drawable/image1">
    </ImageView>
</RelativeLayout>

Solution 2

That's because you have put everything in a LinearLayout, which means you can't place the items where you want them, they are always one after the other. You can try to use a RelativeLayout instead. If that is not flexible enough, you should look at Canvas.

Share:
20,633
NagarjunaReddy
Author by

NagarjunaReddy

My Gitlab Applications http://pnrandroid.blogspot.in/2018/03/how-to-handle-storage-permissions-in.html https://wordpress.com/stats/day/polamreddyn.wordpress.com Usfull Questions: 1) http://stackoverflow.com/questions/424752/any-good-graphing-packages-for-android 2) http://android-graphview.org/ 3) http://jaxenter.com/effort-free-graphs-on-android-with-achartengine-46199.html 4) http://www.dreamincode.net/forums/topic/303235-visualizing-sound-from-the-microphone/ 5) https://github.com/melanke/MlkAndroidChartApi 6) http://www.osciprime.com/?p=source 7) http://android.codeandmagic.org/achartengine-live-scrolling-graph/ 9) http://android-innovation.blogspot.in/2013/07/how-to-implement-pinch-and-pan-zoom-on.html 10) Full to Refresh https://github.com/chrisbanes/Android-PullToRefresh

Updated on July 09, 2022

Comments

  • NagarjunaReddy
    NagarjunaReddy almost 2 years

    I m working on project user to move the image in one position to Another position on the screen. I have written a sample code to move the image but the problem here is if I move one image the neighbouring image also starts moving.. Here is the sample code.any one Idea of this.

    Main.java

    public class MainActivity extends Activity  {
       int windowwidth;
       int windowheight;    
       ImageView ima1,ima2;
    
       private android.widget.RelativeLayout.LayoutParams layoutParams ;
       // private android.widget.RelativeLayout.LayoutParams layoutParams ;
       //private android.widget.RelativeLayout.LayoutParams layoutParams ;           
    
         @Override
         public void onCreate(Bundle savedInstanceState) {
             super.onCreate(savedInstanceState);
             setContentView(R.layout.main);
    
             windowwidth = getWindowManager().getDefaultDisplay().getWidth();
             windowheight = getWindowManager().getDefaultDisplay().getHeight();
    
             System.out.println("width" +windowwidth);
             System.out.println("height" +windowheight);             
    
             ima1 = (ImageView)findViewById(R.id.imageview1);
             ima1.setOnTouchListener(new View.OnTouchListener() {  
    
    public boolean onTouch(View v, MotionEvent event) {
           layoutParams = (RelativeLayout.LayoutParams) ima1.getLayoutParams();
    
             switch(event.getAction())                   
                {
                  case MotionEvent.ACTION_DOWN:                          
                        break;     
    
                  case MotionEvent.ACTION_MOVE:
                        int x_cord = (int) event.getRawX();
                        int y_cord = (int) event.getRawY();
    
                  System.out.println("value of x" +x_cord);
                  System.out.println("value of y" +y_cord);           
    
                        if (x_cord > windowwidth) {
                            x_cord = windowwidth;
                           }
                        if (y_cord > windowheight) {
                            y_cord = windowheight;
                           }
                 layoutParams.leftMargin = x_cord-25;
                 layoutParams.topMargin = y_cord-25;
                 //   layoutParams.rightMargin = x_cord-25;
                 //   layoutParams.bottomMargin = y_cord-25;
                 ima1.setLayoutParams(layoutParams);
                         break;
                   default: break;
                  }  
                   return true;
                }
             });
    
             ima2 = (ImageView)findViewById(R.id.imageview2);
             ima2.setOnTouchListener(new View.OnTouchListener() {         
    
         public boolean onTouch(View v, MotionEvent event) {
             layoutParams = (RelativeLayout.LayoutParams) ima2.getLayoutParams();
                  switch(event.getActionMasked())
                     {
                       case MotionEvent.ACTION_DOWN:
                           break;
                       case MotionEvent.ACTION_MOVE:
                           int x_cord = (int) event.getRawX();
                           int y_cord = (int) event.getRawY();
    
                           System.out.println("value of x1" +x_cord);
                       System.out.println("value of y1" +y_cord);                            
    
                            if (x_cord > windowwidth) {
                                x_cord = windowwidth;
                            }
                            if (y_cord > windowheight) {
                                y_cord = windowheight;
                            }
                            layoutParams.leftMargin = x_cord - 25;
                            layoutParams.topMargin = y_cord - 75;
                            ima2.setLayoutParams(layoutParams);
                            break;
                        default: break;
                    }
                    return true;
                }
            });
           }
       }
    

    main.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:layout_width="100dp" 
        android:layout_height="100dp"
        android:id="@+id/imageview1" 
        android:src="@drawable/image1"  />    
    <ImageView
        android:layout_width="100sp" 
        android:layout_height="100sp" 
        android:id="@+id/imageview2"
        android:src="@drawable/image2"   />             
     </RelativeLayout>
    
  • Dipak Keshariya
    Dipak Keshariya about 12 years
    Please see my edited answer and use imageview instead of textview.
  • banzai86
    banzai86 about 12 years
    Just to clarify, you have 3 images on a 'RelativeLayout' and you want to move them separately over the screen, and they should be able to overlap each other, right?
  • NagarjunaReddy
    NagarjunaReddy about 12 years
    YES three images in layout move them separately over the screen
  • banzai86
    banzai86 about 12 years
    You kept your onTouch code and just replaced the LinearLayout with a RelativeLayout and still more then one image is moving or what is the problem at the moment?
  • NagarjunaReddy
    NagarjunaReddy about 12 years
    use that one move only one image and move to some corners the size of the image is decrease and the second image is not possible to moving...
  • Dipak Keshariya
    Dipak Keshariya about 12 years
    Above Code is Move Both Images Independent.
  • NagarjunaReddy
    NagarjunaReddy about 12 years
  • NagarjunaReddy
    NagarjunaReddy about 12 years
    java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.touchin/com.touchin.Main}: java.lang.InstantiationException: com.touchin.Main and Caused by: java.lang.InstantiationException: com.touchin.Main
  • Dipak Keshariya
    Dipak Keshariya about 12 years
    but this code is working for me, and please first clean your project and then run your project.
  • Dipak Keshariya
    Dipak Keshariya about 12 years
    but before some minutes you tell me, "use that one move only one image and move to some corners the size of the image is decrease and the second image is not possible to moving", so is it working that time?
  • Dipak Keshariya
    Dipak Keshariya about 12 years
    then problem is not in my code and if answer is helpful to you then accept it, so it may helpful to others.
  • NagarjunaReddy
    NagarjunaReddy about 12 years
    hi finally got it but image moving right and bottom edges image size will by decrease how to solve this
  • Dipak Keshariya
    Dipak Keshariya about 12 years
    sorry @NagarjunaReddy i don't know about this.
  • Paresh Mayani
    Paresh Mayani over 11 years
    Dear this kind posting answer again is not allowed. I mean you are posting same answer with same content which is not allowed.
  • Juned
    Juned about 11 years
    @NagarjunaReddy how did you solve your problem? i am facing the same problem
  • nilesh prajapati
    nilesh prajapati over 6 years
    hello any one get solution!!!!!!! i m facing same problem on image move proper but another on move on one direction and also the size of image cange when i move it.