Skip to content Skip to sidebar Skip to footer

Setting Parameter Of Layout Of Android Programmatically

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.

Solution 1:

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.

Solution 2:

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

Post a Comment for "Setting Parameter Of Layout Of Android Programmatically"