Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 443
sai sindhu Avatar asked Dec 09 '22 06:12

sai sindhu


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.

like image 195
Lavanya Avatar answered Dec 31 '22 05:12

Lavanya


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
like image 42
Pratik Avatar answered Dec 31 '22 06:12

Pratik