10

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.

Kevin Coppock
  • 133,643
  • 45
  • 263
  • 274
Vishal Arora
  • 543
  • 3
  • 8
  • 19
  • Related post - [Android - How to place a button on a specific position on different screen densities with XML](https://stackoverflow.com/q/9791194/465053) – RBT Dec 30 '19 at 04:50

4 Answers4

10

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>);
....
Flo
  • 27,355
  • 15
  • 87
  • 125
  • 1
    it says the method setX(int) is undefined for the type Button. – Vishal Arora Apr 13 '11 at 09:37
  • Mh, that's weird. The Button class extends the View class and there you can find the both methods setX() and setY(). Try this instead: View myButton = (View) findViewById(R.id.); – Flo Apr 13 '11 at 09:42
  • we have methods for all the properties, but dnt have any method to set x and y for the buttons or other widgets. :( – Vishal Arora Apr 13 '11 at 09:43
  • dint set for view either..i am using 2.2 google sdk. is it anything to do with the sdk version?? – Vishal Arora Apr 13 '11 at 09:48
  • Check out the method [layout()](http://developer.android.com/reference/android/view/View.html#layout%28int,%20int,%20int,%20int%29) – Flo Apr 13 '11 at 10:05
  • yeah..i tried in 3.0 honeycomb, it works..but how do i make it work in lower versions..i cant make an app in higher version coz it wont b useful for devices wid lower version OS.. – Vishal Arora Apr 13 '11 at 10:05
  • i got the solution.. offsetLeftAndRight() and offsetTopAndBottom() works..but if we specify the co-ordinates in xml, it wont work..otherwise it works properly..thanks a lot for ur time and support.. – Vishal Arora Apr 13 '11 at 10:14
  • yes..i tried and it seems like working, provided we dont give co-ordinates in xml in this case also..but i really dnt understand how this l, r, t, b thing works..i gave all 10, and it appeared on top left corner – Vishal Arora Apr 13 '11 at 10:19
  • Didn't work, create 12 buttons with various values passes to setX and setY. See one massive button in the WRONG place. – Paul McCarthy Jan 28 '20 at 22:05
5

The answer you're looking for is in LayoutParams. Firstly, I'd suggest not using AbsoluteLayout -- it's deprecated -- and using something else, maybe a FrameLayout, and just using the left and top margins as your x and y offsets.

However, to answer your question:

Button button = (Button)findViewById(R.id.my_button);
AbsoluteLayout.LayoutParams absParams = 
    (AbsoluteLayout.LayoutParams)button.getLayoutParams();
absParams.x = myNewX;
absParams.y = myNewY;
button.setLayoutParams(absParams);

Or alternatively:

Button button = (Button)findViewById(R.id.my_button);
button.setLayoutParams(new AbsoluteLayout.LayoutParams(
    AbsoluteLayout.LayoutParams.FILL_PARENT, AbsoluteLayout.LayoutParams,
    myNewX, myNewY));
Kevin Coppock
  • 133,643
  • 45
  • 263
  • 274
0

hummm u can try this. . . Is there an easy way to add a border to the top and bottom of an Android View? Just go threw this site u will get the Sollution. If not then reply to me. And if its help u then give me a vote. Thanks.

Community
  • 1
  • 1
0

Try using the layout-method of the View: http://developer.android.com/reference/android/view/View.html#layout(int,%20int,%20int,%20int)

thoredge
  • 12,237
  • 1
  • 40
  • 55