Programmatically generated GridLayout has column being pushed to the right - android

I am programmatically generating a GridLayout of TextViews. I want the second and third columns (TextViews) of the GridLayout to have equal width, and when there is a lot of text in the TextViews, I want the TextViews to expand vertically downwards instead of occupying more space. I am trying to achieve this by programmatically setting the column weight of the second and third TextViews to be 1. However, when there is lot of text in the second TextView for instance, the second and third TextViews just get pushed to the side. Below is a screenshot:
However, this is what I want my application to look like:
Below is my XML code:
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:rowCount="1"
android:columnCount="3">
<!-- Difference Table -->
<LinearLayout
android:orientation="vertical"
android:id="#+id/difference_table"
android:layout_width="match_parent"
android:layout_column="0"
android:layout_row="0"
android:layout_columnWeight="1"
android:layout_height="wrap_content"></LinearLayout>
</GridLayout>
Below is my Kotlin code:
val diffLayout = findViewById<LinearLayout>(R.id.difference_table)
var diffTable: GridLayout = GridLayout(this)
// Set the dimensions of the grid
diffTable.layoutParams =
ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
diffTable.columnCount = 3;
diffTable.rowCount = 1;
// Create the three columns and add them to the grid
var lineNumber: TextView = TextView(this)
lineNumber.text = "asdf"
lineNumber.setTextColor(Color.parseColor("#000000"))
lineNumber.gravity = Gravity.LEFT
var currentLine1: TextView = TextView(this)
currentLine1.setTextColor(Color.parseColor("#000000"))
currentLine1.text = "asdfasdfasdfadfasdfadfasdfasdfasdfasdfasdfasdf"
lineNumber.gravity = Gravity.LEFT
// 3. Create the TextView representing the current line in Text 2
var currentLine2: TextView = TextView(this)
currentLine2.setTextColor(Color.parseColor("#000000"))
currentLine2.text = "asdfasdfasdfadfasdfadfasdfasdfasdfasdfasdfasdf"
lineNumber.gravity = Gravity.LEFT
// TODO: ENABLE LAYOUT WEIGHT FOR API < 21
diffTable.addView(lineNumber, GridLayout.LayoutParams(
GridLayout.spec(0, GridLayout.CENTER),
GridLayout.spec(0, GridLayout.CENTER, 0.0f)));
// Add the current line in Text 1 to the table
diffTable.addView(currentLine1, GridLayout.LayoutParams(
GridLayout.spec(0, GridLayout.CENTER),
GridLayout.spec(1, GridLayout.CENTER, 1.0f)));
// Add the current line in Text 2 to the table
diffTable.addView(currentLine2, GridLayout.LayoutParams(
GridLayout.spec(0, GridLayout.CENTER),
GridLayout.spec(2, GridLayout.CENTER, 1.0f)));

1) There should be something wrong with flag on extending GridLayouts.
Your both Views inside GridLayout should have weight for vertical and horizontal axis. Try to setup this params for your first and second Views above.
final GridLayout.LayoutParams params = new GridLayout.LayoutParams(GridLayout.spec(
GridLayout.UNDEFINED,GridLayout.FILL,1f),
GridLayout.spec(GridLayout.UNDEFINED,GridLayout.FILL,1f));
2) As an alternative and simple solution, you could replace your both Views above with single LinearLayout container, which could handle weight by it's own. Something like this.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:nestedScrollingEnabled="true"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/view.line.1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal">
<TextView]
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
</LinearLayout>
<LinearLayout
android:id="#+id/view.line.2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal">
<TextView]
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
</LinearLayout>
<LinearLayout>

