4

I am new to android. I want to know how to set the parameters or attributes to layout x and layout y width and height from the program for any layouts like absolute.

0

2 Answers 2

13

For button you can try like this:

RelativeLayout.Layoutparams params = (RelativeLayout.LayoutParams)button.getLayoutParams();
params.setMargins(5, 5, 5, 5);
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
button.setLayoutParams(params);

you have setMargins() to which you pass the left, top, right and bottom values respectively.

Sign up to request clarification or add additional context in comments.

Comments

1

if you set the id for you layout like this way

<LinearLayout android:id="@+id/linear" />

then you can get the layout in code like this way.

Linearlayout linear = (LinearLayout)findViewbyId(R.id.linear");

linear.setLayoutParams(new LayoutParams(arg0, arg1));

here in arg0 and arg1 you can pass the int value of you can set the below value

LayoutParams.FILL_PARENT
LayoutParams.WRAP_CONTENT

3 Comments

Thank u Pratik. How can we set the layout for buttons. i mean to say the x and y coordinates that we do in xml file.
no can't set the button like this way because there was not property to set the x and y value. you can set the margin value to put the button on specific loc using RelativeLayout or you can draw button by overriding the draw() method
@Pratik here instead of fill_parent how can i set my width and height in dip values, like width= 300.0dip and height = 20.05dip?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.