Android: Height of table cell should get height of previous cell

Bevor

I'm creating a TableLayout programmatically. A table row can consist of an amount + unit (cell 1), and ingredient (cell 2) and a delete button (cell 3). The ingredients can be longer than the available width, so I used the weight attribute and set it to 1 to enable a line break:

setLayoutParams(new TableRow.LayoutParams(0, TableRow.LayoutParams.WRAP_CONTENT, 1f));

This works. The problem is that the delete button prevents the table row to increase the height, so it is partly hidden which looks like this:

hidden row

This is the important part of the code that produces one table row:

final TableRow tableRow = new TableRow(getApplicationContext());
tableRow.setTag(INGREDIENT_ENTRY);
tableRow.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));

// Amount and unit
int dp6InPixel = PixelCalculator.convertDpToPixel(getApplicationContext(), 6);
TextView tvAmountAndUnitText = new TextView(getApplicationContext());
tvAmountAndUnitText.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
tvAmountAndUnitText.setText(strAmount + " " + strUnit);
tvAmountAndUnitText.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.black));
tvAmountAndUnitText.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16f);
tvAmountAndUnitText.setGravity(Gravity.RIGHT);
tvAmountAndUnitText.setTypeface(tvAmountAndUnitText.getTypeface(), Typeface.BOLD);
tvAmountAndUnitText.setPadding(dp6InPixel, 0, dp6InPixel, 0);
tableRow.addView(tvAmountAndUnitText);

// Ingredient
TextView tvIngredientText = new TextView(getApplicationContext());
tvIngredientText.setLayoutParams(new TableRow.LayoutParams(0, TableRow.LayoutParams.WRAP_CONTENT, 1f));
tvIngredientText.setText(strIngredient);
tvIngredientText.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.black));
tvIngredientText.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16f);
tableRow.addView(tvIngredientText);

// Button
int dp10InPixel = PixelCalculator.convertDpToPixel(getApplicationContext(), 10);
TextView tvIngredientDeleteButton = new TextView(getApplicationContext());
LayoutParams buttonParams = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT);
buttonParams.setMargins(dp10InPixel, 0, 0, 0);
tvIngredientDeleteButton.setLayoutParams(buttonParams);
tvIngredientDeleteButton.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.black));
tvIngredientDeleteButton.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.lightred));
tvIngredientDeleteButton.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20f);
tvIngredientDeleteButton.setPadding(dp6InPixel, dp6InPixel, dp6InPixel, dp6InPixel);
tvIngredientDeleteButton.setText("x");
//more code

tableRow.addView(tvIngredientDeleteButton);
ingredientTable.addView(tableRow); 

When I set tvIngredientDeleteButton.setMinLines(2);, then I can see the full ingredient cell. Unfortunately all rows have a min height of 2 then which looks ugly. I need some way to recognize if the ingredient cell has a line break and set minLines for that case or any other good solution (but I will not count ingredient characters or something. I guess this can be solved with some table attributes or similar). Any ideas how to solve this problem?

Chitrang

TableRow is direct subclass of LinearLayout. And inorder to set how child views are positioned in it, you need to define it's gravity. Default value is TOP, and I've tried with FILL, CENTER_VERTICAL etc. So any value of setGravity() other than TOP will render TextView with full content. Please refer official document, for more detail.

So you just need to add one statement at a time of declaration of TableRow to achieve your requirement.

          tableRow.setGravity(Gravity.FILL);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Android: Height of table cell should get height of previous cell

From Dev

jquery to get the height of withing the table cell

From Dev

Get UITableView cell height

From Dev

How is table cell height calculated?

From Dev

Force table cell to get the biggest height from the other cells in a row

From Dev

How to set height of table cell "display: table-cell"

From Dev

How to expand the height of a Table View Cell in Swift?

From Dev

Table cell box model: extra width and height

From Dev

How to set table border less then cell height

From Dev

Table-cell <a> element different height in Chrome

From Dev

Make div height 100% in a table cell

From Dev

How to make <a> in table cell height 100%?

From Dev

HTML table dynamic height for one cell

From Dev

Autolayout: table cell with variable height and two labels

From Dev

Set cell height in PDF table created with itextsharp

From Dev

Max-height of table-cell

From Dev

Table Cell 1:1 ratio for height at %-width

From Dev

Height issue with display:table-cell

From Dev

Why image not fit to table cell height in html?

From Dev

Image causing table cell height to extend

From Dev

CSS table-cell height equal to img height

From Dev

Fit the Cell content to the cell height in HTML CSS table

From Dev

Fit the Cell content to the cell height in HTML CSS table

From Dev

Get Pixel Position of Item or Cell Height in Treeview

From Dev

Get height and width from TableLayoutPanel cell

From Dev

Auto-Layout: Get UIImageView height to calculate cell height correctly

From Dev

UICollectionView - dynamic cell height?

From Dev

TCPDF - Multicell cell height

From Dev

Minimum cell height with UITableViewAutomaticDimension

Related Related

HotTag

Archive