Generally we add layout from xml but sometimes we need to add layout using java. Here I am adding linear layout from java file. I am creating LinearLayout class which is provided by Android and add params on it. We have methods to set orientation, gravity etc.
public class DynamicLayoutActivity extends Activity {
LinearLayout dynamicLinearLayout;
TextView text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
dynamicLinearLayout = new LinearLayout(this);
dynamicLinearLayout.setOrientation(LinearLayout.VERTICAL);
LayoutParams layoutParam = new LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
dynamicLinearLayout.setLayoutTexts(text);
text = new TextView(this);
text.setLayoutTexts(new LayoutTexts(LinearLayout.LayoutTexts.MATCH_PARENT,
LinearLayout.LayoutTexts.WRAP_CONTENT));
text.setText("Dynamic Layout.. Yesssss.");
text.setGravity(Gravity.CENTER);
dynamicLinearLayout.addView(text);
setContentView(dynamicLinearLayout);
}
}
0 Comment(s)