Click here to Skip to main content
15,889,865 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi,

I have two image views (IM1, IM2) contained with a relative layout. IM1 is moved by the accellorometer up and down inside the relative layout, over IM2. IM2 is a line and IM2 is a ball. I want to restrict the movement of the ball to the top and bottom of the line.

I got the top and bottom y coordinate of the line as follows:

int[] cordxy = {0,0};
levelBar.getLocationOnScreen(cordxy);

int absoluteTop = cordxy[1];
int absoluteBottom = cordxy[1] + levelBar.getHeight();


I get the y coordinate of the ball as follows:

Java
int[] posXY = {0,0};
indicator.getLocationOnScreen(posXY);
int yPos = posXY[1];


I then carry out a check to see if the y coordinate of the ball is less than the top or greater than the bottom, if it is set y to the y coordinate of equal to the top or bottom y coordinate.

if(yPos<=absoluteTop) //set yPos to absoluteTop
if(yPos=absoluteBottom) //set yPos to absoluteBottom

I tried this in a number of ways, the most promising is below

RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT);
 lp.topMargin = 0;

levelH.addView(indicator, lp)

if(yPos=absoluteTop) levelH.addView(indicator, lp);

This gives me an error as below:

09-12 21:28:52.630: E/AndroidRuntime(9330): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.


If someone could tell me how to set the position of the image view holding the ball I'd appreciate it. I've tried lots of options but I couldn't get any to produce the correct effect.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900