I modified a bit in your previous code and added the width = 0property, which may achieve the display effect you want
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// setContentView(R.layout.activity_main)
// val diffLayout = findViewById<LinearLayout>(R.id.difference_table)
// val diffTable = findViewById<GridLayout>(R.id.grid_layout);
var diffTable: GridLayout = GridLayout(this)
// Set the dimensions of the grid
diffTable.layoutParams =
ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
diffTable.columnCount = 3
diffTable.rowCount = 1
// Create the three columns and add them to the grid
var lineNumber: TextView = TextView(this)
lineNumber.text = "asdf"
lineNumber.setTextColor(Color.parseColor("#000000"))
lineNumber.gravity = Gravity.LEFT
var currentLine1: TextView = TextView(this)
currentLine1.setTextColor(Color.parseColor("#000000"))
currentLine1.text = "asdfasdfasdfadfasdfadfasdfasdfasdfasdfasdfasdf"
lineNumber.gravity = Gravity.CENTER
// 3. Create the TextView representing the current line in Text 2
var currentLine2: TextView = TextView(this)
currentLine2.setTextColor(Color.parseColor("#000000"))
currentLine2.text = "asdfasdfasdfadfasdfadfasdfasdfasdfasdfasdfasdf"
lineNumber.gravity = Gravity.CENTER
var layoutParams = GridLayout.LayoutParams()
layoutParams.rowSpec = GridLayout.spec(0, GridLayout.FILL)
layoutParams.columnSpec = GridLayout.spec(0, GridLayout.FILL)
// TODO: ENABLE LAYOUT WEIGHT FOR API < 21
diffTable.addView(lineNumber, layoutParams)
var layoutParams1 = GridLayout.LayoutParams()
layoutParams1.rowSpec = GridLayout.spec(0, GridLayout.FILL)
layoutParams1.columnSpec = GridLayout.spec(1, GridLayout.FILL, 1f)
layoutParams1.width = 0
// Add the current line in Text 1 to the table
diffTable.addView(currentLine1, layoutParams1)
// Add the current line in Text 2 to the tableLEFT
var layoutParams2 = GridLayout.LayoutParams()
layoutParams2.rowSpec = GridLayout.spec(0, GridLayout.FILL)
layoutParams2.columnSpec = GridLayout.spec(2, GridLayout.FILL, 1f)
layoutParams2.width = 0
diffTable.addView(currentLine2, layoutParams2)
addContentView(diffTable, ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT))
}

My suggestion is to dynamically set the column width for the views. Finally it will align each view with required amount of space.
var diffTable: GridLayout = GridLayout(this)
diffTable.setAlignmentMode(GridLayout.ALIGN_BOUNDS);
diffTable.setColumnCount(3);
diffTable.setRowCount(1);
TextView lineNumber;
for (int i = 0; facilities != null && i < facilities.size(); i++) {
lineNumber = new TextView(getContext());
lineNumber.setText(facilities.get(i));
diffTable.addView(lineNumber, i);
lineNumber.setCompoundDrawablesWithIntrinsicBounds(rightIc, 0, 0, 0);
GridLayout.LayoutParams param =new GridLayout.LayoutParams();
param.height = LayoutParams.WRAP_CONTENT;
param.width = LayoutParams.WRAP_CONTENT;
param.rightMargin = 5;
param.topMargin = 5;
param.setGravity(Gravity.CENTER);
param.columnSpec = GridLayout.spec(c);
param.rowSpec = GridLayout.spec(r);
lineNumber.setLayoutParams (param);
}
Where c stands for 'column index' and r stands for row index of GridLayout.
Note: The above code may require a bit of change if you want to insert your parameters.

Related

Gridview item programmatically from xml code

I want to add views (in this example, ImageViews) to a grid layout programmatically. My grid is fixed to 5 columns and 7 rows (35 items in total) and I don't want to add these 35 views in my xml so I want to do it programmatically. I'm struggling to find a conversion from xml to java/kotlin code for the attributes layout_width, layout_height, layout_columnWeight and layout_rowWeight so that the layout looks exactly the same as if I have defined the views in the xml. Can someone write the code for the views' layoutparams that matches these attributes?
<GridLayout
android:id="#+id/gridView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="fill"
android:columnCount="5"
android:rowCount="7">
<ImageView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
app:srcCompat="#drawable/ic_photo_camera"/>
<ImageView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
app:srcCompat="#drawable/ic_photo_camera"/>
... //35 times the Image view
</GridLayout>
The previous xml would give me the next layout:
I tried the next one without success (Imageviews are showing too big and not fitting all inside the grid boundaries):
for(i in 1..35) {
val view = ImageView(context)
val params = GridLayout.LayoutParams(
GridLayout.spec(GridLayout.UNDEFINED, GridLayout.FILL,1f),
GridLayout.spec(GridLayout.UNDEFINED,GridLayout.FILL,1f))
view.layoutParams = params
gridView.addView(view, 0)
}
Can you help me translate the xml into java/kotlin code? Thanks in advance
First step would be to create a view object
val image = ImageView()
image.setImageResource(resourceID)
Then you'll need to set layout attributes
val params = GridLayout.LayoutParams(x, y)
//x and y is your size, if you want something special you can do for instance LayoutParams.FILL_PARENT
You can also set size a bit later:
params.height = X
params.width = Y
Lastly you just add the view to the layout
gridView.addView(image)
Put that all inside a for loop and you're done. GridView would take care of placing the views.
Result :
Try something like this :
final int MAX_COLUMN = gridView.getColumnCount(); //5
final int MAX_ROW = gridView.getRowCount(); //7
final int itemsCount = MAX_ROW * MAX_COLUMN; //35
int row = 0, column = 0;
for (int i = 0; i < itemsCount; i++) {
ImageView view = new ImageView(this);
//Just to provide alternate colors
if (i % 2 == 0) {
view.setBackgroundColor(Color.RED);
} else {
view.setBackgroundColor(Color.GREEN);
}
GridLayout.LayoutParams params = new GridLayout.LayoutParams(GridLayout.spec(row, 1F), GridLayout.spec(column, 1F));
view.setLayoutParams(params);
gridView.addView(view);
column++;
if (column >= MAX_COLUMN) {
column = 0;
row++;
}
}
If you want specific width and height for your cells, then use :
params.width = 100; // Your width
params.height = 100; //your height

