[ACCEPTED]-Changing position of a button-absolutelayout

Accepted answer
Score: 10

You have to get a reference to you button, for 3 example by calling findViewById(). When you got the reference 2 to the button you can set the x and y value 1 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>);
....
Score: 5

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

However, to answer your 1 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));
Score: 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 3 this site u will get the Sollution. If not 2 then reply to me. And if its help u then 1 give me a vote. Thanks.

More Related questions