Reputation: 13
I want to add 5 blocks of TextView and Edit Text as below
Text View ---- Edit Text ---- Text View
Text View ---- Edit Text ---- Text View
Text View ---- Edit Text ---- Text View
Text View ---- Edit Text ---- Text View
Text View ---- Edit Text ---- Text View
I have tried the following:
LinearLayout rootLayout = (LinearLayout) findViewById(R.id.root_layout);
for (int i = 0; i < 6; i++) {
rootLayout.setOrientation(LinearLayout.HORIZONTAL);
TextView textView = new TextView(this);
textView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT,1));
textView.setText("Text");
rootLayout.addView(textView);
EditText editText = new EditText(this);
editText.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT,1));
rootLayout.addView (editText);
TextView addTextView = new TextView(this);
addTextView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT,1));
addTextView.setText("Additional Text");
rootLayout.addView(addTextViewtextView);
// TextView dividerLine = new TextView(this);
// rootLayout.setOrientation(LinearLayout.VERTICAL);
// rootLayout.addView(dividerLine);
Using the code above the all 15 (3*5) views are added horizontally. When I uncomment the last three line then all views are added vertically. It appears that the layout is set based on the last setOrientation statement in the program.
Upvotes: 1
Views: 3096
Reputation: 1982
LinearLayout rootLayout = (LinearLayout) findViewById(R.id.root_layout);
rootLayout.setOrientation(LinearLayout.VERTICAL);
//this layout still needs to be vertical to hold the children.
for (int i = 0; i < 6; i++) {
//make a new horizontal LinearLayout each time to hold the children.
LinearLayout temp = new LinearLayout(this);
temp.setOrientation(LinearLayout.HORIZONTAL);
temp.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT,1));
TextView textView = new TextView(this);
textView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT,1));
textView.setText("Text");
temp.addView(textView); //add them to this temporary layout.
EditText editText = new EditText(this);
editText.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT,1));
temp.addView (editText);
TextView addTextView = new TextView(this);
addTextView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT,1));
addTextView.setText("Additional Text");
temp.addView(addTextViewtextView);
rootLayout.addView(temp);
In this way, you can add several linear layouts inside one. So basically, for each set of TextView
s, you are making a separate LinearLayout
, and then adding each of these layouts to your main LinearLayout
which is still vertical in orientation.
Upvotes: 3
Reputation: 329
Your code should be followed this way.
A root layout by findViewById(), set it's orientation vertical.
Start for loop
Take a linear layout, set orientation as horizontal 3.1 add text view 3.2 add edit text 3.3 add text view
Add this 3rd step linear layout to root layout.
For loop stop.
Upvotes: 1