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.
2 Answers
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.
Comments
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
sai sindhu
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.
Pratik
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
AndroidOptimist
@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?