Skip to content Skip to sidebar Skip to footer

How To Set Margin In Inflated Linearlayout Dynamically?

I have added a linearlayout in a linearlayout dynamically using this code. LinearLayout root = (LinearLayout) findViewById(R.id.root); View child = inflater.inflate(R.layout.child

Solution 1:

Viewchild= inflater.inflate(R.layout.childrow, null);
LinearLayout.LayoutParamslayoutParams=newLinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); 
layoutParams.setMargins(leftMargin, topMargin,rightMargin, bottomMargin);
child.setLayoutParams(layoutParams);
root.addView(child , index++);

Solution 2:

LayoutParams params=(LayoutParams) child.getLayoutParams();
    params.setMargins(0, 0, 0, 5);
child.setLayoutParams(params);

The params.setMargins(0, 0, 0, 5); allows to set margins for left, top, right and bottom respectively. So to set a margin to the bottom of your child view, replace 5 with a number of your choice.

Hope this will help.. :)

Post a Comment for "How To Set Margin In Inflated Linearlayout Dynamically?"