user2959587
user2959587

Reputation: 11

To set the position of button in android programmatically in linearlayout

I have one button in an linear Layout . 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 by java program ?

Upvotes: 1

Views: 5073

Answers (2)

CoolMonster
CoolMonster

Reputation: 2266

u cant do this with linear layout use Relative layout for positioning the component to absolute position inside parent view.

https://stackoverflow.com/a/3295056/1405008

refer the above answer for more information.

Upvotes: 1

Nambi
Nambi

Reputation: 12042

LinearLayout.LayoutParams btn = new LinearLayout.LayoutParams(
                    R.id.layout_name);

btn.leftMargin = myXPosition;
btn.topMargin = myYPosition;
btn.width = buttonW;
btn.height = buttonH;

myButton.setLayoutParams(btn);

Upvotes: 0

Related Questions