1

I am dynamically adding linearlayouts to a main linearlayout which is contained in a scrollview. Scrollview --> MainLinearLayout --> Childrens.

Now i wanted some space between childrens so i i did the following :

 RelativeLayout.LayoutParams lp=new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,height_layout);
            lp.setMargins(50,50,50,50);
            linearLayout.setLayoutParams(lp); 

But i am unable to see any space between the childrens they all are stuck with each other.

Height width works fine but not the margins.

Techie
  • 13
  • 3

2 Answers2

2

You should use LinearLayout.LayoutParams if you want to set things to LinearLayout

e.g.:

LinearLayout.LayoutParams lp=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,height_layout);
lp.setMargins(50,50,50,50);
linearLayout.setLayoutParams(lp); 
Jonathan Darryl
  • 946
  • 11
  • 16
  • This will crash the app. LinearLayout is the children of relativelayout . read this [link](http://stackoverflow.com/questions/15501831/add-layout-with-parameters-programmatically-android) – Techie May 18 '16 at 07:16
  • @Techie however, in your case, your LinearLayout is a child of LinearLayout, isn't it? – Jonathan Darryl May 18 '16 at 07:19
  • Yes i will try it out and let you know earlier it use to crash – Techie May 18 '16 at 07:21
0

Use below code

((RelativeLayout.LayoutParams)linearLayout.getLayoutParams()).setMargins(left, top, right, bottom);
Mohit Charadva
  • 2,555
  • 1
  • 22
  • 30