Textview marquee programmatically

Trying to populate an textview(s) from an array. I managed to get the desired effect via XML via the code below
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/marque_scrolling_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:padding="16dp"
android:scrollHorizontally="true"
android:singleLine="true"
android:text="Create a Marquee (Scrolling Text) in Android Using TextView. Android Marquee (Scrolling Text) Tutorial with Example"
android:textSize="24sp" />
</LinearLayout>
But I need several of these in an array - so I believe creating the textviews programmatically may be the answer, but cant get them to marquee. This is the code im working on.
String[] strings = {"Mark", "James", "Paul"};
LinearLayout layout = (LinearLayout)findViewById(R.id.linearlayout);
for(String s : strings)
{
TextView newTextView = new TextView(this);
newTextView.setText(s);
newTextView.setSingleLine(true);
newTextView.setEllipsize(TextUtils.TruncateAt.END);
newTextView.setHorizontallyScrolling(true);
newTextView.setLines(1);
newTextView.setMarqueeRepeatLimit(-1);
newTextView.setSelected(true);
newTextView.setTextSize(24);
layout.addView(newTextView);
}
Try this.. It work
TextView testing = (TextView) this.findViewById(R.id.testing);
testing.setEllipsize(TextUtils.TruncateAt.MARQUEE);
testing.setMarqueeRepeatLimit(-1);
testing.setSingleLine(true);
testing.setSelected(true);
Try this.
final RelativeLayout relativeLayout = new RelativeLayout(this);
final RelativeLayout relativeLayoutbotombar = new RelativeLayout(this);
textView = new TextView(this);
textView.setId(1);
RelativeLayout.LayoutParams relativlayparamter = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT);
RelativeLayout.LayoutParams relativlaybottombar = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
relativeLayoutbotombar.setLayoutParams(relativlaybottombar);
textView.setText("text text text text text, with a long ");
textView.setEllipsize(TruncateAt.MARQUEE);
textView.setSelected(true);
textView.setSingleLine(true);
relativeLayout.addView(relativeLayoutbotombar);
relativeLayoutbotombar.addView(textView);
setContentView(relativeLayout, relativlayparamter);
Another option:
TextView textView = (TextView) this.findViewById(R.id.xxx);
textView.setEllipsize(TruncateAt.MARQUEE);
textView.setText("Text text text text");
textView.setSelected(true);
textView.setSingleLine(true);
Use this code. I improved your code, enjoy copy paste.
val strings = arrayOf("Mark", "James", "Paul")
val layout = findViewById<View>(R.id.linear) as LinearLayout
layout.orientation = LinearLayout.VERTICAL
for (s in strings) {
val newTextView = TextView(this)
newTextView.layoutParams = ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT)
newTextView.text = s
newTextView.isSingleLine = true
newTextView.ellipsize = TextUtils.TruncateAt.END
newTextView.setHorizontallyScrolling(true)
newTextView.setLines(1)
newTextView.marqueeRepeatLimit = -1
newTextView.isSelected = true
newTextView.textSize = 24f
addMarquee(newTextView)
layout.addView(newTextView)
}
Also add this function to your class
fun addMarquee(textView: TextView) {
textView.viewTreeObserver.addOnGlobalLayoutListener(object :
ViewTreeObserver.OnGlobalLayoutListener {
override fun onGlobalLayout() {
val pixels = textView.measuredWidth - 1
val params = textView.layoutParams
params.width = pixels
textView.layoutParams = params
textView.isSelected = true
textView.ellipsize = TextUtils.TruncateAt.MARQUEE
textView.isSingleLine = true
textView.marqueeRepeatLimit = -1
textView.viewTreeObserver.removeOnGlobalLayoutListener(this)
}
})
}

