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 7162359
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:43:17+00:00 2026-05-28T13:43:17+00:00

I’m trying to do an app which user can move image… And when the

  • 0

I’m trying to do an app which user can move image… And when the user move it to a certain place, app should do something else… Kinda unlocking…

But the problem is when ACTION_UP statement doesn’t work… Any suggestions?

Here’s my codes:

public class Main extends Activity {

    private View selected_item = null;
    private int offset_x = 0;
    private int offset_y = 0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ViewGroup vg = (ViewGroup) findViewById(R.id.lout);
        vg.setOnTouchListener(new View.OnTouchListener() {

            @Override
            @SuppressWarnings("deprecation")
            public boolean onTouch(View v, MotionEvent event) {

                          switch (event.getActionMasked()) {
                case MotionEvent.ACTION_MOVE:
                    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;
                    AbsoluteLayout.LayoutParams lp = new AbsoluteLayout.LayoutParams(
                            new ViewGroup.MarginLayoutParams(
                                    AbsoluteLayout.LayoutParams.WRAP_CONTENT,
                                    AbsoluteLayout.LayoutParams.WRAP_CONTENT));
                    lp.x = x;
                    lp.y = y;
                    selected_item.setLayoutParams(lp);
                    break;
                default:
                    break;
                }
                return true;
            }
        });

        ImageView img = (ImageView) findViewById(R.id.imageView1);
        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;
                    break;
                case MotionEvent.ACTION_UP:
                    if (offset_x < 150 && offset_x > 120 && offset_y < 230
                            && offset_y > 200)
                        startActivity(new Intent(
                                "com.yahya.GeneralTraining.UNLOCKED"));
                    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-05-28T13:43:18+00:00Added an answer on May 28, 2026 at 1:43 pm

    Seems like your implementation is not exactly what you are looking for, you should assign offset_x and offset_y values inside ACTION_UP instead of ACTION_DOWN. Right now it will work like if user started dragging from x(120 to 150) and y(200 to 230) and drop anywhere. Unlock intent will fire, but what you want is drag from anywhere and drop to x(120 to 150) and y(200 to 230).

    So write:

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        //Are you sure you want event.getActionMasked() ??
        //I use event.getAction() 
        switch (event.getAction()) {
           case MotionEvent.ACTION_DOWN:
           //I don't believe you really need anything to do here
           break;
           case MotionEvent.ACTION_UP:
               offset_x = (int) event.getX();
               offset_y = (int) event.getY();
               selected_item = v;
               if (offset_x < 150 && offset_x > 120 && offset_y < 230 && offset_y > 200)
                   startActivity(new Intent("com.yahya.GeneralTraining.UNLOCKED"));
            break;
            default:
            break;
        }
        return false;
    }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I used javascript for loading a picture on my website depending on which small
I have a jquery bug and I've been looking for hours now, I can't
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I need to clean up various Word 'smart' characters in user input, including but

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.