I ran into a similar problem and I found the answer here. Basically, you should choose the LayoutParams depending on the parent, in your case new LinearLayout.LayoutParams should be new RelativeLayout.LayoutParams.
I ran into a similar problem and I found the answer here. Basically, you should choose the LayoutParams depending on the parent, in your case new LinearLayout.LayoutParams should be new RelativeLayout.LayoutParams.