How to add rows with buttons dynamically into table layout to have equal width

I would like to add some buttons with equal width into table layout dynamically. I've already tried everything from StackOverflow, but I cann't find a right solution.
This is my initial XML-layout:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#color/puzzle_background"
android:orientation="vertical"
android:layout_height="fill_parent"
android:layout_width="match_parent"
android:layout_gravity="center">
<TextView
android:text="#string/levels_label"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dip"
android:layout_marginBottom="10dip"
android:textSize="28sp" />
<TableLayout
android:id="#+id/displayLevels"
android:background="#color/puzzle_background"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="vertical" >
</TableLayout>
</LinearLayout>
This is my code part:
int i = 0;
int j = 0;
while (i<levelNum) {
j = i + 4;
row = new TableRow(this);
TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT);
row.setLayoutParams(lp);
row.setWeightSum(4);
while ((i<j)&&(i<levelNum)) {
Button iBtn = new Button(this);
iBtn.setGravity(Gravity.CENTER_HORIZONTAL);
iBtn.setText(Integer.toString(i + 1));
iBtn.setId(i + 1);
iBtn.setOnClickListener(btnclick);
row.addView(iBtn, 4 + i - j);
i++;
}
lTable.addView(row);
}
But every time I get 4 buttons in the row which don't really fit into the row. The right button in the row is always out of the screen.
What am I doing wrong? Please advice.
With best regards, Alex
I will propose you another way, you could get witdh of your screen by code:
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int width1 = displaymetrics.widthPixels;
and next set width of your button by this:
iBtn.getLayoutParams().width = width1;
or you could else set max width of your button by similar function.
I hope this could help.
after some try, I have solution:
First, your logic has some problem, two while loops equal to one while loop. It confused me.
Second, Button in android has default minimunWidth, so if you setWidth of button less than default value, it will set width with default value.
Third, I use gravity.CENTER is to have all buttons in CENTER.
ok, that is my explain. the following is my code:
TableLayout displayLevels = (TableLayout)findViewById(R.id.displayLevels);
final int screenWidth = dpToPx(getResources().getConfiguration().screenWidthDp);
int i = 0;
int j = 0;
int levelNum = 8;
TableRow row = new TableRow(this);
TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT);
row.setLayoutParams(lp);
row.setGravity(Gravity.CENTER);
int width = (screenWidth - (levelNum)*4)/levelNum;//4: I don't remember name of it. It is default value.4dp or 4px... I don't remember exactly
displayLevels.addView(row);
while (i<levelNum) {
Button iBtn = new Button(this);
iBtn.setGravity(Gravity.CENTER_HORIZONTAL);
iBtn.setMinimumWidth(100);//I set 100px for minimunWidth.
iBtn.setWidth(width);
iBtn.setText(Integer.toString(i + 1));
iBtn.setId(i + 1);
//iBtn.setOnClickListener(btnclick);
row.addView(iBtn, i);
i++;
}
with:
private int dpToPx(int dp) {
return (int) (dp * getResources().getDisplayMetrics().density + 0.5f);
}

android - adding button dynamically when each button width is different , automicatically flow to next row when width is full

