Question

I am trying to do something fairly simple: move an image view by incrementing either the X or Y coordinates. Initially I used the functions getX, getY, setX and setY on the image view, which worked in the emulator. However, when using it with the phone I had an error called NoSuchMethodError on these functions.

Here's the exact error I am getting (I get this for each of the functions I listed above): java.lang.NoSuchMethodError: android.widget.ImageView.getX

According to my research this was caused by the fact that setX and setY are only implemented on the API11, in which case we are using API10. I have tried various other ways, such as using setMatrix, changePos() and a few other functions to change the ImageView's position.

My question is, what can I use to move the ImageViews like this, if I am using the API10?

Was it helpful?

Solution

You could try playing with layout parameters. Assuming you are using FrameLayout, or any other layout that supports margins

private ImageView myView;// populated in onCcreate

private void moveImage(int x, int y) {
    ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams)myView.getLayoutParams();
    lp.leftMargin = x;
    lp.rightMargin = y;
    requestLayout();
}

OTHER TIPS

Here is a example for you:

LinearLayout.LayoutParams lp = (android.widget.LinearLayout.LayoutParams) logoImage
                .getLayoutParams();
    lp.setMargins(0, 28, 0, 0);
        logoImage.setLayoutParams(lp);

if your layout is a relativelayout ,then change the type LinearLayout.LayoutParams .

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top