try this structure whenever you want to create any thing dynamically.
public class PhrasesActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.phrases);
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.LinearLayout1);
LinearLayout L2 = new LinearLayout(this);
L2.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
L2.setGravity(Gravity.CENTER);
TextView a1 = new TextView(this);
a1.setText("Dynamic layouts ftw!");
a1.setVisibility(View.VISIBLE);
Button b1 = new Button(this);
b1.setText("SUBMIT");
L2.addView(a1,(new LayoutParams(0,LayoutParams.WRAP_CONTENT,0.8f)));
L2.addView(b1,(new LayoutParams(0,LayoutParams.WRAP_CONTENT,0.8f)));
linearLayout.addView(L2);
}
}
Let me know you understand or not?