Changing position of a button

Vishal Arora picture Vishal Arora · Apr 13, 2011 · Viewed 64.2k times · Source

I have one button in an AbsoluteLayout in an XML file. From there I am able to set the (x,y) position of the button.

How can I get and set the (x,y) coordinates of the button programmatically?

Thanks all.

Answer

Flo picture Flo · Apr 13, 2011

You have to get a reference to you button, for example by calling findViewById(). When you got the reference to the button you can set the x and y value with button.setX() and button.setY().

....
Button myButton = (Button) findViewById(R.id.<id of the button in the layout xml file>);
myButton.setX(<x value>);
myButton.setY(<y value>);
....