Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8450325
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T10:56:22+00:00 2026-06-10T10:56:22+00:00

I am making a module in which I have two images whenever i touch

  • 0

I am making a module in which I have two images whenever i touch one image it should follow the finger or mouse(in emulator) on drag and if it comes over the other image then they change their positions where the first image was on first touch(ACTION_DOWN) . i have written the following code in which the views are moving but when i drag the first image second is also dragged. Further would like to have idea on how to change the positions.

.xml

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:id="@+id/vg"
   >

<ImageView   
    android:id="@+id/img"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
       />
 <ImageView   
    android:id="@+id/img1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    />
</LinearLayout>

activity file

public class MainActivity extends Activity {
    private View selected_item = null;
    private int offset_x = 0;
    private int offset_y = 0;
    Canvas can;
    Paint paint;
    ImageView img;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ViewGroup vg = (ViewGroup)findViewById(R.id.vg);
    vg.setOnTouchListener(new View.OnTouchListener() {

                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                            switch(event.getActionMasked())
                            {

                                    case MotionEvent.ACTION_MOVE:
                                        if(selected_item == img) {
                                            int x = (int)event.getX() - offset_x;
                                            int y = (int)event.getY() - offset_y;

                    int w = getWindowManager().getDefaultDisplay().getWidth() - 100;
                    int h = getWindowManager().getDefaultDisplay().getHeight() - 100;
                    if(x > w)
                        x = w;
                    if(y > h)
                        y = h;
                                     LinearLayout.LayoutParams lp = new  LinearLayout.LayoutParams(
                                    new ViewGroup.MarginLayoutParams(
                                                    100,
                                                    100));
                                     lp.setMargins(x, y, 0, 0);

                                            selected_item.setLayoutParams(lp); 
                                        }
                                            break;
                                    default:
                                            break;
                            }
                            return true;
                    }
  });
   img = (ImageView)findViewById(R.id.img);

   //timerDelayRemoveView(500, img);

   BitmapDrawable drawable = (BitmapDrawable)getResources().getDrawable(R.drawable.imagesl_02); 
   Bitmap bitmap = drawable.getBitmap();
   Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, 100, 100, true);
   img.setImageBitmap(scaledBitmap);
   LinearLayout.LayoutParams lp0 = new LinearLayout.LayoutParams(100, 100);
   lp0.leftMargin = 0;
   lp0.topMargin = 0;
   img.setLayoutParams(lp0);
   //vg.addView(img, lp1);
  // vg.addView(img, 1);
    img.setOnTouchListener(new View.OnTouchListener() {

                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                            switch(event.getActionMasked())
                            {
                                    case MotionEvent.ACTION_DOWN:
                                            offset_x = (int)event.getX();
                                            offset_y = (int)event.getY();
                                            selected_item = v;
                          Toast.makeText(MainActivity.this, "down",Toast.LENGTH_SHORT).show();
                                            break;

                            default: break;        
                            }

                            return false;
                    }
            });

    ImageView img1 = (ImageView)findViewById(R.id.img1);
    BitmapDrawable drawable1 = (BitmapDrawable)getResources().getDrawable(R.drawable.realimage); 
    Bitmap bitmap1 = drawable1.getBitmap();
    Bitmap scaledBitmap1 = Bitmap.createScaledBitmap(bitmap1, 100, 100, true);
    img1.setImageBitmap(scaledBitmap1);
    LinearLayout.LayoutParams lp1 = new LinearLayout.LayoutParams(100, 100);
    lp1.leftMargin = 100;
    lp1.topMargin = 100;
    img1.setLayoutParams(lp1);

    //img.setImageBitmap(scaledBitmap1);

    img1.setOnTouchListener(new View.OnTouchListener() {

                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                            switch(event.getActionMasked())
                            {
                                    case MotionEvent.ACTION_DOWN:
                                           // offset_x = (int)event.getX();
                                           // offset_y = (int)event.getY();
                                            selected_item = v;
                                            break;
                                    default:
                                            break;
                            }

                            return false;
                    }
            }); 

}
  }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T10:56:24+00:00Added an answer on June 10, 2026 at 10:56 am

    The second Image is getting dragged because you have the two in a LinearLayout. The second image’s layout parameters are dependent on the first, meaning that as you adjust them to the right, the second image is bound to the right edge of the first.

    If I were approaching this problem (Assuming I couldn’t use the dragging API introduced in Honeycomb), I would first put everything in a RelativeLayout with 2 ImageViews. When you “pick up” one of the images, you adjust its layout parameters as you move it around, and then when you drop it you adjust the LayoutParams of both to meet your desired layout.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application which has many services and one UI module. All these
I have a python module which contains a few objects, one of which uses
I have this example module which I want to initialize without making individual call
i am making a module which would generate and transmit ip packets for voice.
I am making a Magento module which is enabled, and my cache is off.
I am making a module in which I require the mac address of network
im making some script with mechanize.browser module. one of problem is all other thing
I have a Hudson CI server which is building a making a maven build.
I have a weird case here.... I'm making a simple magento module right now.
I am making a custom module which also enables an edit page to edit

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.