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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:47:02+00:00 2026-05-27T17:47:02+00:00

I am having trouble positioning view elements on a custom ViewGroup that I have

  • 0

I am having trouble positioning view elements on a custom ViewGroup that I have created, specifically in drag-and-drop situations. I am targeting Android 2.2 and up, so I can’t really use the drag-and-drop API that came into existence in Android 3.

My custom ViewGroup is called “NodeGrid” and it extends “RelativeLayout”. Its onLayout method is overriden such that it looks like this:

@Override
protected void onLayout(boolean arg0, int arg1, int arg2, int arg3, int arg4) 
{
    //pxConversion is a multiplier to convert 1 dip to x pixels.
    //We will use it to convert dip units into px units.
    Resources r = getResources();
    float pxConversion = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1, r.getDisplayMetrics());

    //Layout the children of this viewgroup
    int childCount = this.getChildCount();
    for (int i = 0; i < childCount; i++)
    {
        NodeView view = (NodeView) this.getChildAt(i);

        if (view.DataContext != null)
        {
            int left = (int) (view.DataContext.get_left() * pxConversion);
            int top = (int) (view.DataContext.get_top() * pxConversion);
            int right = left + (int) (view.DataContext.get_width() * pxConversion);
            int bottom = top + (int) (view.DataContext.get_height() * pxConversion);

            view.layout(left, top, right, bottom);
        }
    }   
}

The children of “NodeGrid” are of type “NodeView” (as you can see in the above code). NodeView is simply a custom View class that I have created. It contains a member called “DataContext” which is a view-model class that contains some getters/setters with position information about the NodeView instance on the NodeGrid.

My “NodeView” class captures touch events by the user, so the user can simply drag-and-drop a node anywhere on the grid. The event handler looks like this:

@Override
public boolean onTouchEvent(MotionEvent event) {

    if (event.getAction() == MotionEvent.ACTION_DOWN)
    {
        isBeingDragged = true;
    }
    else if (event.getAction() == MotionEvent.ACTION_UP)
    {
        isBeingDragged = false;
    }
    else if (event.getAction() == MotionEvent.ACTION_MOVE)
    {
        if (DataContext != null && isBeingDragged)
        {
            float xPosition = event.getRawX();
            float yPosition = event.getRawY();

            DataContext.set_left(xPosition);
            DataContext.set_top(yPosition);

            this.requestLayout();
        }
    }

    return true;
}

My problem is that the view that is being dragged isn’t positioned as I would expect it on my “NodeGrid”. It seems that the x-axis position is correct as I drag it, but the y-axis position is offset by some constant amount of pixels at any time. What would be causing this? I have tried using the getX() and getY() methods rather than getRawX() and getRawY(), but that only causes it to be worse.

I wasn’t sure whether getRawX() and getRawY() were returning px units or dip units to me, so expecting that it was giving me back px units, I tried converting them to dip units before assigning the new x and y values to the node, but that only decreased the offset, it did not eliminate it.

What could be causing the difference in where I am touching and where the node is being positioned?

  • 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-27T17:47:02+00:00Added an answer on May 27, 2026 at 5:47 pm

    Very surprised that apparently no one else has encountered this kind of situation…

    After some research, I have partially been able to solve the problem.

    The reason getRawX() and getRawY() did not work is because they work off the absolute screen position without regard to anything on the screen that is not part of your app (such as the Android menu which is constantly at the top of the screen unless you are in full screen mode). Using these coordinates seemed hackish, especially with no apparent way to justify them and convert them to “my app” coordinates.

    getX() and getY() seem to be relative to the object being touched as far as I can tell. When taking this into account, I changed the code in onTouchEvent to read:

    float xPosition = event.getX();
    float yPosition = event.getY();
    
    DataContext.set_left(DataContext.get_left() + xPosition);
    DataContext.set_top(DataContext.get_top() + yPosition);
    
    this.requestLayout();
    

    This definitely helps…but it still doesn’t seem to be a perfect solution. In general, it causes the view object to drag along with my mouse cursor/finger, but it is very jittery, and there are still times where it can get whacky and go off the screen.

    I then thought that maybe it is because I am not incorporating “historical coordinates” into the method. I tried incorporating historical coordinates into the method by putting this code immediately before the code that I just displayed above:

    int historySize = event.getHistorySize();
    for (int i = 0; i < historySize; i++)
    {
        float xPosition = event.getHistoricalX(i);
        float yPosition = event.getHistoricalY(i);
    
        DataContext.set_left(DataContext.get_left() + xPosition);
        DataContext.set_top(DataContext.get_top() + yPosition);
    
        this.requestLayout();
    }
    

    Unfortunately…this actually makes it worse, so either I am not using the historical coordinates correctly or they shouldn’t be used at all. I will post another question with regard to that matter, but this should suffice as a partial answer to my question above.

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

Sidebar

Related Questions

I'm having trouble positioning the layout elements. The AutoComplete in my TableLayout and the
Having trouble with proper regex for RewriteCond RewriteCond %{REQUEST_URI} !^/foo/ Works as expected, that
Having Trouble with Entity Framework. I have been populating EntityReferences with an EntityKey inorder
I having trouble in dividing the HTML frames. I have been using the following
I am having trouble with IE7. I have a header, which is an IMG.
I have created my entire website by using a main table, and having the
Having trouble with some select statements. I have 2 tables. sms_log and sms_messages. sms_log
I am having trouble positioning a (div) element at the top right of a
I'm having trouble properly positioning some divs within a larger div and having them
Having trouble with grepping and cutting at the same time I have a file

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.