I need to create buttons dynamically to a linear layout. The width of each button will be wrap_content. Once the whole row is full, the button should be created in the next row.
I have seen other answers in stackOverflow where this is done, but in those cases the no of buttons in a row is constant. In my case it is dynamic, because the width of each button is dynamic based on text width.
It is similar to how tags are added to a question in this site.
e.g. the answers in one of questions was:
LinearLayout layout = (LinearLayout) findViewById(R.id.linear_layout_tags);
layout.setOrientation(LinearLayout.VERTICAL); //Can also be done in xml by android:orientation="vertical"
for (int i = 0; i < 3; i++) {
LinearLayout row = new LinearLayout(this);
row.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
for (int j = 0; j < 4; j++) {
Button btnTag = new Button(this);
btnTag.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
btnTag.setText("Button " + (j + 1 + (i * 4));
btnTag.setId(j + 1 + (i * 4));
row.addView(btnTag);
}
layout.addView(row);
}
But here the no of buttons in a row is constant. In my case , this will be different because the width of each button is different.
You need a FlowLayout, which is an Extended linear layout that wrap its content when there is no place in the current line.
Check this out: https://github.com/ApmeM/android-flowlayout
Have you tried using GridLayout ?
linearLayoutParent = findViewById(R.id.ll_buttons_parent); // container layout or parent layout - orientation vertical
LayoutInflater inflater = getLayoutInflater();
View view = inflater.inflate(R.layout.button_horizontal_layout, linearLayoutParent, false);
LinearLayout linearLayoutButton = view.findViewById(R.id.ll_button_horizontal);
int rows = 3;
for (int r = 0; r < rows; r++) {
GridLayout gridLayout = new GridLayout(this);
gridLayout.setColumnCount(2);
for (int i = 0; i < 4; i++) {
Button optionButton = new Button(this);
optionButton.setGravity(Gravity.CENTER);
optionButton.setText(String.valueOf(i + 1));
gridLayout.addView(optionButton); // adding buttons to grid layout
}
linearLayoutButton.addView(gridLayout); // adding grid layout to vertical linear layout row by row
}
linearLayoutParent.addView(linearLayoutButton); //adding linear layouts to container layout
Container : ll_buttons_parent
<LinearLayout
android:id="#+id/ll_buttons_parent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintTop_toTopOf="parent"/>
button_horizontal_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/ll_button_horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#F8CCCC"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
app:layout_constraintTop_toTopOf="parent" />

android tablelayout expand row height to listview contents

I have a table layout for a form that has two columns - a field name, and a field value. In some cases the field value is a checklist. I'd like the row height of the "checklist" row to expand so I can see all of the items in the listview.
The following code shows the listview in a tiny vertical scrollable view, the same height as the field name in the first column. How do I expand the row height so I can see the entire list? (The FrameLayout in the table cell is so I can overlay an icon or text).
// get the table
TableLayout table = FindViewById<TableLayout>(Resource.Id.formTable);
// make a new table row
TableRow row = new TableRow(this);
// add the field name (left column)
TextView rowName = new TextView(this);
rowName.Text = field.Name + ":";
rowName.Gravity = GravityFlags.Right | GravityFlags.CenterVertical;
TableRow.LayoutParams tableLayoutParams = new TableRow.LayoutParams(LinearLayout.LayoutParams.FillParent, LinearLayout.LayoutParams.FillParent);
tableLayoutParams.Gravity = GravityFlags.Right | GravityFlags.CenterVertical;
tableLayoutParams.Weight = 1.0f;
tableLayoutParams.Width = 0;
row.AddView(rowName, tableLayoutParams);
// add the field value (right column - in this case, a listview for a checklist)
FrameLayout frameLayout = new FrameLayout(this);
List<string> strings = field.Items.ToList();
ListView listView = new ListView(this);
listView.Adapter = new ListViewFormAdapterCheckList(this, strings);
frameLayout.AddView(listView);
tableLayoutParams = new TableRow.LayoutParams(TableLayout.LayoutParams.WrapContent, TableLayout.LayoutParams.WrapContent);
tableLayoutParams.SetMargins(0, 10, 0, 0);
tableLayoutParams.Width = 0;
row.AddView(frameLayout, tableLayoutParams);
Never got this working with the listview. Used a linearlayout instead...
// get the table
TableLayout table = FindViewById<TableLayout>(Resource.Id.formTable);
// make a new table row
TableRow row = new TableRow(this);
// put the field name at the top of the row
rowName.Gravity = GravityFlags.Right | GravityFlags.Top;
rowName.SetPadding(0, 20, 20, 0);
// frame layout so we can overlay an image / icon over the list
FrameLayout frameLayout = new FrameLayout(this);
frameLayout.Id = kCurrentFieldId++;
frameLayout.Tag = index;
mFieldToView[field] = frameLayout;
List<string> strings = field.Items.ToList();
LinearLayout listLayout = new LinearLayout(this);
listLayout.Orientation = Android.Widget.Orientation.Vertical;
LayoutInflater inflater = (LayoutInflater) GetSystemService(Context.LayoutInflaterService);
foreach (string item in strings)
{
View v = inflater.Inflate(Resource.Layout.list_checklist, null);
v.FindViewById<CheckBox>(Resource.Id.checkbox).Text = item;
listLayout.AddView(v);
}
frameLayout.AddView(listLayout);
tableLayoutParams = new TableRow.LayoutParams(TableLayout.LayoutParams.WrapContent, TableLayout.LayoutParams.WrapContent);
tableLayoutParams.SetMargins(0, 10, 0, 0);
tableLayoutParams.Width = 0;
tableLayoutParams.Weight = 2.0f;
row.AddView(frameLayout, tableLayoutParams);
list_checklist defined as:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="5dip">
<CheckBox
android:id="#+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:gravity="center_vertical"
android:text="checkdesc" />
</RelativeLayout>

Categories

Resources